Merge pull request #609 from jridgewell/static-private

Add static private class field support
This commit is contained in:
Justin Ridgewell 2017-07-03 19:55:34 -04:00 committed by GitHub
commit f09eb3200f
11 changed files with 209 additions and 14 deletions

View File

@ -1056,6 +1056,7 @@ interface ClassProperty <: Node {
type: "ClassProperty";
key: Expression;
value: Expression;
static: boolean;
computed: boolean;
}
```
@ -1067,6 +1068,7 @@ interface ClassPrivateProperty <: Node {
type: "ClassPrivateProperty";
key: Identifier;
value: Expression;
static: boolean;
}
```

View File

@ -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'
@ -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);
}

View File

@ -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 &

View File

@ -106,6 +106,7 @@
},
"name": "x"
},
"static": false,
"value": null
},
{
@ -139,6 +140,7 @@
},
"name": "y"
},
"static": false,
"value": null
}
]
@ -147,4 +149,4 @@
],
"directives": []
}
}
}

View File

@ -106,6 +106,7 @@
},
"name": "x"
},
"static": false,
"value": null
},
{
@ -272,4 +273,4 @@
],
"directives": []
}
}
}

View File

@ -106,6 +106,7 @@
},
"name": "x"
},
"static": false,
"value": null
},
{
@ -139,6 +140,7 @@
},
"name": "y"
},
"static": false,
"value": null
}
]
@ -222,6 +224,7 @@
},
"name": "x"
},
"static": false,
"value": {
"type": "NumericLiteral",
"start": 36,
@ -274,6 +277,7 @@
},
"name": "y"
},
"static": false,
"value": {
"type": "NumericLiteral",
"start": 44,
@ -301,4 +305,4 @@
],
"directives": []
}
}
}

View File

@ -106,6 +106,7 @@
},
"name": "x"
},
"static": false,
"value": {
"type": "NumericLiteral",
"start": 23,
@ -158,6 +159,7 @@
},
"name": "y"
},
"static": false,
"value": {
"type": "NumericLiteral",
"start": 35,
@ -677,6 +679,7 @@
},
"name": "x"
},
"static": false,
"value": {
"type": "NumericLiteral",
"start": 151,
@ -729,6 +732,7 @@
},
"name": "y"
},
"static": false,
"value": {
"type": "NumericLiteral",
"start": 171,
@ -3350,4 +3354,4 @@
],
"directives": []
}
}
}

View File

@ -106,6 +106,7 @@
},
"name": "x"
},
"static": false,
"value": null
},
{
@ -139,6 +140,7 @@
},
"name": "y"
},
"static": false,
"value": null
},
{
@ -1621,4 +1623,4 @@
],
"directives": []
}
}
}

View File

@ -0,0 +1,4 @@
class A {
static #x;
static #y = 1;
}

View File

@ -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": []
}
}

View File

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