From bae5027844f9dc07927b1a826f5fb5d15ce6ffcf Mon Sep 17 00:00:00 2001 From: Yokubjon-J Date: Thu, 10 Mar 2022 23:46:03 +0500 Subject: [PATCH] Allow variable and function with the same name in static blocks (#14344) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 * 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 Co-authored-by: Nicolò Ribaudo --- packages/babel-parser/src/util/scope.js | 2 +- .../static-block-duplicate-name/input.js | 6 ++ .../static-block-duplicate-name/output.json | 70 +++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 packages/babel-parser/test/fixtures/es2022/class-private-names-duplicated/static-block-duplicate-name/input.js create mode 100644 packages/babel-parser/test/fixtures/es2022/class-private-names-duplicated/static-block-duplicate-name/output.json diff --git a/packages/babel-parser/src/util/scope.js b/packages/babel-parser/src/util/scope.js index 5547699ba23c..4f15035a1742 100644 --- a/packages/babel-parser/src/util/scope.js +++ b/packages/babel-parser/src/util/scope.js @@ -103,7 +103,7 @@ export default class ScopeHandler { // > treated like var declarations rather than like lexical declarations. treatFunctionsAsVarInScope(scope: IScope): boolean { return !!( - scope.flags & SCOPE_FUNCTION || + scope.flags & (SCOPE_FUNCTION | SCOPE_STATIC_BLOCK) || (!this.parser.inModule && scope.flags & SCOPE_PROGRAM) ); } diff --git a/packages/babel-parser/test/fixtures/es2022/class-private-names-duplicated/static-block-duplicate-name/input.js b/packages/babel-parser/test/fixtures/es2022/class-private-names-duplicated/static-block-duplicate-name/input.js new file mode 100644 index 000000000000..94af12032127 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2022/class-private-names-duplicated/static-block-duplicate-name/input.js @@ -0,0 +1,6 @@ +class X { + static { + var x; + function x() {} + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2022/class-private-names-duplicated/static-block-duplicate-name/output.json b/packages/babel-parser/test/fixtures/es2022/class-private-names-duplicated/static-block-duplicate-name/output.json new file mode 100644 index 000000000000..7c588fc92a2d --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2022/class-private-names-duplicated/static-block-duplicate-name/output.json @@ -0,0 +1,70 @@ +{ + "type": "File", + "start":0,"end":65,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":6,"column":1,"index":65}}, + "program": { + "type": "Program", + "start":0,"end":65,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":6,"column":1,"index":65}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":65,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":6,"column":1,"index":65}}, + "id": { + "type": "Identifier", + "start":6,"end":7,"loc":{"start":{"line":1,"column":6,"index":6},"end":{"line":1,"column":7,"index":7},"identifierName":"X"}, + "name": "X" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":8,"end":65,"loc":{"start":{"line":1,"column":8,"index":8},"end":{"line":6,"column":1,"index":65}}, + "body": [ + { + "type": "StaticBlock", + "start":14,"end":63,"loc":{"start":{"line":2,"column":4,"index":14},"end":{"line":5,"column":5,"index":63}}, + "body": [ + { + "type": "VariableDeclaration", + "start":29,"end":35,"loc":{"start":{"line":3,"column":6,"index":29},"end":{"line":3,"column":12,"index":35}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":33,"end":34,"loc":{"start":{"line":3,"column":10,"index":33},"end":{"line":3,"column":11,"index":34}}, + "id": { + "type": "Identifier", + "start":33,"end":34,"loc":{"start":{"line":3,"column":10,"index":33},"end":{"line":3,"column":11,"index":34},"identifierName":"x"}, + "name": "x" + }, + "init": null + } + ], + "kind": "var" + }, + { + "type": "FunctionDeclaration", + "start":42,"end":57,"loc":{"start":{"line":4,"column":6,"index":42},"end":{"line":4,"column":21,"index":57}}, + "id": { + "type": "Identifier", + "start":51,"end":52,"loc":{"start":{"line":4,"column":15,"index":51},"end":{"line":4,"column":16,"index":52},"identifierName":"x"}, + "name": "x" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":55,"end":57,"loc":{"start":{"line":4,"column":19,"index":55},"end":{"line":4,"column":21,"index":57}}, + "body": [], + "directives": [] + } + } + ] + } + ] + } + } + ], + "directives": [] + } +}