From 283720d2523603426018f1f44c3c24df6c718efb Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Tue, 1 Nov 2022 00:22:09 +0800 Subject: [PATCH] fix: Parse re-declare var in class static body (#15102) fix --- packages/babel-parser/src/util/scopeflags.ts | 2 +- .../re-declare-var/input.js | 2 + .../re-declare-var/output.json | 102 ++++++++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 packages/babel-parser/test/fixtures/es2022/class-static-block/re-declare-var/input.js create mode 100644 packages/babel-parser/test/fixtures/es2022/class-static-block/re-declare-var/output.json diff --git a/packages/babel-parser/src/util/scopeflags.ts b/packages/babel-parser/src/util/scopeflags.ts index e62ac9d7e825..be4ccdc84eb4 100644 --- a/packages/babel-parser/src/util/scopeflags.ts +++ b/packages/babel-parser/src/util/scopeflags.ts @@ -10,7 +10,7 @@ export const SCOPE_OTHER = 0b000000000, SCOPE_CLASS = 0b001000000, SCOPE_STATIC_BLOCK = 0b010000000, SCOPE_TS_MODULE = 0b100000000, - SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_TS_MODULE; + SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_STATIC_BLOCK | SCOPE_TS_MODULE; export type ScopeFlags = | typeof SCOPE_OTHER diff --git a/packages/babel-parser/test/fixtures/es2022/class-static-block/re-declare-var/input.js b/packages/babel-parser/test/fixtures/es2022/class-static-block/re-declare-var/input.js new file mode 100644 index 000000000000..3f4c266ffda3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2022/class-static-block/re-declare-var/input.js @@ -0,0 +1,2 @@ +const y = 1; +class x { static { var x = 42; var y; } } diff --git a/packages/babel-parser/test/fixtures/es2022/class-static-block/re-declare-var/output.json b/packages/babel-parser/test/fixtures/es2022/class-static-block/re-declare-var/output.json new file mode 100644 index 000000000000..9e63391e27b4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2022/class-static-block/re-declare-var/output.json @@ -0,0 +1,102 @@ +{ + "type": "File", + "start":0,"end":54,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":41,"index":54}}, + "program": { + "type": "Program", + "start":0,"end":54,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":41,"index":54}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start":0,"end":12,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":12,"index":12}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":6,"end":11,"loc":{"start":{"line":1,"column":6,"index":6},"end":{"line":1,"column":11,"index":11}}, + "id": { + "type": "Identifier", + "start":6,"end":7,"loc":{"start":{"line":1,"column":6,"index":6},"end":{"line":1,"column":7,"index":7},"identifierName":"y"}, + "name": "y" + }, + "init": { + "type": "NumericLiteral", + "start":10,"end":11,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":11,"index":11}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + ], + "kind": "const" + }, + { + "type": "ClassDeclaration", + "start":13,"end":54,"loc":{"start":{"line":2,"column":0,"index":13},"end":{"line":2,"column":41,"index":54}}, + "id": { + "type": "Identifier", + "start":19,"end":20,"loc":{"start":{"line":2,"column":6,"index":19},"end":{"line":2,"column":7,"index":20},"identifierName":"x"}, + "name": "x" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":21,"end":54,"loc":{"start":{"line":2,"column":8,"index":21},"end":{"line":2,"column":41,"index":54}}, + "body": [ + { + "type": "StaticBlock", + "start":23,"end":52,"loc":{"start":{"line":2,"column":10,"index":23},"end":{"line":2,"column":39,"index":52}}, + "body": [ + { + "type": "VariableDeclaration", + "start":32,"end":43,"loc":{"start":{"line":2,"column":19,"index":32},"end":{"line":2,"column":30,"index":43}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":36,"end":42,"loc":{"start":{"line":2,"column":23,"index":36},"end":{"line":2,"column":29,"index":42}}, + "id": { + "type": "Identifier", + "start":36,"end":37,"loc":{"start":{"line":2,"column":23,"index":36},"end":{"line":2,"column":24,"index":37},"identifierName":"x"}, + "name": "x" + }, + "init": { + "type": "NumericLiteral", + "start":40,"end":42,"loc":{"start":{"line":2,"column":27,"index":40},"end":{"line":2,"column":29,"index":42}}, + "extra": { + "rawValue": 42, + "raw": "42" + }, + "value": 42 + } + } + ], + "kind": "var" + }, + { + "type": "VariableDeclaration", + "start":44,"end":50,"loc":{"start":{"line":2,"column":31,"index":44},"end":{"line":2,"column":37,"index":50}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":48,"end":49,"loc":{"start":{"line":2,"column":35,"index":48},"end":{"line":2,"column":36,"index":49}}, + "id": { + "type": "Identifier", + "start":48,"end":49,"loc":{"start":{"line":2,"column":35,"index":48},"end":{"line":2,"column":36,"index":49},"identifierName":"y"}, + "name": "y" + }, + "init": null + } + ], + "kind": "var" + } + ] + } + ] + } + } + ], + "directives": [] + } +}