[Bug]Same function declaration will cause SyntaxError
See original GitHub issueBug Report
- I would like to work on a fix!
Current behavior
See this
Input Code
function foo() {}
function foo() {}
Will get SyntaxError: Identifier 'foo' has already been declared
error
Expected behavior
No error. Because this is a valid javascript syntax and can be executed without any error thrown
Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)
See REPL above
Environment
REPL
- Babel version(s): REPL
- Node/npm version: REPL
- OS: Windows 10
- Monorepo: no
- How you are using Babel: REPL
Possible Solution
Additional context
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:9 (4 by maintainers)
Top Results From Across the Web
SyntaxError: function statement requires a name - JavaScript
The JavaScript exception "function statement requires a name" occurs when there is a function statement in the code that requires a name.
Read more >What is the "Function statement requires a name" syntax error?
The JavaScript exception SyntaxError - Function statement requires a name occurs when a function statement (or function declaration) requires a name but is ......
Read more >Structure member assignment causing syntax error when not ...
C code has to be inside a function. You can declare a global outside of a function but you can't write an assignment...
Read more >Functions - F# | Microsoft Learn
Non-recursive function definition. let [inline] function-name ... However, the compiler will attempt to make the function as generic as ...
Read more >Version 1 - History - Changes in Virtuemart 109 - Changes in ...
various fixes to make VirtueMart compatible to Joomla! ... number not checked on form submit, another bug, same reason: payment method can be...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That’s fair.
By the way, could we add more info when throw the error? For example, change “SyntaxError: Identifier ‘foo’ has already been declared” to “SyntaxError: Identifier ‘foo’ has already been declared, this is not allowed in a ESM”
You are saying that it’s possible for Babel to accept invalid code and still produce code that runs.
That’s true, but it’s out of scope for the project. Our goal is to align to the specification so that when gradually disabling Babel transform over time your code doesn’t suddenly start failing.
It’s not ok to silently shallow an error, because that error is mandated by the spec.
That said, you can pass the
errorRecovery: true
parser option which doesn’t throw for early errors: https://runkit.com/nicolo-ribaudo/babel-babel-12354