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

Add static private class field support #609

Merged
merged 1 commit into from
Jul 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ast/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,7 @@ interface ClassProperty <: Node {
type: "ClassProperty";
key: Expression;
value: Expression;
static: boolean;
computed: boolean;
}
```
Expand All @@ -1067,6 +1068,7 @@ interface ClassPrivateProperty <: Node {
type: "ClassPrivateProperty";
key: Identifier;
value: Expression;
static: boolean;
}
```

Expand Down
19 changes: 10 additions & 9 deletions src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -922,15 +922,6 @@ export default class StatementParser extends ExpressionParser {
const method: N.ClassMethod = memberAny;
const prop: N.ClassProperty = memberAny;

if (this.hasPlugin("classPrivateProperties") && this.match(tt.hash)) {
// Private property
this.next();
const privateProp: N.ClassPrivateProperty = memberAny;
privateProp.key = this.parseIdentifier(true);
classBody.body.push(this.parsePrivateClassProperty(privateProp));
return;
}

let isStatic = false;
if (this.match(tt.name) && this.state.value === "static") {
const key = this.parseIdentifier(true); // eats 'static'
Expand Down Expand Up @@ -960,6 +951,16 @@ export default class StatementParser extends ExpressionParser {
isStatic = true;
}

if (this.hasPlugin("classPrivateProperties") && this.match(tt.hash)) {
// Private property
this.next();
const privateProp: N.ClassPrivateProperty = memberAny;
privateProp.key = this.parseIdentifier(true);
privateProp.static = isStatic;
classBody.body.push(this.parsePrivateClassProperty(privateProp));
return;
}

this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
}

Expand Down
1 change: 1 addition & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ export type ClassPrivateProperty = NodeBase & {
type: "ClassPrivateProperty",
key: Identifier,
value: ?Expression, // TODO: Not in spec that this is nullable.
static: boolean,
};

export type OptClassDeclaration = ClassBase &
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
},
"name": "x"
},
"static": false,
"value": null
},
{
Expand Down Expand Up @@ -139,6 +140,7 @@
},
"name": "y"
},
"static": false,
"value": null
}
]
Expand All @@ -147,4 +149,4 @@
],
"directives": []
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
},
"name": "x"
},
"static": false,
"value": null
},
{
Expand Down Expand Up @@ -272,4 +273,4 @@
],
"directives": []
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
},
"name": "x"
},
"static": false,
"value": null
},
{
Expand Down Expand Up @@ -139,6 +140,7 @@
},
"name": "y"
},
"static": false,
"value": null
}
]
Expand Down Expand Up @@ -222,6 +224,7 @@
},
"name": "x"
},
"static": false,
"value": {
"type": "NumericLiteral",
"start": 36,
Expand Down Expand Up @@ -274,6 +277,7 @@
},
"name": "y"
},
"static": false,
"value": {
"type": "NumericLiteral",
"start": 44,
Expand Down Expand Up @@ -301,4 +305,4 @@
],
"directives": []
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
},
"name": "x"
},
"static": false,
"value": {
"type": "NumericLiteral",
"start": 23,
Expand Down Expand Up @@ -158,6 +159,7 @@
},
"name": "y"
},
"static": false,
"value": {
"type": "NumericLiteral",
"start": 35,
Expand Down Expand Up @@ -677,6 +679,7 @@
},
"name": "x"
},
"static": false,
"value": {
"type": "NumericLiteral",
"start": 151,
Expand Down Expand Up @@ -729,6 +732,7 @@
},
"name": "y"
},
"static": false,
"value": {
"type": "NumericLiteral",
"start": 171,
Expand Down Expand Up @@ -3350,4 +3354,4 @@
],
"directives": []
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
},
"name": "x"
},
"static": false,
"value": null
},
{
Expand Down Expand Up @@ -139,6 +140,7 @@
},
"name": "y"
},
"static": false,
"value": null
},
{
Expand Down Expand Up @@ -1621,4 +1623,4 @@
],
"directives": []
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class A {
static #x;
static #y = 1;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have comma-separated field declarations landed yet? If so, it'd be nice to have tests that demonstrate that you can mix static public and private field declarations in a comma-separated list.

Copy link
Contributor

@diervo diervo Jul 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR is pending to be merged: #608

Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
{
"type": "File",
"start": 0,
"end": 41,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 41,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 41,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"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": 41,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 4,
"column": 1
}
},
"body": [
{
"type": "ClassPrivateProperty",
"start": 12,
"end": 22,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 12
}
},
"key": {
"type": "Identifier",
"start": 20,
"end": 21,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 2,
"column": 11
},
"identifierName": "x"
},
"name": "x"
},
"static": true,
"value": null
},
{
"type": "ClassPrivateProperty",
"start": 25,
"end": 39,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 16
}
},
"key": {
"type": "Identifier",
"start": 33,
"end": 34,
"loc": {
"start": {
"line": 3,
"column": 10
},
"end": {
"line": 3,
"column": 11
},
"identifierName": "y"
},
"name": "y"
},
"static": true,
"value": {
"type": "NumericLiteral",
"start": 37,
"end": 38,
"loc": {
"start": {
"line": 3,
"column": 14
},
"end": {
"line": 3,
"column": 15
}
},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
]
}
}
],
"directives": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["classProperties", "classPrivateProperties"]
}