Skip to content

Commit 0794861

Browse files
marco-ippolitoruyadorno
authored andcommitted
module: simplify ts under node_modules check
PR-URL: #55440 Backport-PR-URL: #56208 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent bcfe9c8 commit 0794861

File tree

3 files changed

+6
-20
lines changed

3 files changed

+6
-20
lines changed

lib/internal/modules/cjs/loader.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ const { pathToFileURL, fileURLToPath, isURL } = require('internal/url');
125125
const {
126126
pendingDeprecate,
127127
emitExperimentalWarning,
128-
isUnderNodeModules,
129128
kEmptyObject,
130129
setOwnProperty,
131130
getLazy,
131+
isUnderNodeModules,
132132
isWindows,
133133
} = require('internal/util');
134134
const {
@@ -170,7 +170,6 @@ const {
170170
ERR_REQUIRE_CYCLE_MODULE,
171171
ERR_REQUIRE_ESM,
172172
ERR_UNKNOWN_BUILTIN_MODULE,
173-
ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING,
174173
},
175174
setArrowMessage,
176175
} = require('internal/errors');
@@ -1348,9 +1347,6 @@ let emittedRequireModuleWarning = false;
13481347
function loadESMFromCJS(mod, filename) {
13491348
let source = getMaybeCachedSource(mod, filename);
13501349
if (getOptionValue('--experimental-strip-types') && path.extname(filename) === '.mts') {
1351-
if (isUnderNodeModules(filename)) {
1352-
throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(filename);
1353-
}
13541350
source = stripTypeScriptTypes(source, filename);
13551351
}
13561352
const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader();
@@ -1587,9 +1583,6 @@ function getMaybeCachedSource(mod, filename) {
15871583
}
15881584

15891585
function loadCTS(module, filename) {
1590-
if (isUnderNodeModules(filename)) {
1591-
throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(filename);
1592-
}
15931586
const source = getMaybeCachedSource(module, filename);
15941587
const code = stripTypeScriptTypes(source, filename);
15951588
module._compile(code, filename, 'commonjs');
@@ -1601,9 +1594,6 @@ function loadCTS(module, filename) {
16011594
* @param {string} filename The file path of the module
16021595
*/
16031596
function loadTS(module, filename) {
1604-
if (isUnderNodeModules(filename)) {
1605-
throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(filename);
1606-
}
16071597
// If already analyzed the source, then it will be cached.
16081598
const source = getMaybeCachedSource(module, filename);
16091599
const content = stripTypeScriptTypes(source, filename);

lib/internal/modules/esm/load.js

-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const {
44
RegExpPrototypeExec,
55
} = primordials;
66
const {
7-
isUnderNodeModules,
87
kEmptyObject,
98
} = require('internal/util');
109

@@ -23,7 +22,6 @@ const {
2322
ERR_INVALID_URL,
2423
ERR_UNKNOWN_MODULE_FORMAT,
2524
ERR_UNSUPPORTED_ESM_URL_SCHEME,
26-
ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING,
2725
} = require('internal/errors').codes;
2826

2927
const {
@@ -131,12 +129,6 @@ async function defaultLoad(url, context = kEmptyObject) {
131129

132130
validateAttributes(url, format, importAttributes);
133131

134-
if (getOptionValue('--experimental-strip-types') &&
135-
(format === 'module-typescript' || format === 'commonjs-typescript') &&
136-
isUnderNodeModules(url)) {
137-
throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(url);
138-
}
139-
140132
return {
141133
__proto__: null,
142134
format,

lib/internal/modules/helpers.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const {
1616
ERR_INVALID_ARG_TYPE,
1717
ERR_INVALID_RETURN_PROPERTY_VALUE,
1818
ERR_INVALID_TYPESCRIPT_SYNTAX,
19+
ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING,
1920
} = require('internal/errors').codes;
2021
const { BuiltinModule } = require('internal/bootstrap/realm');
2122

@@ -28,7 +29,7 @@ const assert = require('internal/assert');
2829

2930
const { Buffer } = require('buffer');
3031
const { getOptionValue } = require('internal/options');
31-
const { assertTypeScript, setOwnProperty, getLazy } = require('internal/util');
32+
const { assertTypeScript, setOwnProperty, getLazy, isUnderNodeModules } = require('internal/util');
3233
const { inspect } = require('internal/util/inspect');
3334

3435
const lazyTmpdir = getLazy(() => require('os').tmpdir());
@@ -358,6 +359,9 @@ function parseTypeScript(source, options) {
358359
* @returns {TransformOutput} The stripped TypeScript code.
359360
*/
360361
function stripTypeScriptTypes(source, filename) {
362+
if (isUnderNodeModules(filename)) {
363+
throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(filename);
364+
}
361365
assert(typeof source === 'string');
362366
const options = {
363367
__proto__: null,

0 commit comments

Comments
 (0)