Skip to content

Commit 31a37e7

Browse files
authoredOct 10, 2024
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 e79ae1b commit 31a37e7

File tree

4 files changed

+47
-30
lines changed

4 files changed

+47
-30
lines changed
 

‎doc/api/errors.md

+13
Original file line numberDiff line numberDiff line change
@@ -2094,6 +2094,18 @@ An element in the `iterable` provided to the [WHATWG][WHATWG URL API]
20942094
represent a `[name, value]` tuple – that is, if an element is not iterable, or
20952095
does not consist of exactly two elements.
20962096

2097+
<a id="ERR_INVALID_TYPESCRIPT_SYNTAX"></a>
2098+
2099+
### `ERR_INVALID_TYPESCRIPT_SYNTAX`
2100+
2101+
<!-- YAML
2102+
added: REPLACEME
2103+
-->
2104+
2105+
The provided TypeScript syntax is not valid or unsupported.
2106+
This could happen when using TypeScript syntax that requires
2107+
transformation with [type-stripping][].
2108+
20972109
<a id="ERR_INVALID_URI"></a>
20982110

20992111
### `ERR_INVALID_URI`
@@ -4203,4 +4215,5 @@ An error occurred trying to allocate memory. This should never happen.
42034215
[stream-based]: stream.md
42044216
[syscall]: https://man7.org/linux/man-pages/man2/syscalls.2.html
42054217
[try-catch]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
4218+
[type-stripping]: typescript.md#type-stripping
42064219
[vm]: vm.md

‎lib/internal/errors.js

+1
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,7 @@ E('ERR_INVALID_SYNC_FORK_INPUT',
15241524
TypeError);
15251525
E('ERR_INVALID_THIS', 'Value of "this" must be of type %s', TypeError);
15261526
E('ERR_INVALID_TUPLE', '%s must be an iterable %s tuple', TypeError);
1527+
E('ERR_INVALID_TYPESCRIPT_SYNTAX', '%s', SyntaxError);
15271528
E('ERR_INVALID_URI', 'URI malformed', URIError);
15281529
E('ERR_INVALID_URL', function(input, base = null) {
15291530
this.input = input;

‎lib/internal/modules/helpers.js

+23-30
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const {
1515
const {
1616
ERR_INVALID_ARG_TYPE,
1717
ERR_INVALID_RETURN_PROPERTY_VALUE,
18+
ERR_INVALID_TYPESCRIPT_SYNTAX,
1819
} = require('internal/errors').codes;
1920
const { BuiltinModule } = require('internal/bootstrap/realm');
2021

@@ -312,44 +313,37 @@ function getBuiltinModule(id) {
312313
return normalizedId ? require(normalizedId) : undefined;
313314
}
314315

315-
/**
316-
* TypeScript parsing function, by default Amaro.transformSync.
317-
* @type {Function}
318-
*/
319-
let typeScriptParser;
320316
/**
321317
* The TypeScript parsing mode, either 'strip-only' or 'transform'.
322318
* @type {string}
323319
*/
324-
let typeScriptParsingMode;
325-
/**
326-
* Whether source maps are enabled for TypeScript parsing.
327-
* @type {boolean}
328-
*/
329-
let sourceMapEnabled;
320+
const getTypeScriptParsingMode = getLazy(() =>
321+
(getOptionValue('--experimental-transform-types') ? 'transform' : 'strip-only'),
322+
);
330323

331324
/**
332325
* Load the TypeScript parser.
333-
* @param {Function} parser - A function that takes a string of TypeScript code
334326
* and returns an object with a `code` property.
335327
* @returns {Function} The TypeScript parser function.
336328
*/
337-
function loadTypeScriptParser(parser) {
338-
if (typeScriptParser) {
339-
return typeScriptParser;
340-
}
329+
const loadTypeScriptParser = getLazy(() => {
330+
const amaro = require('internal/deps/amaro/dist/index');
331+
return amaro.transformSync;
332+
});
341333

342-
if (parser) {
343-
typeScriptParser = parser;
344-
} else {
345-
const amaro = require('internal/deps/amaro/dist/index');
346-
// Default option for Amaro is to perform Type Stripping only.
347-
typeScriptParsingMode = getOptionValue('--experimental-transform-types') ? 'transform' : 'strip-only';
348-
sourceMapEnabled = getOptionValue('--enable-source-maps');
349-
// Curry the transformSync function with the default options.
350-
typeScriptParser = amaro.transformSync;
334+
/**
335+
*
336+
* @param {string} source the source code
337+
* @param {object} options the options to pass to the parser
338+
* @returns {TransformOutput} an object with a `code` property.
339+
*/
340+
function parseTypeScript(source, options) {
341+
const parse = loadTypeScriptParser();
342+
try {
343+
return parse(source, options);
344+
} catch (error) {
345+
throw new ERR_INVALID_TYPESCRIPT_SYNTAX(error);
351346
}
352-
return typeScriptParser;
353347
}
354348

355349
/**
@@ -364,14 +358,13 @@ function loadTypeScriptParser(parser) {
364358
*/
365359
function stripTypeScriptTypes(source, filename) {
366360
assert(typeof source === 'string');
367-
const parse = loadTypeScriptParser();
368361
const options = {
369362
__proto__: null,
370-
mode: typeScriptParsingMode,
371-
sourceMap: sourceMapEnabled,
363+
mode: getTypeScriptParsingMode(),
364+
sourceMap: getOptionValue('--enable-source-maps'),
372365
filename,
373366
};
374-
const { code, map } = parse(source, options);
367+
const { code, map } = parseTypeScript(source, options);
375368
if (map) {
376369
// TODO(@marco-ippolito) When Buffer.transcode supports utf8 to
377370
// base64 transformation, we should change this line.

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

+10
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)