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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Parser throws when var declaration and function declaration in class static block #14257

Closed
1 task done
overlookmotel opened this issue Feb 9, 2022 · 15 comments · Fixed by #14344
Closed
1 task done
Assignees
Labels
i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue Spec: Class Static Block

Comments

@overlookmotel
Copy link
Contributor

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

class X {
  static {
    var x;
    function x() {}
  }
}

REPL

Configuration file name

No response

Configuration

n/a

Current and expected behavior

I believe that the above code is legal. It happily runs in NodeJS.

Babel's parser throws an error Identifier 'x' has already been declared.

This only applies within a class static block. The following parses correctly (when sourceType: 'script'):

var x; function x() {}
class X {
  foo() { var x; function x() {} }
  static foo() { var x; function x() {} }
}
function f() { var x; function x() {} }
() => { var x; function x() {} };
function f2() { 'use strict'; var x; function x() {} }
() => { 'use strict'; var x; function x() {} };

REPL

Environment

System:
  OS: macOS 10.15.7
Binaries:
  Node: 16.14.0 - ~/.nvm/versions/node/v16.14.0/bin/node
  npm: 8.3.1 - ~/.nvm/versions/node/v16.14.0/bin/npm
npmPackages:
  @babel/core: ^7.17.2 => 7.17.2
  @babel/generator: ^7.17.0 => 7.17.0
  @babel/helper-module-transforms: ^7.16.7 => 7.16.7
  @babel/helper-plugin-utils: ^7.16.7 => 7.16.7
  @babel/parser: ^7.17.0 => 7.17.0
  @babel/plugin-transform-arrow-functions: ^7.16.7 => 7.16.7
  @babel/plugin-transform-modules-commonjs: ^7.16.8 => 7.16.8
  @babel/plugin-transform-react-jsx: ^7.16.7 => 7.16.7
  @babel/plugin-transform-strict-mode: ^7.16.7 => 7.16.7
  @babel/traverse: ^7.17.0 => 7.17.0
  @babel/types: ^7.17.0 => 7.17.0
  babel-plugin-dynamic-import-node: ^2.3.3 => 2.3.3
  eslint: ^7.32.0 => 7.32.0
  jest: ^27.5.1 => 27.5.1

Possible solution

No response

Additional context

Happy to work on a PR. But could someone please point in right direction? I'm not at all familiar with the parser.

@babel-bot
Copy link
Collaborator

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

@JLHwung
Copy link
Contributor

JLHwung commented Feb 9, 2022

The error is thrown from

this.raise(Errors.VarRedeclaration, { at: loc }, name);

When Babel is parsing a static block, it will push a SCOPE_STATIC_BLOCK

this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);

I think we should handle that in

treatFunctionsAsVarInScope(scope: IScope): boolean {

Alternatively, we can enable SCOPE_FUNCTION flag instead(I didn't try). Intuitively the static block is a function body so SCOPE_FUNCTION may be applied, too.

@nicolo-ribaudo
Copy link
Member

@overlookmotel I'm assigning this to you since you selected "Would you like to work on a fix?". If you are not interested anymore just leave a comment!

@Yokubjon-J
Copy link
Contributor

I would be happy to take on this issue @nicolo-ribaudo .

@overlookmotel
Copy link
Contributor Author

@Yokubjon-J I'd be very happy if you did! I was going to, but I'm unfamiliar with the parser code base, so will probably take me a while.

@Yokubjon-J
Copy link
Contributor

please assign me, @nicolo-ribaudo. i have started working on the issue.

@JLHwung JLHwung assigned Yokubjon-J and unassigned overlookmotel Mar 1, 2022
@Yokubjon-J
Copy link
Contributor

For some unknown reason, I am getting the error false != [object Object] in .ast after running test inside fixtures subdirectory (one in \babel-parser\test\fixtures\es2015\class\static-block\input.js another in \babel-parser\test\fixtures\static\class-static-method\input.js, shown in the picture below, their contents are not the same).
I think the test file directories are organized correctly, I would like to know your suggestions.
image

@nicolo-ribaudo
Copy link
Member

That might be a bug in our test runner; what does it say if you set the OVERWRITE=true environment variable when calling running out tests?

@Yokubjon-J
Copy link
Contributor

Thanks, it worked! I am back to the issue.

@Yokubjon-J
Copy link
Contributor

Yokubjon-J commented Mar 6, 2022

@nicolo-ribaudo when i write tests inside already created folders, they execute normally.
But after creating my own new folder inside fixtures, (\babel-parser\test\fixtures\static\class-static-block), even after setting OVERWRITE=true, i get the error Error: ENOENT: no such file or directory, open 'C:\Users\User\Desktop\Projects\babel\packages\babel-parser\test\fixtures\static\class-static-block\input.js\output.json'. Would you like to suggest something about it?
image

@Yokubjon-J
Copy link
Contributor

i am trying to commit changes but i am getting the error ESLint couldn't find the plugin "@babel/eslint-plugin-development. i tried installing it but it also throws an error npm ERR! Cannot read properties of null (reading 'package'). any suggestions?
image

@JLHwung
Copy link
Contributor

JLHwung commented Mar 7, 2022

@Yokubjon-J Please run yarn to install dependencies. If you are developing on Windows, you can also run make bootstrap in the embedded Linux, mingw64 may not be well supported since we don't run CI on mingw64 environment.

@Yokubjon-J
Copy link
Contributor

Yokubjon-J commented Mar 7, 2022

pull request 14336

nicolo-ribaudo pushed a commit to nicolo-ribaudo/babel that referenced this issue Mar 9, 2022
JLHwung added a commit that referenced this issue Mar 10, 2022
* solution to [Bug]: Parser throws when var declaration and function declaration in class static block #14257

* removed SCOPE_FUNCTION addition in statement.js (babel-parser)

* Update packages/babel-parser/src/util/scope.js

Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com>

* Undo unrelated changes

* test case in new subdir

* import typeof.js

* import typeof.js

* import typeof.js in babel-runtime

* import correct typeof.js in corejs2 and 3 in esm

* removed 2 unneeded tests from es2015

Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com>
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
@overlookmotel
Copy link
Contributor Author

@Yokubjon-J Just want to say thanks loads for fixing this.

@Yokubjon-J
Copy link
Contributor

@overlookmotel thank you also for letting me have this bug (to fix)!

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jun 10, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue Spec: Class Static Block
Projects
None yet
6 participants
@overlookmotel @JLHwung @nicolo-ribaudo @babel-bot @Yokubjon-J and others