| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f97cd5b commit 9c5c3b3
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1403,7 +1403,8 @@ Node.js will try to detect the syntax with the following steps: | |||
| 1403 | 1403 | 1. Run the input as CommonJS. | |
| 1404 | 1404 | 2. If step 1 fails, run the input as an ES module. | |
| 1405 | 1405 | 3. If step 2 fails with a SyntaxError, strip the types. | |
| 1406 | - 4. If step 3 fails with an error code [`ERR_INVALID_TYPESCRIPT_SYNTAX`][], | ||
| 1406 | + 4. If step 3 fails with an error code [`ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX`][] | ||
| 1407 | + or [`ERR_INVALID_TYPESCRIPT_SYNTAX`][], | ||
| 1407 | 1408 | throw the error from step 2, including the TypeScript error in the message, | |
| 1408 | 1409 | else run as CommonJS. | |
| 1409 | 1410 | 5. If step 4 fails, run the input as an ES module. | |
@@ -3691,6 +3692,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12 | |||
| 3691 | 3692 | [`Buffer`]: buffer.md#class-buffer | |
| 3692 | 3693 | [`CRYPTO_secure_malloc_init`]: https://www.openssl.org/docs/man3.0/man3/CRYPTO_secure_malloc_init.html | |
| 3693 | 3694 | [`ERR_INVALID_TYPESCRIPT_SYNTAX`]: errors.md#err_invalid_typescript_syntax | |
| 3695 | + [`ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX`]: errors.md#err_unsupported_typescript_syntax | ||
| 3694 | 3696 | [`NODE_OPTIONS`]: #node_optionsoptions | |
| 3695 | 3697 | [`NO_COLOR`]: https://no-color.org | |
| 3696 | 3698 | [`SlowBuffer`]: buffer.md#class-slowbuffer | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2093,11 +2093,13 @@ does not consist of exactly two elements. | |||
| 2093 | 2093 | ||
| 2094 | 2094 | <!-- YAML | |
| 2095 | 2095 | added: v23.0.0 | |
| 2096 | + changes: | ||
| 2097 | + - version: REPLACEME | ||
| 2098 | + pr-url: https://github.com/nodejs/node/pull/56610 | ||
| 2099 | + description: This error is no longer thrown on valid yet unsupported syntax. | ||
| 2096 | 2100 | --> | |
| 2097 | 2101 | ||
| 2098 | - The provided TypeScript syntax is not valid or unsupported. | ||
| 2099 | - This could happen when using TypeScript syntax that requires | ||
| 2100 | - transformation with [type-stripping][]. | ||
| 2102 | + The provided TypeScript syntax is not valid. | ||
| 2101 | 2103 | ||
| 2102 | 2104 | <a id="ERR_INVALID_URI"></a> | |
| 2103 | 2105 | ||
@@ -3096,6 +3098,18 @@ try { | |||
| 3096 | 3098 | } | |
| 3097 | 3099 | ``` | |
| 3098 | 3100 | ||
| 3101 | + <a id="ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX"></a> | ||
| 3102 | + | ||
| 3103 | + ### `ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX` | ||
| 3104 | + | ||
| 3105 | + <!-- YAML | ||
| 3106 | + added: REPLACEME | ||
| 3107 | + --> | ||
| 3108 | + | ||
| 3109 | + The provided TypeScript syntax is unsupported. | ||
| 3110 | + This could happen when using TypeScript syntax that requires | ||
| 3111 | + transformation with [type-stripping][]. | ||
| 3112 | + | ||
| 3099 | 3113 | <a id="ERR_USE_AFTER_CLOSE"></a> | |
| 3100 | 3114 | ||
| 3101 | 3115 | ### `ERR_USE_AFTER_CLOSE` | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1838,6 +1838,7 @@ E('ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING', | |||
| 1838 | 1838 | E('ERR_UNSUPPORTED_RESOLVE_REQUEST', | |
| 1839 | 1839 | 'Failed to resolve module specifier "%s" from "%s": Invalid relative URL or base scheme is not hierarchical.', | |
| 1840 | 1840 | TypeError); | |
| 1841 | + E('ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX', '%s', SyntaxError); | ||
| 1841 | 1842 | E('ERR_USE_AFTER_CLOSE', '%s was closed', Error); | |
| 1842 | 1843 | ||
| 1843 | 1844 | // This should probably be a `TypeError`. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,8 +12,10 @@ const { assertTypeScript, | |||
| 12 | 12 | isUnderNodeModules, | |
| 13 | 13 | kEmptyObject } = require('internal/util'); | |
| 14 | 14 | const { | |
| 15 | + ERR_INTERNAL_ASSERTION, | ||
| 15 | 16 | ERR_INVALID_TYPESCRIPT_SYNTAX, | |
| 16 | 17 | ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING, | |
| 18 | + ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX, | ||
| 17 | 19 | } = require('internal/errors').codes; | |
| 18 | 20 | const { getOptionValue } = require('internal/options'); | |
| 19 | 21 | const assert = require('internal/assert'); | |
@@ -49,7 +51,20 @@ function parseTypeScript(source, options) { | |||
| 49 | 51 | try { | |
| 50 | 52 | return parse(source, options); | |
| 51 | 53 | } catch (error) { | |
| 52 | - throw new ERR_INVALID_TYPESCRIPT_SYNTAX(error.message); | ||
| 54 | + /** | ||
| 55 | + * Amaro v0.3.0 (from SWC v1.10.7) throws an object with `message` and `code` properties. | ||
| 56 | + * It allows us to distinguish between invalid syntax and unsupported syntax. | ||
| 57 | + */ | ||
| 58 | + switch (error.code) { | ||
| 59 | + case 'UnsupportedSyntax': | ||
| 60 | + throw new ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX(error.message); | ||
| 61 | + case 'InvalidSyntax': | ||
| 62 | + throw new ERR_INVALID_TYPESCRIPT_SYNTAX(error.message); | ||
| 63 | + default: | ||
| 64 | + // SWC will throw strings when something goes wrong. | ||
| 65 | + // Check if has the `message` property or treat it as a string. | ||
| 66 | + throw new ERR_INTERNAL_ASSERTION(error.message ?? error); | ||
| 67 | + } | ||
| 53 | 68 | } | |
| 54 | 69 | } | |
| 55 | 70 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -35,7 +35,7 @@ const { getOptionValue } = require('internal/options'); | |||
| 35 | 35 | const { | |
| 36 | 36 | makeContextifyScript, runScriptInThisContext, | |
| 37 | 37 | } = require('internal/vm'); | |
| 38 | - const { emitExperimentalWarning, isError } = require('internal/util'); | ||
| 38 | + const { emitExperimentalWarning } = require('internal/util'); | ||
| 39 | 39 | // shouldAbortOnUncaughtToggle is a typed array for faster | |
| 40 | 40 | // communication with JS. | |
| 41 | 41 | const { shouldAbortOnUncaughtToggle } = internalBinding('util'); | |
@@ -254,10 +254,6 @@ function evalTypeScript(name, source, breakFirstLine, print, shouldLoadESM = fal | |||
| 254 | 254 | try { | |
| 255 | 255 | compiledScript = compileScript(name, source, baseUrl); | |
| 256 | 256 | } catch (originalError) { | |
| 257 | - // If it's not a SyntaxError, rethrow it. | ||
| 258 | - if (!isError(originalError) || originalError.name !== 'SyntaxError') { | ||
| 259 | - throw originalError; | ||
| 260 | - } | ||
| 261 | 257 | try { | |
| 262 | 258 | sourceToRun = stripTypeScriptModuleTypes(source, name, false); | |
| 263 | 259 | // Retry the CJS/ESM syntax detection after stripping the types. | |
@@ -270,15 +266,14 @@ function evalTypeScript(name, source, breakFirstLine, print, shouldLoadESM = fal | |||
| 270 | 266 | // Emit the experimental warning after the code was successfully evaluated. | |
| 271 | 267 | emitExperimentalWarning('Type Stripping'); | |
| 272 | 268 | } catch (tsError) { | |
| 273 | - // If its not an error, or it's not an invalid typescript syntax error, rethrow it. | ||
| 274 | - if (!isError(tsError) || tsError?.code !== 'ERR_INVALID_TYPESCRIPT_SYNTAX') { | ||
| 275 | - throw tsError; | ||
| 269 | + // If it's invalid or unsupported TypeScript syntax, rethrow the original error | ||
| 270 | + // with the TypeScript error message added to the stack. | ||
| 271 | + if (tsError.code === 'ERR_INVALID_TYPESCRIPT_SYNTAX' || tsError.code === 'ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX') { | ||
| 272 | + originalError.stack = decorateCJSErrorWithTSMessage(originalError.stack, tsError.message); | ||
| 273 | + throw originalError; | ||
| 276 | 274 | } | |
| 277 | 275 | ||
| 278 | - try { | ||
| 279 | - originalError.stack = decorateCJSErrorWithTSMessage(originalError.stack, tsError.message); | ||
| 280 | - } catch { /* Ignore potential errors coming from `stack` getter/setter */ } | ||
| 281 | - throw originalError; | ||
| 276 | + throw tsError; | ||
| 282 | 277 | } | |
| 283 | 278 | } | |
| 284 | 279 | ||
@@ -322,28 +317,23 @@ function evalTypeScriptModuleEntryPoint(source, print) { | |||
| 322 | 317 | // Compile the module to check for syntax errors. | |
| 323 | 318 | moduleWrap = loader.createModuleWrap(source, url); | |
| 324 | 319 | } catch (originalError) { | |
| 325 | - // If it's not a SyntaxError, rethrow it. | ||
| 326 | - if (!isError(originalError) || originalError.name !== 'SyntaxError') { | ||
| 327 | - throw originalError; | ||
| 328 | - } | ||
| 329 | - let strippedSource; | ||
| 330 | 320 | try { | |
| 331 | - strippedSource = stripTypeScriptModuleTypes(source, url, false); | ||
| 321 | + const strippedSource = stripTypeScriptModuleTypes(source, url, false); | ||
| 332 | 322 | // If the moduleWrap was successfully created, execute the module job. | |
| 333 | 323 | // outside the try-catch block to avoid catching runtime errors. | |
| 334 | 324 | moduleWrap = loader.createModuleWrap(strippedSource, url); | |
| 335 | 325 | // Emit the experimental warning after the code was successfully compiled. | |
| 336 | 326 | emitExperimentalWarning('Type Stripping'); | |
| 337 | 327 | } catch (tsError) { | |
| 338 | - // If its not an error, or it's not an invalid typescript syntax error, rethrow it. | ||
| 339 | - if (!isError(tsError) || tsError?.code !== 'ERR_INVALID_TYPESCRIPT_SYNTAX') { | ||
| 340 | - throw tsError; | ||
| 341 | - } | ||
| 342 | - try { | ||
| 328 | + // If it's invalid or unsupported TypeScript syntax, rethrow the original error | ||
| 329 | + // with the TypeScript error message added to the stack. | ||
| 330 | + if (tsError.code === 'ERR_INVALID_TYPESCRIPT_SYNTAX' || | ||
| 331 | + tsError.code === 'ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX') { | ||
| 343 | 332 | originalError.stack = `${tsError.message}\n\n${originalError.stack}`; | |
| 344 | - } catch { /* Ignore potential errors coming from `stack` getter/setter */ } | ||
| 333 | + throw originalError; | ||
| 334 | + } | ||
| 345 | 335 | ||
| 346 | - throw originalError; | ||
| 336 | + throw tsError; | ||
| 347 | 337 | } | |
| 348 | 338 | } | |
| 349 | 339 | // If the moduleWrap was successfully created either with by just compiling | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -102,33 +102,33 @@ test('expect fail eval TypeScript ESM syntax with input-type commonjs-typescript | |||
| 102 | 102 | strictEqual(result.code, 1); | |
| 103 | 103 | }); | |
| 104 | 104 | ||
| 105 | - test('check syntax error is thrown when passing invalid syntax', async () => { | ||
| 105 | + test('check syntax error is thrown when passing unsupported syntax', async () => { | ||
| 106 | 106 | const result = await spawnPromisified(process.execPath, [ | |
| 107 | 107 | '--eval', | |
| 108 | 108 | 'enum Foo { A, B, C }']); | |
| 109 | 109 | strictEqual(result.stdout, ''); | |
| 110 | 110 | match(result.stderr, /SyntaxError/); | |
| 111 | - doesNotMatch(result.stderr, /ERR_INVALID_TYPESCRIPT_SYNTAX/); | ||
| 111 | + doesNotMatch(result.stderr, /ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX/); | ||
| 112 | 112 | strictEqual(result.code, 1); | |
| 113 | 113 | }); | |
| 114 | 114 | ||
| 115 | - test('check syntax error is thrown when passing invalid syntax with --input-type=module-typescript', async () => { | ||
| 115 | + test('check syntax error is thrown when passing unsupported syntax with --input-type=module-typescript', async () => { | ||
| 116 | 116 | const result = await spawnPromisified(process.execPath, [ | |
| 117 | 117 | '--input-type=module-typescript', | |
| 118 | 118 | '--eval', | |
| 119 | 119 | 'enum Foo { A, B, C }']); | |
| 120 | 120 | strictEqual(result.stdout, ''); | |
| 121 | - match(result.stderr, /ERR_INVALID_TYPESCRIPT_SYNTAX/); | ||
| 121 | + match(result.stderr, /ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX/); | ||
| 122 | 122 | strictEqual(result.code, 1); | |
| 123 | 123 | }); | |
| 124 | 124 | ||
| 125 | - test('check syntax error is thrown when passing invalid syntax with --input-type=commonjs-typescript', async () => { | ||
| 125 | + test('check syntax error is thrown when passing unsupported syntax with --input-type=commonjs-typescript', async () => { | ||
| 126 | 126 | const result = await spawnPromisified(process.execPath, [ | |
| 127 | 127 | '--input-type=commonjs-typescript', | |
| 128 | 128 | '--eval', | |
| 129 | 129 | 'enum Foo { A, B, C }']); | |
| 130 | 130 | strictEqual(result.stdout, ''); | |
| 131 | - match(result.stderr, /ERR_INVALID_TYPESCRIPT_SYNTAX/); | ||
| 131 | + match(result.stderr, /ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX/); | ||
| 132 | 132 | strictEqual(result.code, 1); | |
| 133 | 133 | }); | |
| 134 | 134 | ||
@@ -140,7 +140,7 @@ test('should not parse TypeScript with --type-module=commonjs', async () => { | |||
| 140 | 140 | ||
| 141 | 141 | strictEqual(result.stdout, ''); | |
| 142 | 142 | match(result.stderr, /SyntaxError/); | |
| 143 | - doesNotMatch(result.stderr, /ERR_INVALID_TYPESCRIPT_SYNTAX/); | ||
| 143 | + doesNotMatch(result.stderr, /ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX/); | ||
| 144 | 144 | strictEqual(result.code, 1); | |
| 145 | 145 | }); | |
| 146 | 146 | ||
@@ -152,7 +152,7 @@ test('should not parse TypeScript with --type-module=module', async () => { | |||
| 152 | 152 | ||
| 153 | 153 | strictEqual(result.stdout, ''); | |
| 154 | 154 | match(result.stderr, /SyntaxError/); | |
| 155 | - doesNotMatch(result.stderr, /ERR_INVALID_TYPESCRIPT_SYNTAX/); | ||
| 155 | + doesNotMatch(result.stderr, /ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX/); | ||
| 156 | 156 | strictEqual(result.code, 1); | |
| 157 | 157 | }); | |
| 158 | 158 | ||
@@ -222,3 +222,23 @@ test('typescript CJS code is throwing a syntax error at runtime', async () => { | |||
| 222 | 222 | strictEqual(result.stdout, ''); | |
| 223 | 223 | strictEqual(result.code, 1); | |
| 224 | 224 | }); | |
| 225 | + | ||
| 226 | + test('check syntax error is thrown when passing invalid syntax with --input-type=commonjs-typescript', async () => { | ||
| 227 | + const result = await spawnPromisified(process.execPath, [ | ||
| 228 | + '--input-type=commonjs-typescript', | ||
| 229 | + '--eval', | ||
| 230 | + 'function foo(){ await Promise.resolve(1); }']); | ||
| 231 | + strictEqual(result.stdout, ''); | ||
| 232 | + match(result.stderr, /ERR_INVALID_TYPESCRIPT_SYNTAX/); | ||
| 233 | + strictEqual(result.code, 1); | ||
| 234 | + }); | ||
| 235 | + | ||
| 236 | + test('check syntax error is thrown when passing invalid syntax with --input-type=module-typescript', async () => { | ||
| 237 | + const result = await spawnPromisified(process.execPath, [ | ||
| 238 | + '--input-type=module-typescript', | ||
| 239 | + '--eval', | ||
| 240 | + 'function foo(){ await Promise.resolve(1); }']); | ||
| 241 | + strictEqual(result.stdout, ''); | ||
| 242 | + match(result.stderr, /ERR_INVALID_TYPESCRIPT_SYNTAX/); | ||
| 243 | + strictEqual(result.code, 1); | ||
| 244 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -321,3 +321,13 @@ test('execute a TypeScript loader and a .js file', async () => { | |||
| 321 | 321 | match(result.stdout, /Hello, TypeScript!/); | |
| 322 | 322 | strictEqual(result.code, 0); | |
| 323 | 323 | }); | |
| 324 | + | ||
| 325 | + test('execute invalid TypeScript syntax', async () => { | ||
| 326 | + const result = await spawnPromisified(process.execPath, [ | ||
| 327 | + fixtures.path('typescript/ts/test-invalid-syntax.ts'), | ||
| 328 | + ]); | ||
| 329 | + | ||
| 330 | + match(result.stderr, /ERR_INVALID_TYPESCRIPT_SYNTAX/); | ||
| 331 | + strictEqual(result.stdout, ''); | ||
| 332 | + strictEqual(result.code, 1); | ||
| 333 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + function foo(): string { | ||
| 2 | + await Promise.resolve(1); | ||
| 3 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments