repl: add experimental TypeScript support#64077
Conversation
|
Review requested:
|
|
This is going to cause problems with multiline, I suspect. Concise example: const regexps = new Set<
RegExp
>();If typed as multiline input to REPL, it's going to consider the statement complete after the second newline, as it's a valid JS statement, and evaluate |
|
It should be handled exactly like we handle the |
Signed-off-by: Aviv Keller <me@aviv.sh>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #64077 +/- ##
==========================================
- Coverage 90.13% 90.13% -0.01%
==========================================
Files 741 741
Lines 241976 242087 +111
Branches 45543 45571 +28
==========================================
+ Hits 218116 218207 +91
- Misses 15379 15398 +19
- Partials 8481 8482 +1
🚀 New features to boost your workflow:
|
|
cc @nodejs/typescript |
It's not a blocking concern per se, but I do think that this has the potential to be really quite a UX tripwire. For example, pretty much any use of function template parameters hits this ( The alternative would be to make TS an explicit on-off repl flag, ie. parse REPL input like it were in a There is also a subtle difference between REPL and |
|
In the -e you can pass a flag to force the evaluation as module ts or common js ts to disambiguate. In a future where ts support is mixed with js and not behind a flag, js should always be evaluated first |
|
I'm not convinced this is the best way to handle this. What might be better is a new REPL command similar to The otherwise regular behavior of the repl remains untouched. To cover |
@Renegade334 I suppose this is mostly a concern for people copy-pasting multiline inputs into the REPL. That may be solvable by supporting paste bracket mode (87af913). |
| if (!valid && experimentalREPLTypeScript) { | ||
| try { | ||
| const { stripTypeScriptModuleTypes } = | ||
| require('internal/modules/typescript'); | ||
| code = stripTypeScriptModuleTypes(code, file); | ||
| } catch (err) { | ||
| if (err.code !== 'ERR_INVALID_TYPESCRIPT_SYNTAX' && | ||
| err.code !== 'ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX') { | ||
| throw err; | ||
| } | ||
| // Let the evaluator report the original JavaScript syntax error. It | ||
| // also determines whether incomplete input is recoverable. | ||
| } | ||
| } |
There was a problem hiding this comment.
There are other subtleties here specific to strip mode and partial/multiline input, even without any TS syntax present.
For example, function foo() is not deemed a valid statement by acorn and fails isValidSyntax(), but throws a recoverable error on evaluation and therefore currently triggers a multiline continuation. However, it's stripped by swc to pure whitespace, so after this change, the line will be discarded completely.
If we stick with the "speculatively type-strip line-by-line on an invalid JS parse" model then it's worth pointing out that previously valid multiline JS could now do weird things.
Would everyone be fine with TypeScript being, as @Renegade334 said, opt-in, not attempt to be JS and fallback? |
Adds experimental TypeScript support to the REPL, opt-in-able via the
--experimental-repl-typescriptflag.