| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5ea8aa1 commit 6fcac73
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2107,6 +2107,18 @@ An element in the `iterable` provided to the [WHATWG][WHATWG URL API] | |||
| 2107 | 2107 | represent a `[name, value]` tuple – that is, if an element is not iterable, or | |
| 2108 | 2108 | does not consist of exactly two elements. | |
| 2109 | 2109 | ||
| 2110 | + <a id="ERR_INVALID_TYPESCRIPT_SYNTAX"></a> | ||
| 2111 | + | ||
| 2112 | + ### `ERR_INVALID_TYPESCRIPT_SYNTAX` | ||
| 2113 | + | ||
| 2114 | + <!-- YAML | ||
| 2115 | + added: REPLACEME | ||
| 2116 | + --> | ||
| 2117 | + | ||
| 2118 | + The provided TypeScript syntax is not valid or unsupported. | ||
| 2119 | + This could happen when using TypeScript syntax that requires | ||
| 2120 | + transformation with [type-stripping][]. | ||
| 2121 | + | ||
| 2110 | 2122 | <a id="ERR_INVALID_URI"></a> | |
| 2111 | 2123 | ||
| 2112 | 2124 | ### `ERR_INVALID_URI` | |
@@ -4177,4 +4189,5 @@ An error occurred trying to allocate memory. This should never happen. | |||
| 4177 | 4189 | [stream-based]: stream.md | |
| 4178 | 4190 | [syscall]: https://man7.org/linux/man-pages/man2/syscalls.2.html | |
| 4179 | 4191 | [try-catch]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch | |
| 4192 | + [type-stripping]: typescript.md#type-stripping | ||
| 4180 | 4193 | [vm]: vm.md | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1528,6 +1528,7 @@ E('ERR_INVALID_SYNC_FORK_INPUT', | |||
| 1528 | 1528 | TypeError); | |
| 1529 | 1529 | E('ERR_INVALID_THIS', 'Value of "this" must be of type %s', TypeError); | |
| 1530 | 1530 | E('ERR_INVALID_TUPLE', '%s must be an iterable %s tuple', TypeError); | |
| 1531 | + E('ERR_INVALID_TYPESCRIPT_SYNTAX', '%s', SyntaxError); | ||
| 1531 | 1532 | E('ERR_INVALID_URI', 'URI malformed', URIError); | |
| 1532 | 1533 | E('ERR_INVALID_URL', function(input, base = null) { | |
| 1533 | 1534 | this.input = input; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,6 +17,7 @@ const { | |||
| 17 | 17 | const { | |
| 18 | 18 | ERR_INVALID_ARG_TYPE, | |
| 19 | 19 | ERR_INVALID_RETURN_PROPERTY_VALUE, | |
| 20 | + ERR_INVALID_TYPESCRIPT_SYNTAX, | ||
| 20 | 21 | } = require('internal/errors').codes; | |
| 21 | 22 | const { BuiltinModule } = require('internal/bootstrap/realm'); | |
| 22 | 23 | ||
@@ -315,44 +316,37 @@ function getBuiltinModule(id) { | |||
| 315 | 316 | return normalizedId ? require(normalizedId) : undefined; | |
| 316 | 317 | } | |
| 317 | 318 | ||
| 318 | - /** | ||
| 319 | - * TypeScript parsing function, by default Amaro.transformSync. | ||
| 320 | - * @type {Function} | ||
| 321 | - */ | ||
| 322 | - let typeScriptParser; | ||
| 323 | 319 | /** | |
| 324 | 320 | * The TypeScript parsing mode, either 'strip-only' or 'transform'. | |
| 325 | 321 | * @type {string} | |
| 326 | 322 | */ | |
| 327 | - let typeScriptParsingMode; | ||
| 328 | - /** | ||
| 329 | - * Whether source maps are enabled for TypeScript parsing. | ||
| 330 | - * @type {boolean} | ||
| 331 | - */ | ||
| 332 | - let sourceMapEnabled; | ||
| 323 | + const getTypeScriptParsingMode = getLazy(() => | ||
| 324 | + (getOptionValue('--experimental-transform-types') ? 'transform' : 'strip-only'), | ||
| 325 | + ); | ||
| 333 | 326 | ||
| 334 | 327 | /** | |
| 335 | 328 | * Load the TypeScript parser. | |
| 336 | - * @param {Function} parser - A function that takes a string of TypeScript code | ||
| 337 | 329 | * and returns an object with a `code` property. | |
| 338 | 330 | * @returns {Function} The TypeScript parser function. | |
| 339 | 331 | */ | |
| 340 | - function loadTypeScriptParser(parser) { | ||
| 341 | - if (typeScriptParser) { | ||
| 342 | - return typeScriptParser; | ||
| 343 | - } | ||
| 332 | + const loadTypeScriptParser = getLazy(() => { | ||
| 333 | + const amaro = require('internal/deps/amaro/dist/index'); | ||
| 334 | + return amaro.transformSync; | ||
| 335 | + }); | ||
| 344 | 336 | ||
| 345 | - if (parser) { | ||
| 346 | - typeScriptParser = parser; | ||
| 347 | - } else { | ||
| 348 | - const amaro = require('internal/deps/amaro/dist/index'); | ||
| 349 | - // Default option for Amaro is to perform Type Stripping only. | ||
| 350 | - typeScriptParsingMode = getOptionValue('--experimental-transform-types') ? 'transform' : 'strip-only'; | ||
| 351 | - sourceMapEnabled = getOptionValue('--enable-source-maps'); | ||
| 352 | - // Curry the transformSync function with the default options. | ||
| 353 | - typeScriptParser = amaro.transformSync; | ||
| 337 | + /** | ||
| 338 | + * | ||
| 339 | + * @param {string} source the source code | ||
| 340 | + * @param {object} options the options to pass to the parser | ||
| 341 | + * @returns {TransformOutput} an object with a `code` property. | ||
| 342 | + */ | ||
| 343 | + function parseTypeScript(source, options) { | ||
| 344 | + const parse = loadTypeScriptParser(); | ||
| 345 | + try { | ||
| 346 | + return parse(source, options); | ||
| 347 | + } catch (error) { | ||
| 348 | + throw new ERR_INVALID_TYPESCRIPT_SYNTAX(error); | ||
| 354 | 349 | } | |
| 355 | - return typeScriptParser; | ||
| 356 | 350 | } | |
| 357 | 351 | ||
| 358 | 352 | /** | |
@@ -367,14 +361,13 @@ function loadTypeScriptParser(parser) { | |||
| 367 | 361 | */ | |
| 368 | 362 | function stripTypeScriptTypes(source, filename) { | |
| 369 | 363 | assert(typeof source === 'string'); | |
| 370 | - const parse = loadTypeScriptParser(); | ||
| 371 | 364 | const options = { | |
| 372 | 365 | __proto__: null, | |
| 373 | - mode: typeScriptParsingMode, | ||
| 374 | - sourceMap: sourceMapEnabled, | ||
| 366 | + mode: getTypeScriptParsingMode(), | ||
| 367 | + sourceMap: getOptionValue('--enable-source-maps'), | ||
| 375 | 368 | filename, | |
| 376 | 369 | }; | |
| 377 | - const { code, map } = parse(source, options); | ||
| 370 | + const { code, map } = parseTypeScript(source, options); | ||
| 378 | 371 | if (map) { | |
| 379 | 372 | // TODO(@marco-ippolito) When Buffer.transcode supports utf8 to | |
| 380 | 373 | // base64 transformation, we should change this line. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -110,3 +110,13 @@ test('expect fail eval TypeScript ESM syntax with input-type commonjs', async () | |||
| 110 | 110 | match(result.stderr, /Cannot use import statement outside a module/); | |
| 111 | 111 | strictEqual(result.code, 1); | |
| 112 | 112 | }); | |
| 113 | + | ||
| 114 | + test('check syntax error is thrown when passing invalid syntax', async () => { | ||
| 115 | + const result = await spawnPromisified(process.execPath, [ | ||
| 116 | + '--experimental-strip-types', | ||
| 117 | + '--eval', | ||
| 118 | + 'enum Foo { A, B, C }']); | ||
| 119 | + strictEqual(result.stdout, ''); | ||
| 120 | + match(result.stderr, /ERR_INVALID_TYPESCRIPT_SYNTAX/); | ||
| 121 | + strictEqual(result.code, 1); | ||
| 122 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments