Skip to content

Commit

Permalink
module: use more defensive code when handling SWC errors
Browse files Browse the repository at this point in the history
PR-URL: #56646
Reviewed-By: Marco Ippolito <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
aduh95 authored Jan 23, 2025
1 parent d978610 commit 905e4b4
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/internal/modules/typescript.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict';

const {
ObjectPrototypeHasOwnProperty,
} = primordials;
const {
validateBoolean,
validateOneOf,
Expand All @@ -12,7 +15,6 @@ const { assertTypeScript,
isUnderNodeModules,
kEmptyObject } = require('internal/util');
const {
ERR_INTERNAL_ASSERTION,
ERR_INVALID_TYPESCRIPT_SYNTAX,
ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING,
ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX,
Expand Down Expand Up @@ -55,15 +57,16 @@ function parseTypeScript(source, options) {
* Amaro v0.3.0 (from SWC v1.10.7) throws an object with `message` and `code` properties.
* It allows us to distinguish between invalid syntax and unsupported syntax.
*/
switch (error.code) {
switch (error?.code) {
case 'UnsupportedSyntax':
throw new ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX(error.message);
case 'InvalidSyntax':
throw new ERR_INVALID_TYPESCRIPT_SYNTAX(error.message);
default:
// SWC will throw strings when something goes wrong.
// Check if has the `message` property or treat it as a string.
throw new ERR_INTERNAL_ASSERTION(error.message ?? error);
// SWC may throw strings when something goes wrong.
if (typeof error === 'string') { assert.fail(error); }
assert(error != null && ObjectPrototypeHasOwnProperty(error, 'message'));
assert.fail(error.message);
}
}
}
Expand Down

0 comments on commit 905e4b4

Please sign in to comment.