Skip to content

Commit

Permalink
fix: throw expect privateIn when we see tt._in after private name
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Apr 28, 2020
1 parent cff6109 commit 3b282c4
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 10 deletions.
15 changes: 7 additions & 8 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -27,6 +27,7 @@ import {
isReservedWord,
isStrictReservedWord,
isStrictBindReservedWord,
isIdentifierStart,
} from "../util/identifier";
import type { Pos, Position } from "../util/location";
import * as charCodes from "charcodes";
Expand Down Expand Up @@ -1139,10 +1140,12 @@ export default class ExpressionParser extends LValParser {
return this.finishNode(node, "PipelinePrimaryTopicReference");
}

if (this.hasPlugin("privateIn")) {
if (isIdentifierStart(this.input.codePointAt(this.state.end))) {
node = (this.parseMaybePrivateName(true): N.PrivateName);
this.classScope.usePrivateName(node.id.name, node.start);
if (!this.match(tt._in)) {
if (this.match(tt._in)) {
this.expectPlugin("privateIn");
this.classScope.usePrivateName(node.id.name, node.start);
} else {
this.raise(
this.state.start,
Errors.PrivateInExpectedIn,
Expand Down Expand Up @@ -1171,11 +1174,7 @@ export default class ExpressionParser extends LValParser {
const isPrivate = this.match(tt.hash);

if (isPrivate) {
this.expectOnePlugin([
"classPrivateProperties",
"classPrivateMethods",
"privateIn",
]);
this.expectOnePlugin(["classPrivateProperties", "classPrivateMethods"]);
if (!isPrivateNameAllowed) {
this.raise(this.state.pos, Errors.UnexpectedPrivateField);
}
Expand Down
@@ -0,0 +1,7 @@
class Point {
#x = 1;
#y = 2;
static isPoint(obj) {
return #x in obj && #y in obj;
}
}
@@ -0,0 +1,6 @@
{
"plugins": [
"classPrivateProperties"
],
"throws": "This experimental syntax requires enabling the parser plugin: 'privateIn' (5:14)"
}
@@ -1,5 +1,5 @@
{
"throws": "Unexpected token (3:3)",
"throws": "Unexpected token, expected \";\" (3:9)",
"plugins": [
"classProperties",
"classPrivateMethods"
Expand Down
@@ -1,5 +1,4 @@
{
"throws": "Unexpected token (4:11)",
"plugins": [
"classPrivateProperties"
]
Expand Down
@@ -0,0 +1,89 @@
{
"type": "File",
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}},
"errors": [
"SyntaxError: Private names are only allowed in property accesses (`obj.#x`) or in `in` expressions (`#x in obj`) (4:13)"
],
"program": {
"type": "Program",
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}},
"id": {
"type": "Identifier",
"start":6,"end":9,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":9},"identifierName":"Foo"},
"name": "Foo"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start":10,"end":56,"loc":{"start":{"line":1,"column":10},"end":{"line":6,"column":1}},
"body": [
{
"type": "ClassPrivateProperty",
"start":14,"end":17,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":5}},
"static": false,
"key": {
"type": "PrivateName",
"start":14,"end":16,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":4}},
"id": {
"type": "Identifier",
"start":15,"end":16,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":4},"identifierName":"x"},
"name": "x"
}
},
"value": null
},
{
"type": "ClassMethod",
"start":20,"end":54,"loc":{"start":{"line":3,"column":2},"end":{"line":5,"column":3}},
"static": false,
"key": {
"type": "Identifier",
"start":20,"end":31,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":13},"identifierName":"constructor"},
"name": "constructor"
},
"computed": false,
"kind": "constructor",
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":34,"end":54,"loc":{"start":{"line":3,"column":16},"end":{"line":5,"column":3}},
"body": [
{
"type": "ExpressionStatement",
"start":40,"end":50,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":14}},
"expression": {
"type": "UnaryExpression",
"start":40,"end":49,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":13}},
"operator": "delete",
"prefix": true,
"argument": {
"type": "PrivateName",
"start":47,"end":49,"loc":{"start":{"line":4,"column":11},"end":{"line":4,"column":13}},
"id": {
"type": "Identifier",
"start":48,"end":49,"loc":{"start":{"line":4,"column":12},"end":{"line":4,"column":13},"identifierName":"x"},
"name": "x"
}
}
}
}
],
"directives": []
}
}
]
}
}
],
"directives": []
}
}

0 comments on commit 3b282c4

Please sign in to comment.