module: add module.stripTypeScriptTypes · nodejs/node@6575b76 · GitHub
Skip to content

Commit 6575b76

Browse files
marco-ippolitoruyadorno
authored andcommitted
module: add module.stripTypeScriptTypes
PR-URL: #55282 Backport-PR-URL: #56208 Fixes: #54300 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Richard Lau <rlau@redhat.com>
1 parent 0794861 commit 6575b76

10 files changed

Lines changed: 358 additions & 89 deletions

File tree

doc/api/module.md

Lines changed: 101 additions & 0 deletions

lib/internal/main/eval_string.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const {
1414
markBootstrapComplete,
1515
} = require('internal/process/pre_execution');
1616
const { evalModuleEntryPoint, evalScript } = require('internal/process/execution');
17-
const { addBuiltinLibsToObject, stripTypeScriptTypes } = require('internal/modules/helpers');
18-
17+
const { addBuiltinLibsToObject } = require('internal/modules/helpers');
18+
const { stripTypeScriptModuleTypes } = require('internal/modules/typescript');
1919
const { getOptionValue } = require('internal/options');
2020

2121
prepareMainThreadExecution();
@@ -24,7 +24,7 @@ markBootstrapComplete();
2424

2525
const code = getOptionValue('--eval');
2626
const source = getOptionValue('--experimental-strip-types') ?
27-
stripTypeScriptTypes(code) :
27+
stripTypeScriptModuleTypes(code) :
2828
code;
2929

3030
const print = getOptionValue('--print');

lib/internal/modules/cjs/loader.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ const {
153153
setHasStartedUserCJSExecution,
154154
stripBOM,
155155
toRealPath,
156-
stripTypeScriptTypes,
157156
} = require('internal/modules/helpers');
157+
const { stripTypeScriptModuleTypes } = require('internal/modules/typescript');
158158
const packageJsonReader = require('internal/modules/package_json_reader');
159159
const { getOptionValue, getEmbedderOptions } = require('internal/options');
160160
const shouldReportRequiredModules = getLazy(() => process.env.WATCH_REPORT_DEPENDENCIES);
@@ -1347,7 +1347,7 @@ let emittedRequireModuleWarning = false;
13471347
function loadESMFromCJS(mod, filename) {
13481348
let source = getMaybeCachedSource(mod, filename);
13491349
if (getOptionValue('--experimental-strip-types') && path.extname(filename) === '.mts') {
1350-
source = stripTypeScriptTypes(source, filename);
1350+
source = stripTypeScriptModuleTypes(source, filename);
13511351
}
13521352
const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader();
13531353
const isMain = mod[kIsMainSymbol];
@@ -1584,7 +1584,7 @@ function getMaybeCachedSource(mod, filename) {
15841584

15851585
function loadCTS(module, filename) {
15861586
const source = getMaybeCachedSource(module, filename);
1587-
const code = stripTypeScriptTypes(source, filename);
1587+
const code = stripTypeScriptModuleTypes(source, filename);
15881588
module._compile(code, filename, 'commonjs');
15891589
}
15901590

@@ -1596,7 +1596,7 @@ function loadCTS(module, filename) {
15961596
function loadTS(module, filename) {
15971597
// If already analyzed the source, then it will be cached.
15981598
const source = getMaybeCachedSource(module, filename);
1599-
const content = stripTypeScriptTypes(source, filename);
1599+
const content = stripTypeScriptModuleTypes(source, filename);
16001600
let format;
16011601
const pkg = packageJsonReader.getNearestParentPackageJSON(filename);
16021602
// Function require shouldn't be used in ES modules.
@@ -1616,7 +1616,7 @@ function loadTS(module, filename) {
16161616
if (Module._cache[parentPath]) {
16171617
let parentSource;
16181618
try {
1619-
parentSource = stripTypeScriptTypes(fs.readFileSync(parentPath, 'utf8'), parentPath);
1619+
parentSource = stripTypeScriptModuleTypes(fs.readFileSync(parentPath, 'utf8'), parentPath);
16201620
} catch {
16211621
// Continue regardless of error.
16221622
}

lib/internal/modules/esm/get_format.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,10 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
164164
// Since experimental-strip-types depends on detect-module, we always return null
165165
// if source is undefined.
166166
if (!source) { return null; }
167-
const { stripTypeScriptTypes, stringify } = require('internal/modules/helpers');
167+
const { stringify } = require('internal/modules/helpers');
168+
const { stripTypeScriptModuleTypes } = require('internal/modules/typescript');
168169
const stringifiedSource = stringify(source);
169-
const parsedSource = stripTypeScriptTypes(stringifiedSource, fileURLToPath(url));
170+
const parsedSource = stripTypeScriptModuleTypes(stringifiedSource, fileURLToPath(url));
170171
const detectedFormat = detectModuleFormat(parsedSource, url);
171172
const format = `${detectedFormat}-typescript`;
172173
if (format === 'module-typescript' && foundPackageJson) {

lib/internal/modules/esm/translators.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ const {
3030
assertBufferSource,
3131
loadBuiltinModule,
3232
stringify,
33-
stripTypeScriptTypes,
3433
stripBOM,
3534
urlToFilename,
3635
} = require('internal/modules/helpers');
36+
const { stripTypeScriptModuleTypes } = require('internal/modules/typescript');
3737
const {
3838
kIsCachedByESMLoader,
3939
Module: CJSModule,
@@ -244,7 +244,7 @@ translators.set('require-commonjs', (url, source, isMain) => {
244244
translators.set('require-commonjs-typescript', (url, source, isMain) => {
245245
emitExperimentalWarning('Type Stripping');
246246
assert(cjsParse);
247-
const code = stripTypeScriptTypes(stringify(source), url);
247+
const code = stripTypeScriptModuleTypes(stringify(source), url);
248248
return createCJSModuleWrap(url, code);
249249
});
250250

@@ -459,7 +459,7 @@ translators.set('wasm', async function(url, source) {
459459
translators.set('commonjs-typescript', function(url, source) {
460460
emitExperimentalWarning('Type Stripping');
461461
assertBufferSource(source, true, 'load');
462-
const code = stripTypeScriptTypes(stringify(source), url);
462+
const code = stripTypeScriptModuleTypes(stringify(source), url);
463463
debug(`Translating TypeScript ${url}`);
464464
return FunctionPrototypeCall(translators.get('commonjs'), this, url, code, false);
465465
});
@@ -468,7 +468,7 @@ translators.set('commonjs-typescript', function(url, source) {
468468
translators.set('module-typescript', function(url, source) {
469469
emitExperimentalWarning('Type Stripping');
470470
assertBufferSource(source, true, 'load');
471-
const code = stripTypeScriptTypes(stringify(source), url);
471+
const code = stripTypeScriptModuleTypes(stringify(source), url);
472472
debug(`Translating TypeScript ${url}`);
473473
return FunctionPrototypeCall(translators.get('module'), this, url, code, false);
474474
});

lib/internal/modules/helpers.js

Lines changed: 1 addition & 74 deletions

0 commit comments

Comments
 (0)