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

[v22.x backport] typescript syntax detection #56912

Merged
merged 8 commits into from
Feb 6, 2025
Prev Previous commit
module: use more defensive code when handling SWC errors
PR-URL: #56646
Backport-PR-URL: #56912
Reviewed-By: Marco Ippolito <[email protected]>
Reviewed-By: James M Snell <[email protected]>
aduh95 committed Feb 6, 2025

Verified

This commit was signed with the committer’s verified signature.
aduh95 Antoine du Hamel
commit bae4b2e20a00b31730d8c066d885882594a685d1
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,
@@ -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);
}
}
}

Unchanged files with check annotations Beta

added: v22.10.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/56610

Check warning on line 2111 in doc/api/errors.md

GitHub Actions / lint-pr-url

pr-url doesn't match the URL of the current PR.
description: This error is no longer thrown on valid yet unsupported syntax.
-->