Skip to content

Commit

Permalink
Enter new class scope when parse module blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed Jan 27, 2021
1 parent c296d71 commit fa763c2
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -54,6 +54,7 @@ import {
newAsyncArrowScope,
newExpressionScope,
} from "../util/expression-scope.js";
import ClassScopeHandler from "../util/class-scope";
import { Errors } from "./error";

/*::
Expand Down Expand Up @@ -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<N.Program>();
node.body = this.parseProgram(program, tt.braceR, "module");
this.scope.exit();
Expand All @@ -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<N.ModuleExpression>(node, "ModuleExpression");
}
}
@@ -0,0 +1,7 @@
class B {
#p() {
module {
class C { [this.#p]; }
};
}
}
@@ -0,0 +1,9 @@
{
"sourceType": "module",
"plugins": [
"moduleBlocks",
"classPrivateProperties",
"classPrivateMethods",
"classProperties"
]
}
@@ -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": []
}
}
@@ -0,0 +1,11 @@
class B {
#p() {
module {
class C {
[this.#p];
#p = 3;
}
};
}
}

@@ -0,0 +1,9 @@
{
"sourceType": "module",
"plugins": [
"moduleBlocks",
"classPrivateProperties",
"classPrivateMethods",
"classProperties"
]
}
@@ -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": []
}
}

0 comments on commit fa763c2

Please sign in to comment.