Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: use more defensive code when handling SWC errors #56646

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading