-
-
Notifications
You must be signed in to change notification settings - Fork 35.7k
repl: add experimental TypeScript support #64077
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,12 +95,14 @@ const { | |
| const acornWalk = require('internal/deps/acorn/acorn-walk/dist/walk'); | ||
| const { | ||
| decorateErrorStack, | ||
| emitExperimentalWarning, | ||
| isError, | ||
| deprecate, | ||
| SideEffectFreeRegExpPrototypeSymbolReplace, | ||
| SideEffectFreeRegExpPrototypeSymbolSplit, | ||
| } = require('internal/util'); | ||
| const { inspect } = require('internal/util/inspect'); | ||
| const { processTypeScriptCode } = require('internal/modules/typescript'); | ||
| const vm = require('vm'); | ||
|
|
||
| const { runInThisContext, runInContext } = vm.Script.prototype; | ||
|
|
@@ -141,6 +143,10 @@ const { | |
| const experimentalREPLAwait = getOptionValue( | ||
| '--experimental-repl-await', | ||
| ); | ||
| const experimentalREPLTypeScript = getOptionValue( | ||
| '--experimental-repl-typescript', | ||
| ); | ||
|
|
||
| const pendingDeprecation = getOptionValue('--pending-deprecation'); | ||
| const { | ||
| REPL_MODE_SLOPPY, | ||
|
|
@@ -241,6 +247,11 @@ function removeProcessNewListener() { | |
|
|
||
| fixReplRequire(module); | ||
|
|
||
| const tsOptions = { __proto__: null, mode: 'transform' }; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. transform? We have removed this mode, I don't see that we would want to offer different TS support in REPL than in normal execution?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes we should not transform at all |
||
| if (experimentalREPLTypeScript) { | ||
| emitExperimentalWarning('TypeScript support in the REPL'); | ||
| } | ||
|
|
||
| // This is the default "writer" value, if none is passed in the REPL options, | ||
| // and it can be overridden by custom print functions, such as `probe` or | ||
| // `eyes.js`. | ||
|
|
@@ -499,7 +510,9 @@ class REPLServer extends Interface { | |
| const fallbackCode = SideEffectFreeRegExpPrototypeSymbolReplace(/\bawait\b/g, code, ''); | ||
| try { | ||
| makeContextifyScript( | ||
| fallbackCode, // code | ||
| experimentalREPLTypeScript ? | ||
| processTypeScriptCode(fallbackCode, tsOptions) : | ||
| fallbackCode, // code | ||
| file, // filename, | ||
| 0, // lineOffset | ||
| 0, // columnOffset, | ||
|
|
@@ -537,7 +550,9 @@ class REPLServer extends Interface { | |
| code = `'use strict'; void 0;\n${code}`; | ||
| } | ||
| script = makeContextifyScript( | ||
| code, // code | ||
| experimentalREPLTypeScript ? | ||
| processTypeScriptCode(code, tsOptions) : | ||
| code, // code | ||
| file, // filename, | ||
| 0, // lineOffset | ||
| 0, // columnOffset, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Flags: --experimental-repl-typescript | ||
| 'use strict'; | ||
| require('../common'); | ||
| const assert = require('assert'); | ||
|
|
||
| const { startNewREPLServer } = require('../common/repl'); | ||
|
|
||
| const { input, output } = startNewREPLServer({ terminal: false, prompt: '> ' }); | ||
|
|
||
| input.emit('data', 'let x: number = 3\n'); | ||
| assert.match(output.accumulator, /undefined\n> /); | ||
|
Check failure on line 11 in test/parallel/test-repl-typescript.js
|
||
| output.accumulator = ''; | ||
|
|
||
| input.emit('data', 'x\n'); | ||
| assert.match(output.accumulator, /3\n> /); | ||
|
|
||
| // Transform TypeScript to JavaScript and evaluate it | ||
| input.emit('data', 'enum Color { Red, Green, Blue }\n'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a test for: |
||
| assert.match(output.accumulator, /undefined\n> /); | ||
| output.accumulator = ''; | ||
|
|
||
| input.emit('data', 'Color.Reed\n'); | ||
| assert.match(output.accumulator, /0\n> /); | ||
|
Check failure on line 23 in test/parallel/test-repl-typescript.js
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why release candidate