denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
96.67k stars 5.34k forks source link

"Uncaught SyntaxError: Unexpected token '<'" when using primitive as parameter in arrow function declaration #23881

Open ploiu opened 5 months ago

ploiu commented 5 months ago

deno --version: deno 1.43.5 (release, x86_64-pc-windows-msvc) v8 12.4.254.13 typescript 5.4.5

I did not find this issue after searching in the issues tab for this repository.


about: deno gives a confusing error message when incorrectly declaring an arrow function. example from REPL:

> (0) => 1
Uncaught SyntaxError: Unexpected token '<'

This can be extremely confusing in normal development because there is no < in the source code for the user to refer to.

This appears to happen regardless of context. Attempting this with a non-arrow function results in a much more helpful error message. Browsers and tsc each give a different error message, and none of them mention < anywhere (browsers tried were chromium-edge and firefox).


Please let me know if this is the wrong bug report format or if this is the wrong place to submit this report, as this is my first issue report in a long time :)

marvinhagemeister commented 5 months ago

Thanks for the detailed bug report. The message is indeed confusing. Might be related to the outer REPL code. SWC alone throws this error:


  x Not a pattern
   ,-[input.tsx:1:1]
 1 | (0) => 1
   :  ^
   `----

Caused by:
    0: failed to process js file
    1: Syntax Error
yazan-abdalrahman commented 2 months ago

Hi @lucacasonato @marvinhagemeister , I need to work on it but would need some guidance on where to begin debugging it.

yazan-abdalrahman commented 2 months ago

@marvinhagemeister Is that the right respond? image

yazan-abdalrahman commented 2 months ago

@lucacasonato, @marvinhagemeister, @dsherret, @bartlomieju

I tried to debug the REPL scenario. I found the source ="(0) => 1", it was parsed as "(<invalid>)=>1;" I found that the "parse_string_input" method in deno_ast parse a program; the erroneous result stems from this parsing. image I tried to debug the pars module in SWC to determine why it returned "<invalid>", but I'm still unsure of how it does so. If you have any knowledge about this area or know one can assist me in this area, please let me know. Additionally, when I tried to debug the run script scenario, I was unable to locate the plea to parse the script, which it's different from location of REPL.