diff --git a/lib/internal/modules/typescript.js b/lib/internal/modules/typescript.js index 689788b09853c4..6abfc707657b92 100644 --- a/lib/internal/modules/typescript.js +++ b/lib/internal/modules/typescript.js @@ -1,5 +1,8 @@ 'use strict'; +const { + ObjectPrototypeHasOwnProperty, +} = primordials; const { validateBoolean, validateOneOf, @@ -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, @@ -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); } } }