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

Incorrect compile error when having a 'const' inside a function expression of same name #6302

Closed
mor-n4 opened this Issue Sep 25, 2017 · 3 comments

Comments

Projects
None yet
5 participants
@mor-n4

mor-n4 commented Sep 25, 2017

This is a bug report.

Babel shows a compile-time error for the below snippet, even though it is valid ES2015.

Input Code

REPL

(function fn() {
  const fn = 42;
});

Babel Configuration (.babelrc, package.json, cli command)

{
  "presets": ["es2015"]
}

Expected Behavior

No compile error should occur, as the above code is valid ES2015 (tested with node.js).

Current Behavior

The following incorrect compile error is reported:

repl: "fn" is read-only
> 1 | (function fn() {
    |  ^
  2 |   const fn = 42;
  3 | });

Interestingly, the error disappears if either the 'const' is renamed, the function expression is renamed, or the outer parentheses are removed (i.e. function expression is turned into a declared function). So, to reproduce the bug, the function and 'const' must have the same name and the function has to be a function expression (not a function declaration).

Possible Solution

n/a

Context

n/a

Your Environment

software version(s)
Babel v6.26.0
Operating System Mac OS X
@babel-bot

This comment has been minimized.

Show comment
Hide comment
@babel-bot

babel-bot Sep 25, 2017

Collaborator

Hey @mor-n4! 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.

Collaborator

babel-bot commented Sep 25, 2017

Hey @mor-n4! 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.

@wcastand

This comment has been minimized.

Show comment
Hide comment
@wcastand

wcastand Oct 3, 2017

Contributor

Hi, can someone help write a test case for this bug. I tried to add in babel-traverse tests a fixture with the code. (actual.js, expected.js, option.json) but the tests are still passing.

I thought this was going to failed since it's supposed to throw an error.
I tried to make a typo in the test's code and the test throw and error so they are taken into account.

Maybe i don't have the right approach ?

Contributor

wcastand commented Oct 3, 2017

Hi, can someone help write a test case for this bug. I tried to add in babel-traverse tests a fixture with the code. (actual.js, expected.js, option.json) but the tests are still passing.

I thought this was going to failed since it's supposed to throw an error.
I tried to make a typo in the test's code and the test throw and error so they are taken into account.

Maybe i don't have the right approach ?

@loganfsmyth

This comment has been minimized.

Show comment
Hide comment
@loganfsmyth

loganfsmyth Mar 5, 2018

Member

This appears to be working in 7.x

Member

loganfsmyth commented Mar 5, 2018

This appears to be working in 7.x

@loganfsmyth loganfsmyth closed this Mar 5, 2018

@lock lock bot added the outdated label Jun 4, 2018

@lock lock bot locked as resolved and limited conversation to collaborators Jun 4, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.