* * Wrapper of evalScript * * This function wraps the evaluation of the source code in a try-catch block. * If the source code fails to be evaluated, it will retry evaluating the source code * with the TypeScript parser. * * If the source code fails to be evaluated with the TypeScript parser,
(name, source, breakFirstLine, print, shouldLoadESM = false)
| 277 | * @returns {void} |
| 278 | */ |
| 279 | function evalTypeScript(name, source, breakFirstLine, print, shouldLoadESM = false) { |
| 280 | const origModule = globalThis.module; // Set e.g. when called from the REPL. |
| 281 | const module = createModule(name); |
| 282 | const baseUrl = pathToFileURL(module.filename).href; |
| 283 | |
| 284 | if (shouldUseModuleEntryPoint(name, source)) { |
| 285 | return evalTypeScriptModuleEntryPoint(source, print); |
| 286 | } |
| 287 | |
| 288 | let compiledScript; |
| 289 | // This variable can be modified if the source code is stripped. |
| 290 | let sourceToRun = source; |
| 291 | try { |
| 292 | compiledScript = compileScript(name, source, baseUrl); |
| 293 | } catch (originalError) { |
| 294 | try { |
| 295 | sourceToRun = stripTypeScriptModuleTypes(source, kEvalTag); |
| 296 | // Retry the CJS/ESM syntax detection after stripping the types. |
| 297 | if (shouldUseModuleEntryPoint(name, sourceToRun)) { |
| 298 | return evalTypeScriptModuleEntryPoint(source, print); |
| 299 | } |
| 300 | // If the ContextifiedScript was successfully created, execute it. |
| 301 | // outside the try-catch block to avoid catching runtime errors. |
| 302 | compiledScript = compileScript(name, sourceToRun, baseUrl); |
| 303 | } catch (tsError) { |
| 304 | // If it's invalid or unsupported TypeScript syntax, rethrow the original error |
| 305 | // with the TypeScript error message added to the stack. |
| 306 | if (tsError.code === 'ERR_INVALID_TYPESCRIPT_SYNTAX' || tsError.code === 'ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX') { |
| 307 | originalError.stack = decorateCJSErrorWithTSMessage(originalError.stack, tsError.message); |
| 308 | throw originalError; |
| 309 | } |
| 310 | |
| 311 | throw tsError; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | const evalFunction = () => runScriptInContext(name, |
| 316 | sourceToRun, |
| 317 | breakFirstLine, |
| 318 | print, |
| 319 | module, |
| 320 | baseUrl, |
| 321 | compiledScript, |
| 322 | origModule); |
| 323 | |
| 324 | if (shouldLoadESM) { |
| 325 | return require('internal/modules/run_main').runEntryPointWithESMLoader(evalFunction); |
| 326 | } |
| 327 | evalFunction(); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Wrapper of evalModuleEntryPoint |
nothing calls this directly
no test coverage detected
searching dependent graphs…