Modify grammar to support Private Fields proposal: (#260)

* Modify grammar to support Private Fields proposal:
- Adding optional plugin `classPrivateProperties`
- Adding PrivateName type identifier
- Adding ClassPrivateProperty to ClassBody
- Allow PrivateName in MemberExpression
- Allow PrivateName as a reference
- Adding tests

* Remove unnecesary liberal parameter

* Guarding for plugin dependecy for future versioning

* update spec.md [skip ci]

* move comment [skip ci]

* remove unused param [skip ci]

* Refactor PrivateName to contain Identifier in name property
This commit is contained in:
Diego Ferreiro Val 2017-05-22 11:33:48 -04:00 committed by Henry Zhu
parent 6c4acecf00
commit 01da62283c
26 changed files with 4148 additions and 5 deletions

View File

@ -3,6 +3,7 @@ These are the core Babylon AST node types.
- [Node objects](#node-objects) - [Node objects](#node-objects)
- [Changes](#changes) - [Changes](#changes)
- [Identifier](#identifier) - [Identifier](#identifier)
- [PrivateName](#privatename)
- [Literals](#literals) - [Literals](#literals)
- [RegExpLiteral](#regexpliteral) - [RegExpLiteral](#regexpliteral)
- [NullLiteral](#nullliteral) - [NullLiteral](#nullliteral)
@ -90,6 +91,7 @@ These are the core Babylon AST node types.
- [ClassBody](#classbody) - [ClassBody](#classbody)
- [ClassMethod](#classmethod) - [ClassMethod](#classmethod)
- [ClassProperty](#classproperty) - [ClassProperty](#classproperty)
- [ClassPrivateProperty](#classprivateproperty)
- [ClassDeclaration](#classdeclaration) - [ClassDeclaration](#classdeclaration)
- [ClassExpression](#classexpression) - [ClassExpression](#classexpression)
- [MetaProperty](#metaproperty) - [MetaProperty](#metaproperty)
@ -166,6 +168,18 @@ interface Identifier <: Expression, Pattern {
An identifier. Note that an identifier may be an expression or a destructuring pattern. An identifier. Note that an identifier may be an expression or a destructuring pattern.
# PrivateName
```js
interface PrivateName <: Expression, Pattern {
type: "PrivateName";
name: Identifier;
}
```
A Private Name Identifier.
# Literals # Literals
```js ```js
@ -1015,7 +1029,7 @@ interface Class <: Node {
```js ```js
interface ClassBody <: Node { interface ClassBody <: Node {
type: "ClassBody"; type: "ClassBody";
body: [ ClassMethod | ClassProperty ]; body: [ ClassMethod | ClassProperty | ClassPrivateProperty ];
} }
``` ```
@ -1043,6 +1057,16 @@ interface ClassProperty <: Node {
} }
``` ```
## ClassPrivateProperty
```js
interface ClassPrivateProperty <: Node {
type: "ClassPrivateProperty";
key: Identifier;
value: Expression;
}
```
## ClassDeclaration ## ClassDeclaration
```js ```js

View File

@ -99,7 +99,6 @@ export default class ExpressionParser extends LValParser {
parseMaybeAssign(noIn?: ?boolean, refShorthandDefaultPos?: ?Pos, afterLeftParse?: Function, refNeedsArrowPos?: ?Pos): N.Expression { parseMaybeAssign(noIn?: ?boolean, refShorthandDefaultPos?: ?Pos, afterLeftParse?: Function, refNeedsArrowPos?: ?Pos): N.Expression {
const startPos = this.state.start; const startPos = this.state.start;
const startLoc = this.state.startLoc; const startLoc = this.state.startLoc;
if (this.match(tt._yield) && this.state.inGenerator) { if (this.match(tt._yield) && this.state.inGenerator) {
let left = this.parseYield(); let left = this.parseYield();
if (afterLeftParse) left = afterLeftParse.call(this, left, startPos, startLoc); if (afterLeftParse) left = afterLeftParse.call(this, left, startPos, startLoc);
@ -297,7 +296,7 @@ export default class ExpressionParser extends LValParser {
} else if (this.eat(tt.dot)) { } else if (this.eat(tt.dot)) {
const node = this.startNodeAt(startPos, startLoc); const node = this.startNodeAt(startPos, startLoc);
node.object = base; node.object = base;
node.property = this.parseIdentifier(true); node.property = this.hasPlugin("classPrivateProperties") ? this.parseMaybePrivateName() : this.parseIdentifier(true);
node.computed = false; node.computed = false;
base = this.finishNode(node, "MemberExpression"); base = this.finishNode(node, "MemberExpression");
} else if (this.eat(tt.bracketL)) { } else if (this.eat(tt.bracketL)) {
@ -525,6 +524,13 @@ export default class ExpressionParser extends LValParser {
this.takeDecorators(node); this.takeDecorators(node);
return this.parseClass(node, false); return this.parseClass(node, false);
case tt.hash:
if (this.hasPlugin("classPrivateProperties")) {
return this.parseMaybePrivateName();
} else {
this.unexpected();
}
case tt._new: case tt._new:
return this.parseNew(); return this.parseNew();
@ -547,6 +553,18 @@ export default class ExpressionParser extends LValParser {
} }
} }
parseMaybePrivateName(): N.PrivateName | N.Identifier {
const isPrivate = this.eat(tt.hash);
if (isPrivate) {
const node = this.startNode();
node.name = this.parseIdentifier(true);
return this.finishNode(node, "PrivateName");
} else {
return this.parseIdentifier(true);
}
}
parseFunctionExpression(): N.FunctionExpression | N.MetaProperty { parseFunctionExpression(): N.FunctionExpression | N.MetaProperty {
const node = this.startNode(); const node = this.startNode();
const meta = this.parseIdentifier(true); const meta = this.parseIdentifier(true);

View File

@ -26,6 +26,7 @@ export default class LValParser extends NodeUtils {
if (node) { if (node) {
switch (node.type) { switch (node.type) {
case "Identifier": case "Identifier":
case "PrivateName":
case "ObjectPattern": case "ObjectPattern":
case "ArrayPattern": case "ArrayPattern":
case "AssignmentPattern": case "AssignmentPattern":
@ -227,6 +228,7 @@ export default class LValParser extends NodeUtils {
checkClashes: ?{ [key: string]: boolean }, checkClashes: ?{ [key: string]: boolean },
contextDescription: string): void { contextDescription: string): void {
switch (expr.type) { switch (expr.type) {
case "PrivateName":
case "Identifier": case "Identifier":
this.checkReservedWord(expr.name, expr.start, false, true); this.checkReservedWord(expr.name, expr.start, false, true);

View File

@ -650,6 +650,7 @@ export default class StatementParser extends ExpressionParser {
// class bodies are implicitly strict // class bodies are implicitly strict
const oldStrict = this.state.strict; const oldStrict = this.state.strict;
this.state.strict = true; this.state.strict = true;
this.state.inClass = true;
let hadConstructor = false; let hadConstructor = false;
let decorators = []; let decorators = [];
@ -680,6 +681,13 @@ export default class StatementParser extends ExpressionParser {
decorators = []; decorators = [];
} }
if (this.hasPlugin("classPrivateProperties") && this.match(tt.hash)) { // Private property
this.next();
this.parsePropertyName(method);
classBody.body.push(this.parsePrivateClassProperty(method));
continue;
}
method.static = false; method.static = false;
if (this.match(tt.name) && this.state.value === "static") { if (this.match(tt.name) && this.state.value === "static") {
const key = this.parseIdentifier(true); // eats 'static' const key = this.parseIdentifier(true); // eats 'static'
@ -774,9 +782,26 @@ export default class StatementParser extends ExpressionParser {
node.body = this.finishNode(classBody, "ClassBody"); node.body = this.finishNode(classBody, "ClassBody");
this.state.inClass = false;
this.state.strict = oldStrict; this.state.strict = oldStrict;
} }
parsePrivateClassProperty(node: N.ClassPrivateProperty): N.ClassPrivateProperty {
this.state.inClassProperty = true;
if (this.match(tt.eq)) {
this.next();
node.value = this.parseMaybeAssign();
} else {
node.value = null;
}
this.semicolon();
this.state.inClassProperty = false;
return this.finishNode(node, "ClassPrivateProperty");
}
parseClassProperty(node: N.ClassProperty): N.ClassProperty { parseClassProperty(node: N.ClassProperty): N.ClassProperty {
const hasPlugin = this.hasPlugin("classProperties"); const hasPlugin = this.hasPlugin("classProperties");
const noPluginMsg = "You can only use Class Properties when the 'classProperties' plugin is enabled."; const noPluginMsg = "You can only use Class Properties when the 'classProperties' plugin is enabled.";

View File

@ -405,8 +405,17 @@ export default class Tokenizer extends LocationParser {
getTokenFromCode(code: number): void { getTokenFromCode(code: number): void {
switch (code) { switch (code) {
case 35: // '#'
if (this.hasPlugin("classPrivateProperties") && this.state.inClass) {
++this.state.pos; return this.finishToken(tt.hash);
} else {
this.raise(this.state.pos, `Unexpected character '${codePointToString(code)}'`);
}
// The interpretation of a dot depends on whether it is followed // The interpretation of a dot depends on whether it is followed
// by a digit or another two dots. // by a digit or another two dots.
case 46: // '.' case 46: // '.'
return this.readToken_dot(); return this.readToken_dot();

View File

@ -24,6 +24,7 @@ export default class State {
this.inAsync = this.inAsync =
this.inPropertyName = this.inPropertyName =
this.inType = this.inType =
this.inClass =
this.inClassProperty = this.inClassProperty =
this.noAnonFunctionType = this.noAnonFunctionType =
false; false;
@ -81,6 +82,7 @@ export default class State {
noAnonFunctionType: boolean; noAnonFunctionType: boolean;
inPropertyName: boolean; inPropertyName: boolean;
inClassProperty: boolean; inClassProperty: boolean;
inClass: boolean;
// Labels in scope. // Labels in scope.
labels: Array<{ kind: ?("loop" | "switch"), statementStart?: number }>; labels: Array<{ kind: ?("loop" | "switch"), statementStart?: number }>;

View File

@ -108,6 +108,7 @@ export const types: { [name: string]: TokenType } = {
backQuote: new TokenType("`", { startsExpr }), backQuote: new TokenType("`", { startsExpr }),
dollarBraceL: new TokenType("${", { beforeExpr, startsExpr }), dollarBraceL: new TokenType("${", { beforeExpr, startsExpr }),
at: new TokenType("@"), at: new TokenType("@"),
hash: new TokenType("#"),
// Operators. These carry several kinds of properties to help the // Operators. These carry several kinds of properties to help the
// parser use them properly (the presence of these properties is // parser use them properly (the presence of these properties is

View File

@ -62,6 +62,13 @@ export type Identifier = PatternBase & {
__clone(): Identifier; __clone(): Identifier;
}; };
export type PrivateName = PatternBase & {
type: "PrivateName";
name: string;
__clone(): Identifier;
};
// Literals // Literals
export type Literal = RegExpLiteral | NullLiteral | StringLiteral | BooleanLiteral | NumericLiteral; export type Literal = RegExpLiteral | NullLiteral | StringLiteral | BooleanLiteral | NumericLiteral;
@ -334,7 +341,7 @@ export type ObjectExpression = NodeBase & {
properties: $ReadOnlyArray<ObjectProperty | ObjectMethod | SpreadElement>; properties: $ReadOnlyArray<ObjectProperty | ObjectMethod | SpreadElement>;
}; };
export type ObjectOrClassMember = ClassMethod | ClassProperty | ObjectMember; export type ObjectOrClassMember = ClassMethod | ClassProperty | ClassPrivateProperty | ObjectMember;
export type ObjectMember = ObjectProperty | ObjectMethod; export type ObjectMember = ObjectProperty | ObjectMethod;
@ -552,7 +559,7 @@ export type ClassMemberBase = NodeBase & HasDecorators & {
export type Accessibility = "public" | "protected" | "private"; export type Accessibility = "public" | "protected" | "private";
export type ClassMember = ClassMethod | ClassProperty; export type ClassMember = ClassMethod | ClassProperty | ClassPrivateProperty;
export type MethodLike = ObjectMethod | FunctionExpression | ClassMethod; export type MethodLike = ObjectMethod | FunctionExpression | ClassMethod;
@ -584,6 +591,18 @@ export type ClassProperty = ClassMemberBase & {
readonly?: true; readonly?: true;
}; };
export type ClassPrivateProperty = ClassMemberBase & {
type: "ClassPrivateProperty";
key: Identifier;
value: ?Expression; // TODO: Not in spec that this is nullable.
typeAnnotation?: ?FlowTypeAnnotation; // TODO: Not in spec
variance?: ?FlowVariance; // TODO: Not in spec
// TypeScript only: (TODO: Not in spec)
readonly?: true;
};
export type OptClassDeclaration = ClassBase & DeclarationBase & HasDecorators & { export type OptClassDeclaration = ClassBase & DeclarationBase & HasDecorators & {
type: "ClassDeclaration"; type: "ClassDeclaration";
// TypeScript only // TypeScript only

View File

@ -0,0 +1,4 @@
class Foo {
#p = x
[#m] () {}
}

View File

@ -0,0 +1,7 @@
{
"throws": "Unexpected token, expected ; (3:10)",
"plugins": [
"classProperties",
"classPrivateProperties"
]
}

View File

@ -0,0 +1,4 @@
class Foo {
#p = x
*#m () {}
}

View File

@ -0,0 +1,4 @@
{
"throws": "Unexpected token, expected ; (3:9)",
"plugins": ["classProperties", "classPrivateProperties"]
}

View File

@ -0,0 +1,3 @@
class Foo {
#x #y
}

View File

@ -0,0 +1,4 @@
{
"throws": "Unexpected token, expected ; (2:5)",
"plugins": ["classProperties", "classPrivateProperties"]
}

View File

@ -0,0 +1,4 @@
class Foo {
#x
#y
}

View File

@ -0,0 +1,152 @@
{
"type": "File",
"start": 0,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"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": 23,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 4,
"column": 1
}
},
"body": [
{
"type": "ClassPrivateProperty",
"start": 14,
"end": 16,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 4
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 15,
"end": 16,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 4
},
"identifierName": "x"
},
"name": "x"
},
"value": null
},
{
"type": "ClassPrivateProperty",
"start": 19,
"end": 21,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 4
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 20,
"end": 21,
"loc": {
"start": {
"line": 3,
"column": 3
},
"end": {
"line": 3,
"column": 4
},
"identifierName": "y"
},
"name": "y"
},
"value": null
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["classProperties", "classPrivateProperties"]
}

View File

@ -0,0 +1,3 @@
class A { #x; #y; }
class B { #x = 0; #y = 1; }

View File

@ -0,0 +1,308 @@
{
"type": "File",
"start": 0,
"end": 48,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 27
}
},
"program": {
"type": "Program",
"start": 0,
"end": 48,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 27
}
},
"sourceType": "script",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 19
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "A"
},
"name": "A"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 8,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 19
}
},
"body": [
{
"type": "ClassPrivateProperty",
"start": 10,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 13
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 11,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 12
},
"identifierName": "x"
},
"name": "x"
},
"value": null
},
{
"type": "ClassPrivateProperty",
"start": 14,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 14
},
"end": {
"line": 1,
"column": 17
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 15,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 16
},
"identifierName": "y"
},
"name": "y"
},
"value": null
}
]
}
},
{
"type": "ClassDeclaration",
"start": 21,
"end": 48,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 27
}
},
"id": {
"type": "Identifier",
"start": 27,
"end": 28,
"loc": {
"start": {
"line": 3,
"column": 6
},
"end": {
"line": 3,
"column": 7
},
"identifierName": "B"
},
"name": "B"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 29,
"end": 48,
"loc": {
"start": {
"line": 3,
"column": 8
},
"end": {
"line": 3,
"column": 27
}
},
"body": [
{
"type": "ClassPrivateProperty",
"start": 31,
"end": 38,
"loc": {
"start": {
"line": 3,
"column": 10
},
"end": {
"line": 3,
"column": 17
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 32,
"end": 33,
"loc": {
"start": {
"line": 3,
"column": 11
},
"end": {
"line": 3,
"column": 12
},
"identifierName": "x"
},
"name": "x"
},
"value": {
"type": "NumericLiteral",
"start": 36,
"end": 37,
"loc": {
"start": {
"line": 3,
"column": 15
},
"end": {
"line": 3,
"column": 16
}
},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
},
{
"type": "ClassPrivateProperty",
"start": 39,
"end": 46,
"loc": {
"start": {
"line": 3,
"column": 18
},
"end": {
"line": 3,
"column": 25
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 40,
"end": 41,
"loc": {
"start": {
"line": 3,
"column": 19
},
"end": {
"line": 3,
"column": 20
},
"identifierName": "y"
},
"name": "y"
},
"value": {
"type": "NumericLiteral",
"start": 44,
"end": 45,
"loc": {
"start": {
"line": 3,
"column": 23
},
"end": {
"line": 3,
"column": 24
}
},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["classProperties", "classPrivateProperties"]
}

View File

@ -0,0 +1,19 @@
class Point {
#x;
#y;
constructor(x = 0, y = 0) {
#x = +x;
#y = +y;
}
get x() { return this.#x }
set x(value) { this.#x = +value }
get y() { return this.#y }
set y(value) { this.#y = +value }
equals(p) { return this.#x === p.#x && this.#y === p.#y }
toString() { return `Point<${ this.#x },${ this.#y }>` }
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,3 @@
{
"plugins": ["classProperties", "classPrivateProperties"]
}

View File

@ -0,0 +1,19 @@
class Point {
#x;
#y;
constructor(x = 0, y = 0) {
#x = +x;
#y = +y;
}
get x() { return #x }
set x(value) { #x = +value }
get y() { return #y }
set y(value) { #y = +value }
equals(p) { return #x === p.#x && #y === p.#y }
toString() { return `Point<${ #x },${ #y }>` }
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,3 @@
{
"plugins": ["classProperties", "classPrivateProperties"]
}