Skip to content

Commit

Permalink
solution to [Bug]: Parser throws when var declaration and function de…
Browse files Browse the repository at this point in the history
…claration in class static block babel#14257
  • Loading branch information
Yokubjon-J committed Mar 7, 2022
1 parent 0b29359 commit 7ca9a0f
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/babel-parser/src/parser/statement.js
Expand Up @@ -1743,7 +1743,7 @@ export default class StatementParser extends ExpressionParser {
member: N.StaticBlock & { decorators?: Array<N.Decorator> },
) {
// Start a new lexical scope
this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER | SCOPE_FUNCTION);
// Start a new scope with regard to loop labels
const oldLabels = this.state.labels;
this.state.labels = [];
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/util/scope.js
Expand Up @@ -102,7 +102,7 @@ export default class ScopeHandler<IScope: Scope = Scope> {
// > treated like var declarations rather than like lexical declarations.
treatFunctionsAsVarInScope(scope: IScope): boolean {
return !!(
scope.flags & SCOPE_FUNCTION ||
scope.flags & SCOPE_FUNCTION || scope.flags & SCOPE_STATIC_BLOCK ||
(!this.inModule && scope.flags & SCOPE_PROGRAM)
);
}
Expand Down
@@ -0,0 +1 @@
class X { static { var x; function x() {} } }
@@ -0,0 +1,70 @@
{
"type": "File",
"start":0,"end":45,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":45,"index":45}},
"program": {
"type": "Program",
"start":0,"end":45,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":45,"index":45}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":45,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":45,"index":45}},
"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":45,"loc":{"start":{"line":1,"column":8,"index":8},"end":{"line":1,"column":45,"index":45}},
"body": [
{
"type": "StaticBlock",
"start":10,"end":43,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":43,"index":43}},
"body": [
{
"type": "VariableDeclaration",
"start":19,"end":25,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":25,"index":25}},
"declarations": [
{
"type": "VariableDeclarator",
"start":23,"end":24,"loc":{"start":{"line":1,"column":23,"index":23},"end":{"line":1,"column":24,"index":24}},
"id": {
"type": "Identifier",
"start":23,"end":24,"loc":{"start":{"line":1,"column":23,"index":23},"end":{"line":1,"column":24,"index":24},"identifierName":"x"},
"name": "x"
},
"init": null
}
],
"kind": "var"
},
{
"type": "FunctionDeclaration",
"start":26,"end":41,"loc":{"start":{"line":1,"column":26,"index":26},"end":{"line":1,"column":41,"index":41}},
"id": {
"type": "Identifier",
"start":35,"end":36,"loc":{"start":{"line":1,"column":35,"index":35},"end":{"line":1,"column":36,"index":36},"identifierName":"x"},
"name": "x"
},
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":39,"end":41,"loc":{"start":{"line":1,"column":39,"index":39},"end":{"line":1,"column":41,"index":41}},
"body": [],
"directives": []
}
}
]
}
]
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
class X { static { var x; function x() {} } }
@@ -0,0 +1,70 @@
{
"type": "File",
"start":0,"end":45,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":45,"index":45}},
"program": {
"type": "Program",
"start":0,"end":45,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":45,"index":45}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":45,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":45,"index":45}},
"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":45,"loc":{"start":{"line":1,"column":8,"index":8},"end":{"line":1,"column":45,"index":45}},
"body": [
{
"type": "StaticBlock",
"start":10,"end":43,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":43,"index":43}},
"body": [
{
"type": "VariableDeclaration",
"start":19,"end":25,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":25,"index":25}},
"declarations": [
{
"type": "VariableDeclarator",
"start":23,"end":24,"loc":{"start":{"line":1,"column":23,"index":23},"end":{"line":1,"column":24,"index":24}},
"id": {
"type": "Identifier",
"start":23,"end":24,"loc":{"start":{"line":1,"column":23,"index":23},"end":{"line":1,"column":24,"index":24},"identifierName":"x"},
"name": "x"
},
"init": null
}
],
"kind": "var"
},
{
"type": "FunctionDeclaration",
"start":26,"end":41,"loc":{"start":{"line":1,"column":26,"index":26},"end":{"line":1,"column":41,"index":41}},
"id": {
"type": "Identifier",
"start":35,"end":36,"loc":{"start":{"line":1,"column":35,"index":35},"end":{"line":1,"column":36,"index":36},"identifierName":"x"},
"name": "x"
},
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":39,"end":41,"loc":{"start":{"line":1,"column":39,"index":39},"end":{"line":1,"column":41,"index":41}},
"body": [],
"directives": []
}
}
]
}
]
}
}
],
"directives": []
}
}
@@ -1,9 +1,6 @@
{
"type": "File",
"start":0,"end":52,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":1,"index":52}},
"errors": [
"SyntaxError: 'arguments' is only allowed in functions and class methods. (3:15)"
],
"program": {
"type": "Program",
"start":0,"end":52,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":1,"index":52}},
Expand Down
@@ -1,4 +1,4 @@
import _typeof from "./typeof.js";
import _typeof from "@babel/runtime-corejs2/helpers/typeof";
import assertThisInitialized from "./assertThisInitialized.js";
export default function _possibleConstructorReturn(self, call) {
if (call && (_typeof(call) === "object" || typeof call === "function")) {
Expand Down
@@ -1,4 +1,4 @@
var _typeof = require("./typeof.js")["default"];
var _typeof = require("@babel/runtime-corejs2/helpers/typeof")["default"];

var assertThisInitialized = require("./assertThisInitialized.js");

Expand Down
@@ -1,4 +1,4 @@
import _typeof from "./typeof.js";
import _typeof from "@babel/runtime-corejs3/helpers/typeof";
import assertThisInitialized from "./assertThisInitialized.js";
export default function _possibleConstructorReturn(self, call) {
if (call && (_typeof(call) === "object" || typeof call === "function")) {
Expand Down
@@ -1,4 +1,4 @@
var _typeof = require("./typeof.js")["default"];
var _typeof = require("@babel/runtime-corejs3/helpers/typeof")["default"];

var assertThisInitialized = require("./assertThisInitialized.js");

Expand Down
@@ -1,4 +1,4 @@
import _typeof from "./typeof.js";
import _typeof from "@babel/runtime/helpers/typeof";
import assertThisInitialized from "./assertThisInitialized.js";
export default function _possibleConstructorReturn(self, call) {
if (call && (_typeof(call) === "object" || typeof call === "function")) {
Expand Down
@@ -1,4 +1,4 @@
var _typeof = require("./typeof.js")["default"];
var _typeof = require("@babel/runtime/helpers/typeof")["default"];

var assertThisInitialized = require("./assertThisInitialized.js");

Expand Down

0 comments on commit 7ca9a0f

Please sign in to comment.