Skip to content
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

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

Open
qpwo opened this issue May 28, 2021 · 5 comments
Open

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

qpwo opened this issue May 28, 2021 · 5 comments
Labels
you can do this Good candidate for a pull request.

Comments

@qpwo
Copy link

qpwo commented May 28, 2021

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 qpwo changed the title Either multiline support or stop-paste-on-error FR: Either multiline support or stop-paste-on-error May 28, 2021
@cspotcode cspotcode added the you can do this Good candidate for a pull request. label May 28, 2021
@cspotcode
Copy link
Collaborator

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.

ts-node/src/repl.ts

Lines 355 to 370 in aba4ae6

const RECOVERY_CODES: Set<number> = new Set([
1003, // "Identifier expected."
1005, // "')' expected."
1109, // "Expression expected."
1126, // "Unexpected end of text."
1160, // "Unterminated template literal."
1161, // "Unterminated regular expression literal."
2355, // "A function whose declared type is neither 'void' nor 'any' must return a value."
]);
/**
* Check if a function can recover gracefully.
*/
function isRecoverable(error: TSError) {
return error.diagnosticCodes.every((code) => RECOVERY_CODES.has(code));
}

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

@cspotcode
Copy link
Collaborator

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

@qpwo
Copy link
Author

qpwo commented May 28, 2021

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

@cspotcode
Copy link
Collaborator

cspotcode commented May 28, 2021 via email

@cefn
Copy link

cefn commented Sep 13, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
you can do this Good candidate for a pull request.
Projects
None yet
Development

No branches or pull requests

3 participants