Skip to content

Commit 905e4b4

Browse files
authored
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 d978610 commit 905e4b4

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/internal/modules/typescript.js

Lines changed: 8 additions & 5 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy