Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6fcac73

Browse filesBrowse files
marco-ippolitoaduh95
authored andcommitted
module: wrap swc error in ERR_INVALID_TYPESCRIPT_SYNTAX
PR-URL: #55316 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 5ea8aa1 commit 6fcac73
Copy full SHA for 6fcac73

File tree

Expand file treeCollapse file tree

4 files changed

+47
-30
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+47
-30
lines changed
Open diff view settings
Collapse file

‎doc/api/errors.md‎

Copy file name to clipboardExpand all lines: doc/api/errors.md
+13Lines changed: 13 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -2107,6 +2107,18 @@ An element in the `iterable` provided to the [WHATWG][WHATWG URL API]
21072107
represent a `[name, value]` tuple – that is, if an element is not iterable, or
21082108
does not consist of exactly two elements.
21092109

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+
21102122
<a id="ERR_INVALID_URI"></a>
21112123

21122124
### `ERR_INVALID_URI`
@@ -4177,4 +4189,5 @@ An error occurred trying to allocate memory. This should never happen.
41774189
[stream-based]: stream.md
41784190
[syscall]: https://man7.org/linux/man-pages/man2/syscalls.2.html
41794191
[try-catch]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
4192+
[type-stripping]: typescript.md#type-stripping
41804193
[vm]: vm.md
Collapse file

‎lib/internal/errors.js‎

Copy file name to clipboardExpand all lines: lib/internal/errors.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,7 @@ E('ERR_INVALID_SYNC_FORK_INPUT',
15281528
TypeError);
15291529
E('ERR_INVALID_THIS', 'Value of "this" must be of type %s', TypeError);
15301530
E('ERR_INVALID_TUPLE', '%s must be an iterable %s tuple', TypeError);
1531+
E('ERR_INVALID_TYPESCRIPT_SYNTAX', '%s', SyntaxError);
15311532
E('ERR_INVALID_URI', 'URI malformed', URIError);
15321533
E('ERR_INVALID_URL', function(input, base = null) {
15331534
this.input = input;
Collapse file

‎lib/internal/modules/helpers.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/helpers.js
+23-30Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const {
1717
const {
1818
ERR_INVALID_ARG_TYPE,
1919
ERR_INVALID_RETURN_PROPERTY_VALUE,
20+
ERR_INVALID_TYPESCRIPT_SYNTAX,
2021
} = require('internal/errors').codes;
2122
const { BuiltinModule } = require('internal/bootstrap/realm');
2223

@@ -315,44 +316,37 @@ function getBuiltinModule(id) {
315316
return normalizedId ? require(normalizedId) : undefined;
316317
}
317318

318-
/**
319-
* TypeScript parsing function, by default Amaro.transformSync.
320-
* @type {Function}
321-
*/
322-
let typeScriptParser;
323319
/**
324320
* The TypeScript parsing mode, either 'strip-only' or 'transform'.
325321
* @type {string}
326322
*/
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+
);
333326

334327
/**
335328
* Load the TypeScript parser.
336-
* @param {Function} parser - A function that takes a string of TypeScript code
337329
* and returns an object with a `code` property.
338330
* @returns {Function} The TypeScript parser function.
339331
*/
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+
});
344336

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);
354349
}
355-
return typeScriptParser;
356350
}
357351

358352
/**
@@ -367,14 +361,13 @@ function loadTypeScriptParser(parser) {
367361
*/
368362
function stripTypeScriptTypes(source, filename) {
369363
assert(typeof source === 'string');
370-
const parse = loadTypeScriptParser();
371364
const options = {
372365
__proto__: null,
373-
mode: typeScriptParsingMode,
374-
sourceMap: sourceMapEnabled,
366+
mode: getTypeScriptParsingMode(),
367+
sourceMap: getOptionValue('--enable-source-maps'),
375368
filename,
376369
};
377-
const { code, map } = parse(source, options);
370+
const { code, map } = parseTypeScript(source, options);
378371
if (map) {
379372
// TODO(@marco-ippolito) When Buffer.transcode supports utf8 to
380373
// base64 transformation, we should change this line.
Collapse file

‎test/es-module/test-typescript-eval.mjs‎

Copy file name to clipboardExpand all lines: test/es-module/test-typescript-eval.mjs
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,13 @@ test('expect fail eval TypeScript ESM syntax with input-type commonjs', async ()
110110
match(result.stderr, /Cannot use import statement outside a module/);
111111
strictEqual(result.code, 1);
112112
});
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+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.