Skip to content

Commit

Permalink
module: add --experimental-strip-input-types
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-ippolito committed Dec 16, 2024
1 parent 547a634 commit 588e2a7
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 11 deletions.
12 changes: 12 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,18 @@ added:

Use this flag to enable [ShadowRealm][] support.

### `--experimental-strip-input-types`

<!-- YAML
added: REPLACEME
-->

> Stability: 1.1 - Active development
Enable experimental type-stripping for `--eval`.
Implies [`--experimental-strip-types`][].
For more information, see the [TypeScript type-stripping][] documentation.

### `--experimental-strip-types`

<!-- YAML
Expand Down
3 changes: 2 additions & 1 deletion doc/api/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ import { fn, FnParams } from './fn.ts';

### Non-file forms of input

Type stripping can be enabled for `--eval`. The module system
Type stripping can be enabled for `--eval` by using the flag [`--experimental-strip-input-types`][]. The module system
will be determined by `--input-type`, as it is for JavaScript.

TypeScript syntax is unsupported in the REPL, STDIN input, `--print`, `--check`, and
Expand Down Expand Up @@ -181,6 +181,7 @@ with `#`.
[CommonJS]: modules.md
[ES Modules]: esm.md
[Full TypeScript support]: #full-typescript-support
[`--experimental-strip-input-types`]: cli.md#--experimental-strip-input-types
[`--experimental-strip-types`]: cli.md#--experimental-strip-types
[`--experimental-transform-types`]: cli.md#--experimental-transform-types
[`tsconfig` "paths"]: https://www.typescriptlang.org/tsconfig/#paths
Expand Down
3 changes: 3 additions & 0 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ Configures the type of test isolation used in the test runner.
.It Fl -experimental-test-module-mocks
Enable module mocking in the test runner.
.
.It Fl -experimental-strip-input-types
Enable experimental type-stripping for eval input.
.
.It Fl -experimental-strip-types
Enable experimental type-stripping for TypeScript files.
.
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/main/eval_string.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ addBuiltinLibsToObject(globalThis, '<eval>');
markBootstrapComplete();

const code = getOptionValue('--eval');
const source = getOptionValue('--experimental-strip-types') ?
const source = getOptionValue('--experimental-strip-input-types') ?
stripTypeScriptModuleTypes(code) :
code;

Expand Down
5 changes: 5 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
"ES module to preload (option can be repeated)",
&EnvironmentOptions::preload_esm_modules,
kAllowedInEnvvar);
AddOption("--experimental-strip-input-types",
"Experimental type-stripping for eval",
&EnvironmentOptions::experimental_strip_input_types,
kAllowedInEnvvar);
AddOption("--experimental-strip-types",
"Experimental type-stripping for TypeScript files.",
&EnvironmentOptions::experimental_strip_types,
Expand All @@ -855,6 +859,7 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
kAllowedInEnvvar);
Implies("--experimental-transform-types", "--experimental-strip-types");
Implies("--experimental-transform-types", "--enable-source-maps");
Implies("--experimental-strip-input-types", "--experimental-strip-types");
AddOption("--interactive",
"always enter the REPL even if stdin does not appear "
"to be a terminal",
Expand Down
1 change: 1 addition & 0 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ class EnvironmentOptions : public Options {
std::vector<std::string> preload_esm_modules;

bool experimental_strip_types = false;
bool experimental_strip_input_types = false;
bool experimental_transform_types = false;

std::vector<std::string> user_argv;
Expand Down
18 changes: 9 additions & 9 deletions test/es-module/test-typescript-eval.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if (!process.config.variables.node_use_amaro) skip('Requires Amaro');

test('eval TypeScript ESM syntax', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--experimental-strip-input-types',
'--eval',
`import util from 'node:util'
const text: string = 'Hello, TypeScript!'
Expand All @@ -19,7 +19,7 @@ test('eval TypeScript ESM syntax', async () => {

test('eval TypeScript ESM syntax with input-type module', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--experimental-strip-input-types',
'--input-type=module',
'--eval',
`import util from 'node:util'
Expand All @@ -33,7 +33,7 @@ test('eval TypeScript ESM syntax with input-type module', async () => {

test('eval TypeScript CommonJS syntax', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--experimental-strip-input-types',
'--eval',
`const util = require('node:util');
const text: string = 'Hello, TypeScript!'
Expand All @@ -46,7 +46,7 @@ test('eval TypeScript CommonJS syntax', async () => {

test('eval TypeScript CommonJS syntax with input-type commonjs', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--experimental-strip-input-types',
'--input-type=commonjs',
'--eval',
`const util = require('node:util');
Expand All @@ -60,7 +60,7 @@ test('eval TypeScript CommonJS syntax with input-type commonjs', async () => {

test('eval TypeScript CommonJS syntax by default', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--experimental-strip-input-types',
'--eval',
`const util = require('node:util');
const text: string = 'Hello, TypeScript!'
Expand All @@ -74,7 +74,7 @@ test('eval TypeScript CommonJS syntax by default', async () => {

test('TypeScript ESM syntax not specified', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--experimental-strip-input-types',
'--eval',
`import util from 'node:util'
const text: string = 'Hello, TypeScript!'
Expand All @@ -86,7 +86,7 @@ test('TypeScript ESM syntax not specified', async () => {

test('expect fail eval TypeScript CommonJS syntax with input-type module', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--experimental-strip-input-types',
'--input-type=module',
'--eval',
`const util = require('node:util');
Expand All @@ -100,7 +100,7 @@ test('expect fail eval TypeScript CommonJS syntax with input-type module', async

test('expect fail eval TypeScript ESM syntax with input-type commonjs', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--experimental-strip-input-types',
'--input-type=commonjs',
'--eval',
`import util from 'node:util'
Expand All @@ -113,7 +113,7 @@ test('expect fail eval TypeScript ESM syntax with input-type commonjs', async ()

test('check syntax error is thrown when passing invalid syntax', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--experimental-strip-input-types',
'--eval',
'enum Foo { A, B, C }']);
strictEqual(result.stdout, '');
Expand Down

0 comments on commit 588e2a7

Please sign in to comment.