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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: BABEL_PARSE_ERROR regarding duplicate function definitions #16303

Open
1 task
Roise-yue opened this issue Feb 26, 2024 · 4 comments
Open
1 task

[Bug]: BABEL_PARSE_ERROR regarding duplicate function definitions #16303

Roise-yue opened this issue Feb 26, 2024 · 4 comments

Comments

@Roise-yue
Copy link

馃捇

  • Would you like to work on a fix?

How are you using Babel?

@babel/cli

Input code

function* countDown(begin) {
while (begin >= 0) {
yield begin--;
}
}
var begin;
function* func1() {
yield 'aha';
yield 'ihi';
yield 'uhu';
}
for (const e of func1())
for (const e of countDown(10)) {
console.log(e);
}
function* countDown(begin) {
while (begin >= 0) {
yield begin--;
}
}
for (const e of countDown(10)) {
console.log(e);

}

Configuration file name

.babelrc

Configuration

{
"presets": ["@babel/preset-env"],
"plugins": []
}

Current and expected behavior

I am using babel to run the code,and here is the error reported:Syntax error,Identifier "CountDown" has already been declared.
To my knowledge, in JavaScript, when a function is declared repeatedly, the function declaration defined later will overwrite the previous function declaration. Therefore, I believe that the code I provided is grammatically compliant, but Babel refuses to translate this code. Is this because Babel believes there is a syntax error in this code? Please explain this issue to me, thank you

Environment

Babel:v7.23.9
node.js : v21.6.1
npm: v10.2.4
Ubuntu: v20.04

Possible solution

No response

Additional context

No response

@babel-bot
Copy link
Collaborator

Hey @Roise-yue! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.

@liuxingbaoyu
Copy link
Member

liuxingbaoyu commented Feb 26, 2024

Please check if the source code type is set to module, duplicate declarations are not allowed in strict mode.
repl

@Roise-yue
Copy link
Author

Please check if the source code type is set to module, duplicate declarations are not allowed in strict mode. repl

Thank you for your answer. After checking, I found that the code is not in strict mode, maybe there are other reasons.

@nicolo-ribaudo
Copy link
Member

Can you add "sourceType": "script" to your Babel config to actually force Babel to parse the file as a script and not as a module?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants