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 |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #64077 +/- ##
==========================================
+ Coverage 90.16% 90.17% +0.01%
==========================================
Files 746 746
Lines 242760 242781 +21
Branches 45765 45764 -1
==========================================
+ Hits 218875 218931 +56
+ Misses 15375 15335 -40
- Partials 8510 8515 +5
🚀 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? |
|
Bump on my question above :-)? |
yes |
| Enable experimental support for TypeScript stripping in the REPL. Input that is | ||
| valid JavaScript is evaluated as JavaScript before falling back to TypeScript | ||
| type stripping. |
There was a problem hiding this comment.
Don't believe this holds true with the change.
| // Let the evaluator report the original JavaScript syntax error. It | ||
| // also determines whether incomplete input is recoverable. |
There was a problem hiding this comment.
From a multiline perspective, this approach will still only allow multiline input to be pure JS, as SWC InvalidSyntax errors will leave the input untransformed, and input containing TS-only syntax will then fail both JS execution and the Acorn recoverable parsing test.
// Triggers multiline continuation
function foo(
// Triggers "uncaught SyntaxError"
function bar<T>(The inconsistency would be really jarring, and I wonder if we need to just disable multiline in TS mode, and explicitly signpost users to .editor.
Alternatively, we could try and identify recoverable SWC InvalidSyntax errors from the initial strip attempt, but this would still have some inconsistencies with JS multiline. Truly unparseable partial input usually errors in SWC with some flavour of "unexpected eof", but there are cases like const baz: string whose "stripped" JS equivalents are considered recoverable, but which SWC doesn't report in this way.
Adds experimental TypeScript support to the REPL, opt-in-able via the
--experimental-repl-typescriptflag.