denoland / deno

A modern runtime for JavaScript and TypeScript.

Home Page:https://deno.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

ploiu opened this issue · comments

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.

  • Can be replicated with regular deno run as well as the REPL
  • Can be replicated when using a number, string, boolean, or null as the parameter
  • Can not be replicated when using an object as the parameter (e.g. Infinity, [], {}, undefined, etc. all successfully create an anonymous function)
  • Can be replicated when mixing object and non-object parameters (e.g. (1, undefined) => 0 replicates this)
  • Can not be replicated if the parameter is not wrapped in parenthesis (e.g. 0 => 1 gives back parse error: Expected ';', '}' or <eof>)

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 :)

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

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

@marvinhagemeister
Is that the right respond?
image

@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.