ts-node icon indicating copy to clipboard operation
ts-node copied to clipboard

FR: Either multiline support or stop-paste-on-error

Open qpwo opened this issue 3 years ago • 5 comments

Desired Behavior

Copy-paste below code into ts-node twice, and only get Duplicate function implementation error.

function add(x: number, y: number): number {
    return x + y
}

Current behavior

$ ts-node
> function add(x: number, y: number): number {
...     return x + y
... }
'use strict'
> function add(x: number, y: number): number {
[eval].ts:1:10 - error TS2393: Duplicate function implementation.

1 function add(x: number, y: number): number {
           ~~~
[eval].ts:4:10 - error TS2393: Duplicate function implementation.

4 function add(x: number, y: number): number {
           ~~~
[eval].ts:4:37 - error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.

4 function add(x: number, y: number): number {
                                      ~~~~~~
[eval].ts:5:1 - error TS1005: '}' expected.

5 
  

  [eval].ts:4:44
    4 function add(x: number, y: number): number {
                                                 ~
    The parser expected to find a '}' to match the '{' token here.

undefined
>     return x + y
[eval].ts:4:5 - error TS1108: A 'return' statement can only be used within a function body.

4     return x + y
      ~~~~~~

undefined
> }
[eval].ts:4:1 - error TS1128: Declaration or statement expected.

4 }
  ~

undefined
> 

Is this request related to a problem?

Frustrated when I paste in a function I've defined before and get a pile of errors.

Alternatives you've considered

You can put the code in a block to redefine functions, but maybe I'd rather get the error.

ts-node
> function add(x: number, y: number): number {
... return x + y;
... }
'use strict'
> add(5,10)
15
> {
...     function add(x: number, y: number): number {
.....     return x + x + y + y;
.....     }
... }
[Function: add]
> 
undefined
> add(5,10)
30
> 

Additional context

I heard there is better multiline support coming to node repl sometime and it might automatically solve this. I don't know much about REPLs or if you have a reliable way to detect if code is being pasted instead of typed.

qpwo avatar May 28 '21 19:05 qpwo

Possible duplicate of #729

Marking "help wanted" to indicate we will accept a pull request. Here is some context for any would-be author:

We recognize certain TS diagnostics as "recoverable" indicating multiline input. Maybe some diagnostics are missing from this list. https://github.com/TypeStrong/ts-node/blob/aba4ae6085420bde82c7aad256b2d9ee0e9a0de2/src/repl.ts#L355-L370

Our ignoreDiagnostics option allows suppressing certain TS diagnostics. It's possible that the REPL should get additional suppressions by default. See also #1120

cspotcode avatar May 28 '21 19:05 cspotcode

@qpwo are you interested in writing a pull request for this?

cspotcode avatar May 28 '21 19:05 cspotcode

I'll give it a try sometime but expect it to be challenging since I'm not versed in this kind of repo

qpwo avatar May 28 '21 21:05 qpwo

No worries, this one should be manageable. Check out CONTRIBUTING.md and feel free to ask me any questions. npm run test-local pretty much does the heavy lifting.

And thanks!

On Fri, May 28, 2021, 5:00 PM Luke Harold Miles @.***> wrote:

I'll give it a try sometime but expect it to be challenging since I'm not versed in this kind of repo

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/TypeStrong/ts-node/issues/1352#issuecomment-850667212, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAC35OACRA7VONWGXIKKL23TP776JANCNFSM45XKBG5Q .

cspotcode avatar May 28 '21 21:05 cspotcode

Isn't following https://docs.micropython.org/en/latest/reference/repl.html#paste-mode a suitable convention? The Micropython REPL feature seems to have a similar purpose, being able to treat multiple lines as a unit before attempting to compile/interpret them.

cefn avatar Sep 13 '21 14:09 cefn