Skip to content

Commit 062986d

Browse files
module: wrap swc error in ERR_INVALID_TYPESCRIPT_SYNTAX
1 parent 20d8b85 commit 062986d

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
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`
@@ -4197,4 +4209,5 @@ Type stripping is not supported for files descendent of a `node_modules` directo
41974209
[stream-based]: stream.md
41984210
[syscall]: https://man7.org/linux/man-pages/man2/syscalls.2.html
41994211
[try-catch]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
4212+
[type-stripping]: typescript.md#type-stripping
42004213
[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

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

@@ -372,7 +373,7 @@ function stripTypeScriptTypes(source, filename) {
372373
sourceMap: sourceMapEnabled,
373374
filename,
374375
};
375-
const { code, map } = parse(source, options);
376+
const { code, map } = tryParse(() => parse(source, options));
376377
if (map) {
377378
// TODO(@marco-ippolito) When Buffer.transcode supports utf8 to
378379
// base64 transformation, we should change this line.
@@ -385,6 +386,14 @@ function stripTypeScriptTypes(source, filename) {
385386
return `${code}\n\n//# sourceURL=${filename}`;
386387
}
387388

389+
function tryParse(parse) {
390+
try {
391+
return parse();
392+
} catch (error) {
393+
throw new ERR_INVALID_TYPESCRIPT_SYNTAX(error);
394+
}
395+
}
396+
388397
/** @type {import('internal/util/types')} */
389398
let _TYPES = null;
390399
/**

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)