module: support eval with ts syntax detection · nodejs/node@174d88e · GitHub
Skip to content

Commit 174d88e

Browse files
marco-ippolitoaduh95
authored andcommitted
module: support eval with ts syntax detection
PR-URL: #56285 Backport-PR-URL: #56912 Refs: nodejs/typescript#17 Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
1 parent 8cbb7cc commit 174d88e

10 files changed

Lines changed: 511 additions & 68 deletions

File tree

doc/api/cli.md

Lines changed: 17 additions & 2 deletions

lib/internal/main/eval_string.js

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,37 @@ const {
1313
prepareMainThreadExecution,
1414
markBootstrapComplete,
1515
} = require('internal/process/pre_execution');
16-
const { evalModuleEntryPoint, evalScript } = require('internal/process/execution');
16+
const {
17+
evalModuleEntryPoint,
18+
evalTypeScript,
19+
parseAndEvalCommonjsTypeScript,
20+
parseAndEvalModuleTypeScript,
21+
evalScript,
22+
} = require('internal/process/execution');
1723
const { addBuiltinLibsToObject } = require('internal/modules/helpers');
18-
const { stripTypeScriptModuleTypes } = require('internal/modules/typescript');
1924
const { getOptionValue } = require('internal/options');
2025

2126
prepareMainThreadExecution();
2227
addBuiltinLibsToObject(globalThis, '<eval>');
2328
markBootstrapComplete();
2429

2530
const code = getOptionValue('--eval');
26-
const source = getOptionValue('--experimental-strip-types') ?
27-
stripTypeScriptModuleTypes(code) :
28-
code;
2931

