Skip to content

Commit 8af1382

Browse files
Revert "vm: add vm.stripTypeScriptTypes(code, options)"
This reverts commit 9ab643f.
1 parent 9ab643f commit 8af1382

File tree

4 files changed

+31
-199
lines changed

4 files changed

+31
-199
lines changed

doc/api/vm.md

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,65 +1564,6 @@ local scope, so the value `localVar` is changed. In this way
15641564
`vm.runInThisContext()` is much like an [indirect `eval()` call][], e.g.
15651565
`(0,eval)('code')`.
15661566
1567-
## `vm.stripTypeScriptTypes(code[, options])`
1568-
1569-
<!-- YAML
1570-
added: REPLACEME
1571-
-->
1572-
1573-
> Stability: 1.0 - Early development
1574-
1575-
* `code` {string} The code to strip type annotations from.
1576-
* `options` {Object}
1577-
* `mode` {string} **Default:** `'strip-only'`. Possible values are:
1578-
* `'strip-only'` Only strip type annotations without performing the transformation of TypeScript features.
1579-
* `'transform'` Strip type annotations and transform TypeScript features to JavaScript.
1580-
* `sourceMap` {boolean} **Default:** `false`. Only when `mode` is `'transform'`, if `true`, a source map
1581-
will be generated for the transformed code.
1582-
* `filename` {string} Only when `mode` is `'transform'`, specifies the filename used in the source map.
1583-
* Returns: {string} The code with type annotations stripped.
1584-
1585-
`vm.stripTypeScriptTypes()` removes type annotations from TypeScript code. It
1586-
can be used to strip type annotations from TypeScript code before running it
1587-
with `vm.runInContext()` or `vm.compileFunction()`.
1588-
By default, it will throw an error if the code contains TypeScript features
1589-
that require transformation such as `Enums`,
1590-
see [type-stripping][] for more information.
1591-
When mode is `'transform'`, it also transforms TypeScript features to JavaScript,
1592-
see [transform TypeScript features][] for more information.
1593-
When mode is `'strip-only'`, source maps are not generated, because locations are preserved.
1594-
If `sourceMap` or `filename` is provided, when mode is `'strip-only'`, an error will be thrown.
1595-
1596-
```js
1597-
const vm = require('node:vm');
1598-
1599-
const code = `const a: number = 1;`;
1600-
const strippedCode = vm.stripTypeScriptTypes(code);
1601-
console.log(strippedCode);
1602-
// Prints: const a = 1;
1603-
```
1604-
1605-
When `mode` is `'transform'`, the code is transformed to JavaScript:
1606-
1607-
```js
1608-
const vm = require('node:vm');
1609-
1610-
const code = `
1611-
namespace MathUtil {
1612-
export const add = (a: number, b: number) => a + b;
1613-
}`;
1614-
const strippedCode = vm.stripTypeScriptTypes(code, { mode: 'transform', sourceMap: true });
1615-
console.log(strippedCode);
1616-
1617-
// Prints:
1618-
// var MathUtil;
1619-
// (function(MathUtil) {
1620-
// MathUtil.add = (a, b)=>a + b;
1621-
// })(MathUtil || (MathUtil = {}));
1622-
1623-
//# sourceMappingURL=data:application/json;base64, ...
1624-
```
1625-
16261567
## Example: Running an HTTP server within a VM
16271568
16281569
When using either [`script.runInThisContext()`][] or
@@ -2041,5 +1982,3 @@ const { Script, SyntheticModule } = require('node:vm');
20411982
[global object]: https://es5.github.io/#x15.1
20421983
[indirect `eval()` call]: https://es5.github.io/#x10.4.2
20431984
[origin]: https://developer.mozilla.org/en-US/docs/Glossary/Origin
2044-
[transform TypeScript features]: typescript.md#typescript-features
2045-
[type-stripping]: typescript.md#type-stripping

lib/internal/modules/helpers.js

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -313,33 +313,44 @@ function getBuiltinModule(id) {
313313
return normalizedId ? require(normalizedId) : undefined;
314314
}
315315

316+
/**
317+
* TypeScript parsing function, by default Amaro.transformSync.
318+
* @type {Function}
319+
*/
320+
let typeScriptParser;
316321
/**
317322
* The TypeScript parsing mode, either 'strip-only' or 'transform'.
318323
* @type {string}
319324
*/
320-
const getTypeScriptParsingMode = getLazy(() =>
321-
(getOptionValue('--experimental-transform-types') ? 'transform' : 'strip-only'),
322-
);
325+
let typeScriptParsingMode;
326+
/**
327+
* Whether source maps are enabled for TypeScript parsing.
328+
* @type {boolean}
329+
*/
330+
let sourceMapEnabled;
323331

