module: simplify ts under node_modules check

PR-URL: https://github.com/nodejs/node/pull/55440
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
Marco Ippolito 2024-10-20 11:09:56 +02:00 committed by GitHub
parent 78b72ca7ba
commit b0ffe9ed35
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 5 additions and 20 deletions

View File

@ -124,7 +124,6 @@ const { pathToFileURL, fileURLToPath, isURL } = require('internal/url');
const { const {
pendingDeprecate, pendingDeprecate,
emitExperimentalWarning, emitExperimentalWarning,
isUnderNodeModules,
kEmptyObject, kEmptyObject,
setOwnProperty, setOwnProperty,
getLazy, getLazy,
@ -170,7 +169,6 @@ const {
ERR_REQUIRE_CYCLE_MODULE, ERR_REQUIRE_CYCLE_MODULE,
ERR_REQUIRE_ESM, ERR_REQUIRE_ESM,
ERR_UNKNOWN_BUILTIN_MODULE, ERR_UNKNOWN_BUILTIN_MODULE,
ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING,
}, },
setArrowMessage, setArrowMessage,
} = require('internal/errors'); } = require('internal/errors');
@ -1370,9 +1368,6 @@ let hasPausedEntry = false;
function loadESMFromCJS(mod, filename) { function loadESMFromCJS(mod, filename) {
let source = getMaybeCachedSource(mod, filename); let source = getMaybeCachedSource(mod, filename);
if (getOptionValue('--experimental-strip-types') && path.extname(filename) === '.mts') { if (getOptionValue('--experimental-strip-types') && path.extname(filename) === '.mts') {
if (isUnderNodeModules(filename)) {
throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(filename);
}
source = stripTypeScriptTypes(source, filename); source = stripTypeScriptTypes(source, filename);
} }
const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader(); const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader();
@ -1594,9 +1589,6 @@ function getMaybeCachedSource(mod, filename) {
} }
function loadCTS(module, filename) { function loadCTS(module, filename) {
if (isUnderNodeModules(filename)) {
throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(filename);
}
const source = getMaybeCachedSource(module, filename); const source = getMaybeCachedSource(module, filename);
const code = stripTypeScriptTypes(source, filename); const code = stripTypeScriptTypes(source, filename);
module._compile(code, filename, 'commonjs'); module._compile(code, filename, 'commonjs');
@ -1608,9 +1600,6 @@ function loadCTS(module, filename) {
* @param {string} filename The file path of the module * @param {string} filename The file path of the module
*/ */
function loadTS(module, filename) { function loadTS(module, filename) {
if (isUnderNodeModules(filename)) {
throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(filename);
}
// If already analyzed the source, then it will be cached. // If already analyzed the source, then it will be cached.
const source = getMaybeCachedSource(module, filename); const source = getMaybeCachedSource(module, filename);
const content = stripTypeScriptTypes(source, filename); const content = stripTypeScriptTypes(source, filename);

View File

@ -4,7 +4,6 @@ const {
RegExpPrototypeExec, RegExpPrototypeExec,
} = primordials; } = primordials;
const { const {
isUnderNodeModules,
kEmptyObject, kEmptyObject,
} = require('internal/util'); } = require('internal/util');
@ -23,7 +22,6 @@ const {
ERR_INVALID_URL, ERR_INVALID_URL,
ERR_UNKNOWN_MODULE_FORMAT, ERR_UNKNOWN_MODULE_FORMAT,
ERR_UNSUPPORTED_ESM_URL_SCHEME, ERR_UNSUPPORTED_ESM_URL_SCHEME,
ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING,
} = require('internal/errors').codes; } = require('internal/errors').codes;
const { const {
@ -131,12 +129,6 @@ async function defaultLoad(url, context = kEmptyObject) {
validateAttributes(url, format, importAttributes); validateAttributes(url, format, importAttributes);
if (getOptionValue('--experimental-strip-types') &&
(format === 'module-typescript' || format === 'commonjs-typescript') &&
isUnderNodeModules(url)) {
throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(url);
}
return { return {
__proto__: null, __proto__: null,
format, format,

View File

@ -16,6 +16,7 @@ const {
ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_TYPE,
ERR_INVALID_RETURN_PROPERTY_VALUE, ERR_INVALID_RETURN_PROPERTY_VALUE,
ERR_INVALID_TYPESCRIPT_SYNTAX, ERR_INVALID_TYPESCRIPT_SYNTAX,
ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING,
} = require('internal/errors').codes; } = require('internal/errors').codes;
const { BuiltinModule } = require('internal/bootstrap/realm'); const { BuiltinModule } = require('internal/bootstrap/realm');
@ -28,7 +29,7 @@ const assert = require('internal/assert');
const { Buffer } = require('buffer'); const { Buffer } = require('buffer');
const { getOptionValue } = require('internal/options'); const { getOptionValue } = require('internal/options');
const { assertTypeScript, setOwnProperty, getLazy } = require('internal/util'); const { assertTypeScript, setOwnProperty, getLazy, isUnderNodeModules } = require('internal/util');
const { inspect } = require('internal/util/inspect'); const { inspect } = require('internal/util/inspect');
const lazyTmpdir = getLazy(() => require('os').tmpdir()); const lazyTmpdir = getLazy(() => require('os').tmpdir());
@ -358,6 +359,9 @@ function parseTypeScript(source, options) {
* @returns {TransformOutput} The stripped TypeScript code. * @returns {TransformOutput} The stripped TypeScript code.
*/ */
function stripTypeScriptTypes(source, filename) { function stripTypeScriptTypes(source, filename) {
if (isUnderNodeModules(filename)) {
throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(filename);
}
assert(typeof source === 'string'); assert(typeof source === 'string');
const options = { const options = {
__proto__: null, __proto__: null,