-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vm: add vm.stripTypeScriptTypes(code, options)
- Loading branch information
1 parent
20d8b85
commit 9ab643f
Showing
4 changed files
with
199 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const vm = require('vm'); | ||
const { test } = require('node:test'); | ||
|
||
common.expectWarning( | ||
'ExperimentalWarning', | ||
'vm.stripTypeScriptTypes is an experimental feature and might change at any time', | ||
); | ||
|
||
test('vm.stripTypeScriptTypes', () => { | ||
const source = 'const x: number = 1;'; | ||
const result = vm.stripTypeScriptTypes(source); | ||
assert.strictEqual(result, 'const x = 1;'); | ||
}); | ||
|
||
test('vm.stripTypeScriptTypes explicit', () => { | ||
const source = 'const x: number = 1;'; | ||
const result = vm.stripTypeScriptTypes(source, { mode: 'strip-only' }); | ||
assert.strictEqual(result, 'const x = 1;'); | ||
}); | ||
|
||
test('vm.stripTypeScriptTypes invalid mode', () => { | ||
const source = 'const x: number = 1;'; | ||
assert.throws(() => vm.stripTypeScriptTypes(source, { mode: 'invalid' }), { code: 'ERR_INVALID_ARG_VALUE' }); | ||
}); | ||
|
||
test('vm.stripTypeScriptTypes sourceMap throws when mode is strip-only', () => { | ||
const source = 'const x: number = 1;'; | ||
assert.throws(() => vm.stripTypeScriptTypes(source, | ||
{ mode: 'strip-only', sourceMap: true }), | ||
{ code: 'ERR_INVALID_ARG_VALUE' }); | ||
}); | ||
|
||
test('vm.stripTypeScriptTypes filename throws when mode is strip-only', () => { | ||
const source = 'const x: number = 1;'; | ||
assert.throws(() => vm.stripTypeScriptTypes(source, | ||
{ mode: 'strip-only', filename: 'foo.ts' }), | ||
{ code: 'ERR_INVALID_ARG_VALUE' }); | ||
}); | ||
|
||
test('vm.stripTypeScriptTypes source map when mode is transform', () => { | ||
const source = ` | ||
namespace MathUtil { | ||
export const add = (a: number, b: number) => a + b; | ||
}`; | ||
const result = vm.stripTypeScriptTypes(source, { mode: 'transform', sourceMap: true }); | ||
const script = new vm.Script(result); | ||
const sourceMap = | ||
{ | ||
version: 3, | ||
sources: [ | ||
'<anon>', | ||
], | ||
sourcesContent: [ | ||
'\n namespace MathUtil {\n export const add = (a: number, b: number) => a + b;\n }', | ||
], | ||
names: [], | ||
mappings: ';UACY;aACK,MAAM,CAAC,GAAW,IAAc,IAAI;AACnD,GAFU,aAAA' | ||
}; | ||
assert(script.sourceMapURL, `sourceMappingURL=data:application/json;base64,${JSON.stringify(sourceMap)}`); | ||
}); | ||
|
||
test('vm.stripTypeScriptTypes source map when mode is transform and filename', () => { | ||
const source = ` | ||
namespace MathUtil { | ||
export const add = (a: number, b: number) => a + b; | ||
}`; | ||
const result = vm.stripTypeScriptTypes(source, { mode: 'transform', sourceMap: true, filename: 'test.ts' }); | ||
const script = new vm.Script(result); | ||
const sourceMap = | ||
{ | ||
version: 3, | ||
sources: [ | ||
'test.ts', | ||
], | ||
sourcesContent: [ | ||
'\n namespace MathUtil {\n export const add = (a: number, b: number) => a + b;\n }', | ||
], | ||
names: [], | ||
mappings: ';UACY;aACK,MAAM,CAAC,GAAW,IAAc,IAAI;AACnD,GAFU,aAAA' | ||
}; | ||
assert(script.sourceMapURL, `sourceMappingURL=data:application/json;base64,${JSON.stringify(sourceMap)}`); | ||
}); |