Skip to content

Commit 9f99a6a

Browse files
committed
module: use more defensive code when handling SWC errors
PR-URL: #56646 Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 538e194 commit 9f99a6a

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/internal/modules/typescript.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
22

3+
const {
4+
ObjectPrototypeHasOwnProperty,
5+
} = primordials;
36
const {
47
validateBoolean,
58
validateOneOf,
@@ -12,7 +15,6 @@ const { assertTypeScript,
1215
isUnderNodeModules,
1316
kEmptyObject } = require('internal/util');
1417
const {
15-
ERR_INTERNAL_ASSERTION,
1618
ERR_INVALID_TYPESCRIPT_SYNTAX,
1719
ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING,
1820
ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX,
@@ -55,15 +57,16 @@ function parseTypeScript(source, options) {
5557
* Amaro v0.3.0 (from SWC v1.10.7) throws an object with `message` and `code` properties.
5658
* It allows us to distinguish between invalid syntax and unsupported syntax.
5759
*/
58-
switch (error.code) {
60+
switch (error?.code) {
5961
case 'UnsupportedSyntax':
6062
throw new ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX(error.message);
6163
case 'InvalidSyntax':
6264
throw new ERR_INVALID_TYPESCRIPT_SYNTAX(error.message);
6365
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);
66+
// SWC may throw strings when something goes wrong.
67+
if (typeof error === 'string') { assert.fail(error); }
68+
assert(error != null && ObjectPrototypeHasOwnProperty(error, 'message'));
69+
assert.fail(error.message);
6770
}
6871
}
6972
}

0 commit comments

Comments
 (0)