324332
/**
325333
* Load the TypeScript parser.
334+
* @param {Function} parser - A function that takes a string of TypeScript code
326335
* and returns an object with a `code` property.
327336
* @returns {Function} The TypeScript parser function.
328337
*/
329-
const loadTypeScriptParser = getLazy(() => {
330-
const amaro = require('internal/deps/amaro/dist/index');
331-
return amaro.transformSync;
332-
});
338+
function loadTypeScriptParser(parser) {
339+
if (typeScriptParser) {
340+
return typeScriptParser;
341+
}
333342

334-
/**
335-
*
336-
* @param {string} source the source code
337-
* @param {object} options the options to pass to the parser
338-
* @returns {TransformOutput} an object with a `code` property.
339-
*/
340-
function parseTypeScript(source, options) {
341-
const parse = loadTypeScriptParser();
342-
return parse(source, options);
343+
if (parser) {
344+
typeScriptParser = parser;
345+
} else {
346+
const amaro = require('internal/deps/amaro/dist/index');
347+
// Default option for Amaro is to perform Type Stripping only.
348+
typeScriptParsingMode = getOptionValue('--experimental-transform-types') ? 'transform' : 'strip-only';
349+
sourceMapEnabled = getOptionValue('--enable-source-maps');
350+
// Curry the transformSync function with the default options.
351+
typeScriptParser = amaro.transformSync;
352+
}
353+
return typeScriptParser;
343354
}
344355

345356
/**
@@ -354,13 +365,14 @@ function parseTypeScript(source, options) {
354365
*/
355366
function stripTypeScriptTypes(source, filename) {
356367
assert(typeof source === 'string');
368+
const parse = loadTypeScriptParser();
357369
const options = {
358370
__proto__: null,
359-
mode: getTypeScriptParsingMode(),
360-
sourceMap: getOptionValue('--enable-source-maps'),
371+
mode: typeScriptParsingMode,
372+
sourceMap: sourceMapEnabled,
361373
filename,
362374
};
363-
const { code, map } = parseTypeScript(source, options);
375+
const { code, map } = parse(source, options);
364376
if (map) {
365377
// TODO(@marco-ippolito) When Buffer.transcode supports utf8 to
366378
// base64 transformation, we should change this line.
@@ -476,7 +488,6 @@ module.exports = {
476488
loadBuiltinModule,
477489
makeRequireFunction,
478490
normalizeReferrerURL,
479-
parseTypeScript,
480491
stripTypeScriptTypes,
481492
stringify,
482493
stripBOM,

lib/vm.js

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ const {
6767
vm_dynamic_import_main_context_default,
6868
vm_context_no_contextify,
6969
} = internalBinding('symbols');
70-
const { parseTypeScript } = require('internal/modules/helpers');
71-
const { Buffer } = require('buffer');
7270
const kParsingContext = Symbol('script parsing context');
7371

7472
/**
@@ -402,35 +400,6 @@ const vmConstants = {
402400

403401
ObjectFreeze(vmConstants);
404402

405-
function stripTypeScriptTypes(code, options = kEmptyObject) {
406-
emitExperimentalWarning('vm.stripTypeScriptTypes');
407-
validateObject(options, 'options');
408-
const { mode = 'strip-only', sourceMap = false, filename = '' } = options;
409-
validateOneOf(mode, 'options.mode', ['strip-only', 'transform']);
410-
if (mode === 'strip-only') {
411-
validateOneOf(sourceMap, 'options.sourceMap', [false, undefined]);
412-
validateOneOf(filename, 'options.filename', ['', undefined]);
413-
}
414-
validateBoolean(sourceMap, 'options.sourceMap');
415-
validateString(filename, 'options.filename');
416-
417-
const transformOptions = {
418-
__proto__: null,
419-
mode,
420-
sourceMap,
421-
filename,
422-
};
423-
424-
const { code: transformed, map } = parseTypeScript(code, transformOptions);
425-
if (map) {
426-
// TODO(@marco-ippolito) When Buffer.transcode supports utf8 to
427-
// base64 transformation, we should change this line.
428-
const base64SourceMap = Buffer.from(map).toString('base64');
429-
return `${transformed}\n\n//# sourceMappingURL=data:application/json;base64,${base64SourceMap}`;
430-
}
431-
return transformed;
432-
}
433-
434403
module.exports = {
435404
Script,
436405
createContext,
@@ -442,7 +411,6 @@ module.exports = {
442411
compileFunction,
443412
measureMemory,
444413
constants: vmConstants,
445-
stripTypeScriptTypes,
446414
};
447415

448416
// The vm module is patched to include vm.Module, vm.SourceTextModule

test/parallel/test-vm-strip-types.js

Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
 (0)