Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Commit

Permalink
Fix parsing of private fields
Browse files Browse the repository at this point in the history
The computed key is not part of the spec.
key for ClassProperties is an Expression
Do not parse computed and literal keys for PrivateClassProperties
  • Loading branch information
danez committed Jun 5, 2017
1 parent e579109 commit 9026b8e
Show file tree
Hide file tree
Showing 16 changed files with 28 additions and 1,918 deletions.
2 changes: 1 addition & 1 deletion ast/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ interface ClassMethod <: Function {
```js
interface ClassProperty <: Node {
type: "ClassProperty";
key: Identifier;
key: Expression;
value: Expression;
computed: boolean;
}
Expand Down
5 changes: 2 additions & 3 deletions src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -970,20 +970,19 @@ export default class ExpressionParser extends LValParser {
if (!node) this.unexpected();
}

parsePropertyName(prop: N.ObjectOrClassMember): N.Identifier {
parsePropertyName(prop: N.ObjectOrClassMember): N.Expression {
if (this.eat(tt.bracketL)) {
// $FlowFixMe (ClassPrivateMember shouldn't be allowed to be computed!)
prop.computed = true;
prop.key = this.parseMaybeAssign();
this.expect(tt.bracketR);
} else {
// $FlowFixMe (ClassPrivateMember shouldn't be allowed to be computed!)
prop.computed = false;
const oldInPropertyName = this.state.inPropertyName;
this.state.inPropertyName = true;
prop.key = (this.match(tt.num) || this.match(tt.string)) ? this.parseExprAtom() : this.parseIdentifier(true);
this.state.inPropertyName = oldInPropertyName;
}

return prop.key;
}

Expand Down
5 changes: 1 addition & 4 deletions src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,9 +642,7 @@ export default class StatementParser extends ExpressionParser {

isNonstaticConstructor(method: N.ClassMethod | N.ClassProperty): boolean {
return !method.computed && !method.static && (
// $FlowFixMe ('key' downcasting)
(method.key.name === "constructor") || // Identifier
// $FlowFixMe ('key' downcasting)
(method.key.value === "constructor") // Literal
);
}
Expand Down Expand Up @@ -707,7 +705,7 @@ export default class StatementParser extends ExpressionParser {
if (this.hasPlugin("classPrivateProperties") && this.match(tt.hash)) { // Private property
this.next();
const privateProp: N.ClassPrivateProperty = memberAny;
this.parsePropertyName(privateProp);
privateProp.key = this.parseIdentifier(true);
classBody.body.push(this.parsePrivateClassProperty(privateProp));
return;
}
Expand Down Expand Up @@ -749,7 +747,6 @@ export default class StatementParser extends ExpressionParser {

const isSimple = this.match(tt.name);
const key = this.parsePropertyName(methodOrProp);
// $FlowFixMe ('key' downcasting)
if (!methodOrProp.computed && methodOrProp.static && (methodOrProp.key.name === "prototype" || methodOrProp.key.value === "prototype")) {
this.raise(methodOrProp.key.start, "Classes may not have static property named prototype");
}
Expand Down
1 change: 0 additions & 1 deletion src/plugins/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,6 @@ export default (superClass: Class<Parser>): Class<Parser> => class extends super
parsePropertyName(node: N.ObjectOrClassMember): N.Identifier {
const variance = this.flowParseVariance();
const key = super.parsePropertyName(node);
// $FlowFixMe (variance not defined on ClassPrivateProperty)
node.variance = variance;
return key;
}
Expand Down
6 changes: 3 additions & 3 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ export type ObjectExpression = NodeBase & {
properties: $ReadOnlyArray<ObjectProperty | ObjectMethod | SpreadElement>;
};

export type ObjectOrClassMember = ClassMethod | ClassProperty | ClassPrivateProperty | ObjectMember;
export type ObjectOrClassMember = ClassMethod | ClassProperty | ObjectMember;

export type ObjectMember = ObjectProperty | ObjectMethod;

Expand All @@ -359,7 +359,7 @@ export type ObjectProperty = ObjectMemberBase & {
shorthand: boolean;
};

export type ObjectMethod = ObjectMemberBase & MethodBase & {
export type ObjectMethod = ObjectMemberBase & MethodBase & {
type: "ObjectMethod";
kind: "get" | "set" | "method"; // Never "constructor"
};
Expand Down Expand Up @@ -579,7 +579,7 @@ export type ClassMethod = MethodBase & ClassMemberBase & {

export type ClassProperty = ClassMemberBase & {
type: "ClassProperty";
key: Identifier;
key: Expression;
value: ?Expression; // TODO: Not in spec that this is nullable.

typeAnnotation?: ?FlowTypeAnnotation; // TODO: Not in spec
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Foo {
#p = x
[#m] () {}
#[m] = 1
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"throws": "Unexpected token, expected ; (3:10)",
"throws": "Unexpected token (3:3)",
"plugins": [
"classProperties",
"classPrivateProperties"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Foo {
#"p" = x
#2 = y
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"throws": "Unexpected token (2:3)",
"plugins": [
"classProperties",
"classPrivateProperties"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Foo {
#p = x
#m () {}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"throws": "Unexpected token, expected ; (3:5)",
"plugins": ["classProperties", "classPrivateProperties"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@
"column": 4
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 15,
Expand Down Expand Up @@ -123,7 +122,6 @@
"column": 4
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 20,
Expand All @@ -149,4 +147,4 @@
],
"directives": []
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@
"column": 13
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 11,
Expand Down Expand Up @@ -123,7 +122,6 @@
"column": 17
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 15,
Expand Down Expand Up @@ -207,7 +205,6 @@
"column": 17
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 32,
Expand Down Expand Up @@ -260,7 +257,6 @@
"column": 25
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 40,
Expand Down Expand Up @@ -305,4 +301,4 @@
],
"directives": []
}
}
}

This file was deleted.

Loading

0 comments on commit 9026b8e

Please sign in to comment.