3032
const print = getOptionValue('--print');
3133
const shouldLoadESM = getOptionValue('--import').length > 0 || getOptionValue('--experimental-loader').length > 0;
32-
if (getOptionValue('--input-type') === 'module' ||
33-
(getOptionValue('--experimental-default-type') === 'module' && getOptionValue('--input-type') !== 'commonjs')) {
34-
evalModuleEntryPoint(source, print);
34+
const inputType = getOptionValue('--input-type');
35+
const tsEnabled = getOptionValue('--experimental-strip-types');
36+
if (inputType === 'module' ||
37+
(getOptionValue('--experimental-default-type') === 'module' && inputType !== 'commonjs')) {
38+
evalModuleEntryPoint(code, print);
39+
} else if (inputType === 'module-typescript' && tsEnabled) {
40+
parseAndEvalModuleTypeScript(code, print);
3541
} else {
3642
// For backward compatibility, we want the identifier crypto to be the
3743
// `node:crypto` module rather than WebCrypto.
3844
const isUsingCryptoIdentifier =
39-
getOptionValue('--experimental-global-webcrypto') &&
40-
RegExpPrototypeExec(/\bcrypto\b/, source) !== null;
45+
getOptionValue('--experimental-global-webcrypto') &&
46+
RegExpPrototypeExec(/\bcrypto\b/, code) !== null;
4147
const shouldDefineCrypto = isUsingCryptoIdentifier && internalBinding('config').hasOpenSSL;
4248

4349
if (isUsingCryptoIdentifier && !shouldDefineCrypto) {
@@ -52,11 +58,24 @@ if (getOptionValue('--input-type') === 'module' ||
5258
};
5359
ObjectDefineProperty(object, name, { __proto__: null, set: setReal });
5460
}
55-
evalScript('[eval]',
56-
shouldDefineCrypto ? (
57-
print ? `let crypto=require("node:crypto");{${source}}` : `(crypto=>{{${source}}})(require('node:crypto'))`
58-
) : source,
59-
getOptionValue('--inspect-brk'),
60-
print,
61-
shouldLoadESM);
61+
62+
let evalFunction;
63+
if (inputType === 'commonjs') {
64+
evalFunction = evalScript;
65+
} else if (inputType === 'commonjs-typescript' && tsEnabled) {
66+
evalFunction = parseAndEvalCommonjsTypeScript;
67+
} else if (tsEnabled) {
68+
evalFunction = evalTypeScript;
69+
} else {
70+
// Default to commonjs.
71+
evalFunction = evalScript;
72+
}
73+
74+
evalFunction('[eval]',
75+
shouldDefineCrypto ? (
76+
print ? `let crypto=require("node:crypto");{${code}}` : `(crypto=>{{${code}}})(require('node:crypto'))`
77+
) : code,
78+
getOptionValue('--inspect-brk'),
79+
print,
80+
shouldLoadESM);
6281
}

lib/internal/modules/cjs/loader.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,6 @@ function initializeCJS() {
435435

436436
const tsEnabled = getOptionValue('--experimental-strip-types');
437437
if (tsEnabled) {
438-
emitExperimentalWarning('Type Stripping');
439438
Module._extensions['.cts'] = loadCTS;
440439
Module._extensions['.ts'] = loadTS;
441440
}

lib/internal/modules/esm/loader.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,25 @@ class ModuleLoader {
210210
}
211211
}
212212

213-
async eval(source, url, isEntryPoint = false) {
213+
/**
214+
*
215+
* @param {string} source Source code of the module.
216+
* @param {string} url URL of the module.
217+
* @returns {object} The module wrap object.
218+
*/
219+
createModuleWrap(source, url) {
220+
return compileSourceTextModule(url, source, this);
221+
}
222+
223+
/**
224+
*
225+
* @param {string} url URL of the module.
226+
* @param {object} wrap Module wrap object.
227+
* @param {boolean} isEntryPoint Whether the module is the entry point.
228+
* @returns {Promise<object>} The module object.
229+
*/
230+
async executeModuleJob(url, wrap, isEntryPoint = false) {
214231
const { ModuleJob } = require('internal/modules/esm/module_job');
215-
const wrap = compileSourceTextModule(url, source, this);
216232
const module = await onImport.tracePromise(async () => {
217233
const job = new ModuleJob(
218234
this, url, undefined, wrap, false, false);
@@ -232,6 +248,18 @@ class ModuleLoader {
232248
};
233249
}
234250

251+
/**
252+
*
253+
* @param {string} source Source code of the module.
254+
* @param {string} url URL of the module.
255+
* @param {boolean} isEntryPoint Whether the module is the entry point.
256+
* @returns {Promise<object>} The module object.
257+
*/
258+
eval(source, url, isEntryPoint = false) {
259+
const wrap = this.createModuleWrap(source, url);
260+
return this.executeModuleJob(url, wrap, isEntryPoint);
261+
}
262+
235263
/**
236264
* Get a (possibly not yet fully linked) module job from the cache, or create one and return its Promise.
237265
* @param {string} specifier The module request of the module to be resolved. Typically, what's

lib/internal/modules/esm/translators.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ translators.set('require-commonjs', (url, source, isMain) => {
242242
// Handle CommonJS modules referenced by `require` calls.
243243
// This translator function must be sync, as `require` is sync.
244244
translators.set('require-commonjs-typescript', (url, source, isMain) => {
245-
emitExperimentalWarning('Type Stripping');
246245
assert(cjsParse);
247246
const code = stripTypeScriptModuleTypes(stringify(source), url);
248247
return createCJSModuleWrap(url, code);
@@ -457,7 +456,6 @@ translators.set('wasm', async function(url, source) {
457456

458457
// Strategy for loading a commonjs TypeScript module
459458
translators.set('commonjs-typescript', function(url, source) {
460-
emitExperimentalWarning('Type Stripping');
461459
assertBufferSource(source, true, 'load');
462460
const code = stripTypeScriptModuleTypes(stringify(source), url);
463461
debug(`Translating TypeScript ${url}`);
@@ -466,7 +464,6 @@ translators.set('commonjs-typescript', function(url, source) {
466464

467465
// Strategy for loading an esm TypeScript module
468466
translators.set('module-typescript', function(url, source) {
469-
emitExperimentalWarning('Type Stripping');
470467
assertBufferSource(source, true, 'load');
471468
const code = stripTypeScriptModuleTypes(stringify(source), url);
472469
debug(`Translating TypeScript ${url}`);

lib/internal/modules/typescript.js

Lines changed: 5 additions & 1 deletion

0 commit comments

Comments
 (0)