diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 53026d24f664..7200b8d8a2cf 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -54,6 +54,7 @@ import { newAsyncArrowScope, newExpressionScope, } from "../util/expression-scope.js"; +import ClassScopeHandler from "../util/class-scope"; import { Errors } from "./error"; /*:: @@ -2680,6 +2681,8 @@ export default class ExpressionParser extends LValParser { this.prodParam.enter(paramFlags); const oldInModule = this.inModule; this.inModule = true; + const oldClassScope = this.classScope; + this.classScope = new ClassScopeHandler(this.raise.bind(this)); const program = this.startNode(); node.body = this.parseProgram(program, tt.braceR, "module"); this.scope.exit(); @@ -2689,6 +2692,7 @@ export default class ExpressionParser extends LValParser { this.eat(tt.braceR); this.state.labels = oldLabels; this.state.exportedIdentifiers = oldExportedIdentifiers; + this.classScope = oldClassScope; return this.finishNode(node, "ModuleExpression"); } } diff --git a/packages/babel-parser/test/fixtures/experimental/module-blocks/invalid-class-in-module-blocks/input.js b/packages/babel-parser/test/fixtures/experimental/module-blocks/invalid-class-in-module-blocks/input.js new file mode 100644 index 000000000000..04f0c7aab135 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-blocks/invalid-class-in-module-blocks/input.js @@ -0,0 +1,7 @@ +class B { + #p() { + module { + class C { [this.#p]; } + }; + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-blocks/invalid-class-in-module-blocks/options.json b/packages/babel-parser/test/fixtures/experimental/module-blocks/invalid-class-in-module-blocks/options.json new file mode 100644 index 000000000000..5a36d4e0d5af --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-blocks/invalid-class-in-module-blocks/options.json @@ -0,0 +1,9 @@ +{ + "sourceType": "module", + "plugins": [ + "moduleBlocks", + "classPrivateProperties", + "classPrivateMethods", + "classProperties" + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-blocks/invalid-class-in-module-blocks/output.json b/packages/babel-parser/test/fixtures/experimental/module-blocks/invalid-class-in-module-blocks/output.json new file mode 100644 index 000000000000..86b7e8c0c9e4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-blocks/invalid-class-in-module-blocks/output.json @@ -0,0 +1,116 @@ +{ + "type": "File", + "start":0,"end":73,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}}, + "errors": [ + "SyntaxError: Private name #p is not defined (4:22)" + ], + "program": { + "type": "Program", + "start":0,"end":73,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":73,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}}, + "id": { + "type": "Identifier", + "start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"B"}, + "name": "B" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":8,"end":73,"loc":{"start":{"line":1,"column":8},"end":{"line":7,"column":1}}, + "body": [ + { + "type": "ClassPrivateMethod", + "start":12,"end":71,"loc":{"start":{"line":2,"column":2},"end":{"line":6,"column":3}}, + "static": false, + "key": { + "type": "PrivateName", + "start":12,"end":14,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":4}}, + "id": { + "type": "Identifier", + "start":13,"end":14,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":4},"identifierName":"p"}, + "name": "p" + } + }, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":17,"end":71,"loc":{"start":{"line":2,"column":7},"end":{"line":6,"column":3}}, + "body": [ + { + "type": "ExpressionStatement", + "start":23,"end":67,"loc":{"start":{"line":3,"column":4},"end":{"line":5,"column":6}}, + "expression": { + "type": "ModuleExpression", + "start":23,"end":66,"loc":{"start":{"line":3,"column":4},"end":{"line":5,"column":5}}, + "body": { + "type": "Program", + "start":38,"end":66,"loc":{"start":{"line":4,"column":6},"end":{"line":5,"column":5}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":38,"end":60,"loc":{"start":{"line":4,"column":6},"end":{"line":4,"column":28}}, + "id": { + "type": "Identifier", + "start":44,"end":45,"loc":{"start":{"line":4,"column":12},"end":{"line":4,"column":13},"identifierName":"C"}, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":46,"end":60,"loc":{"start":{"line":4,"column":14},"end":{"line":4,"column":28}}, + "body": [ + { + "type": "ClassProperty", + "start":48,"end":58,"loc":{"start":{"line":4,"column":16},"end":{"line":4,"column":26}}, + "static": false, + "computed": true, + "key": { + "type": "MemberExpression", + "start":49,"end":56,"loc":{"start":{"line":4,"column":17},"end":{"line":4,"column":24}}, + "object": { + "type": "ThisExpression", + "start":49,"end":53,"loc":{"start":{"line":4,"column":17},"end":{"line":4,"column":21}} + }, + "computed": false, + "property": { + "type": "PrivateName", + "start":54,"end":56,"loc":{"start":{"line":4,"column":22},"end":{"line":4,"column":24}}, + "id": { + "type": "Identifier", + "start":55,"end":56,"loc":{"start":{"line":4,"column":23},"end":{"line":4,"column":24},"identifierName":"p"}, + "name": "p" + } + } + }, + "value": null + } + ] + } + } + ], + "directives": [] + } + } + } + ], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-class-in-module-blocks/input.js b/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-class-in-module-blocks/input.js new file mode 100644 index 000000000000..3ca87a589410 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-class-in-module-blocks/input.js @@ -0,0 +1,11 @@ +class B { + #p() { + module { + class C { + [this.#p]; + #p = 3; + } + }; + } +} + \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-class-in-module-blocks/options.json b/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-class-in-module-blocks/options.json new file mode 100644 index 000000000000..02815f98933b --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-class-in-module-blocks/options.json @@ -0,0 +1,9 @@ +{ + "sourceType": "module", + "plugins": [ + "moduleBlocks", + "classPrivateProperties", + "classPrivateMethods", + "classProperties" + ] +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-class-in-module-blocks/output.json b/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-class-in-module-blocks/output.json new file mode 100644 index 000000000000..8f9f2543972c --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-class-in-module-blocks/output.json @@ -0,0 +1,136 @@ +{ + "type": "File", + "start":0,"end":103,"loc":{"start":{"line":1,"column":0},"end":{"line":10,"column":1}}, + "program": { + "type": "Program", + "start":0,"end":103,"loc":{"start":{"line":1,"column":0},"end":{"line":10,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":103,"loc":{"start":{"line":1,"column":0},"end":{"line":10,"column":1}}, + "id": { + "type": "Identifier", + "start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"B"}, + "name": "B" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":8,"end":103,"loc":{"start":{"line":1,"column":8},"end":{"line":10,"column":1}}, + "body": [ + { + "type": "ClassPrivateMethod", + "start":12,"end":101,"loc":{"start":{"line":2,"column":2},"end":{"line":9,"column":3}}, + "static": false, + "key": { + "type": "PrivateName", + "start":12,"end":14,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":4}}, + "id": { + "type": "Identifier", + "start":13,"end":14,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":4},"identifierName":"p"}, + "name": "p" + } + }, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":17,"end":101,"loc":{"start":{"line":2,"column":7},"end":{"line":9,"column":3}}, + "body": [ + { + "type": "ExpressionStatement", + "start":23,"end":97,"loc":{"start":{"line":3,"column":4},"end":{"line":8,"column":6}}, + "expression": { + "type": "ModuleExpression", + "start":23,"end":96,"loc":{"start":{"line":3,"column":4},"end":{"line":8,"column":5}}, + "body": { + "type": "Program", + "start":38,"end":96,"loc":{"start":{"line":4,"column":6},"end":{"line":8,"column":5}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":38,"end":90,"loc":{"start":{"line":4,"column":6},"end":{"line":7,"column":7}}, + "id": { + "type": "Identifier", + "start":44,"end":45,"loc":{"start":{"line":4,"column":12},"end":{"line":4,"column":13},"identifierName":"C"}, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":46,"end":90,"loc":{"start":{"line":4,"column":14},"end":{"line":7,"column":7}}, + "body": [ + { + "type": "ClassProperty", + "start":56,"end":66,"loc":{"start":{"line":5,"column":8},"end":{"line":5,"column":18}}, + "static": false, + "computed": true, + "key": { + "type": "MemberExpression", + "start":57,"end":64,"loc":{"start":{"line":5,"column":9},"end":{"line":5,"column":16}}, + "object": { + "type": "ThisExpression", + "start":57,"end":61,"loc":{"start":{"line":5,"column":9},"end":{"line":5,"column":13}} + }, + "computed": false, + "property": { + "type": "PrivateName", + "start":62,"end":64,"loc":{"start":{"line":5,"column":14},"end":{"line":5,"column":16}}, + "id": { + "type": "Identifier", + "start":63,"end":64,"loc":{"start":{"line":5,"column":15},"end":{"line":5,"column":16},"identifierName":"p"}, + "name": "p" + } + } + }, + "value": null + }, + { + "type": "ClassPrivateProperty", + "start":75,"end":82,"loc":{"start":{"line":6,"column":8},"end":{"line":6,"column":15}}, + "static": false, + "key": { + "type": "PrivateName", + "start":75,"end":77,"loc":{"start":{"line":6,"column":8},"end":{"line":6,"column":10}}, + "id": { + "type": "Identifier", + "start":76,"end":77,"loc":{"start":{"line":6,"column":9},"end":{"line":6,"column":10},"identifierName":"p"}, + "name": "p" + } + }, + "value": { + "type": "NumericLiteral", + "start":80,"end":81,"loc":{"start":{"line":6,"column":13},"end":{"line":6,"column":14}}, + "extra": { + "rawValue": 3, + "raw": "3" + }, + "value": 3 + } + } + ] + } + } + ], + "directives": [] + } + } + } + ], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file