Skip to content

Commit a12dbf0

Browse files
marco-ippolitoaduh95
authored andcommitted
module: simplify ts under node_modules check
PR-URL: #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>
1 parent 52da293 commit a12dbf0

File tree

3 files changed

+5
-20
lines changed

3 files changed

+5
-20
lines changed

lib/internal/modules/cjs/loader.js

-11
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ const { pathToFileURL, fileURLToPath, isURL } = require('internal/url');
124124
const {
125125
pendingDeprecate,
126126
emitExperimentalWarning,
127-
isUnderNodeModules,
128127
kEmptyObject,
129128
setOwnProperty,
130129
getLazy,
@@ -170,7 +169,6 @@ const {
170169
ERR_REQUIRE_CYCLE_MODULE,
171170
ERR_REQUIRE_ESM,
172171
ERR_UNKNOWN_BUILTIN_MODULE,
173-
ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING,
174172
},
175173
setArrowMessage,
176174
} = require('internal/errors');
@@ -1370,9 +1368,6 @@ let hasPausedEntry = false;
13701368
function loadESMFromCJS(mod, filename) {
13711369
let source = getMaybeCachedSource(mod, filename);
13721370
if (getOptionValue('--experimental-strip-types') && path.extname(filename) === '.mts') {
1373-
if (isUnderNodeModules(filename)) {
1374-
throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(filename);
1375-
}
13761371
source = stripTypeScriptTypes(source, filename);
13771372
}
13781373
const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader();
@@ -1594,9 +1589,6 @@ function getMaybeCachedSource(mod, filename) {
15941589
}
15951590

15961591
function loadCTS(module, filename) {
1597-
if (isUnderNodeModules(filename)) {
1598-
throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(filename);
1599-
}
16001592
const source = getMaybeCachedSource(module, filename);
16011593
const code = stripTypeScriptTypes(source, filename);
16021594
module._compile(code, filename, 'commonjs');
@@ -1608,9 +1600,6 @@ function loadCTS(module, filename) {
16081600
* @param {string} filename The file path of the module
16091601
*/
16101602
function loadTS(module, filename) {
1611-
if (isUnderNodeModules(filename)) {
1612-
throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(filename);
1613-
}
16141603
// If already analyzed the source, then it will be cached.
16151604
const source = getMaybeCachedSource(module, filename);
16161605
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)