nodejs / typescript

TypeScript support in Node.js core
MIT License
79 stars 0 forks source link

SyntaxError stack trace source maps #4

Open legendecas opened 1 month ago

legendecas commented 1 month ago

With https://github.com/nodejs/node/pull/54283#issuecomment-2283390708, when the source codes containing syntax errors (like yet to be supported ECMAScript syntaxes) are used with the flag --experimental-tranform-types, there is no time for --enable-source-maps to collect source maps and perform stack trace mapping, as it collect source maps information from V8 parse results.

const obj = {
  [Symbol.dispose]() {
    console.log('disposed')
  }
}

function foo() {
  using _ = obj;
}

For example, if the following snippet is been parsed as JS, the error would be:

file:///Users/cwu631/Developer/nodejs/node/a.mjs:8
  using _ = obj;
        ^

SyntaxError: Unexpected identifier '_'
    at compileSourceTextModule (node:internal/modules/esm/utils:337:16)
    at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:166:18)
    at callTranslator (node:internal/modules/esm/loader:436:14)
    at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:442:30)
    at async ModuleJob._link (node:internal/modules/esm/module_job:106:19)

Node.js v23.0.0-pre

However, if it is parsed as TS:

~/D/n/node (pr-54283)> ./node --experimental-transform-types a.mts
(node:11541) ExperimentalWarning: Type Stripping is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
file:///Users/cwu631/Developer/nodejs/node/a.mts:7
    using _ = obj
          ^

SyntaxError: Unexpected identifier '_'
    at compileSourceTextModule (node:internal/modules/esm/utils:337:16)
    at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:166:18)
    at ModuleLoader.<anonymous> (node:internal/modules/esm/translators:540:10)
    at callTranslator (node:internal/modules/esm/loader:436:14)
    at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:442:30)
    at async ModuleJob._link (node:internal/modules/esm/module_job:106:19)

Node.js v23.0.0-pre

Note that the reported syntax error line number is 7, not 8 as expected.

marco-ippolito commented 1 month ago

Is it an issue with the typescript loader implementation or with sourcemaps?

legendecas commented 1 month ago

The problem is with the source map support, but it is only outstanding with an internal loader because the transpiled sources are not visible to users.