Skip to content

Commit

Permalink
[parser] Add support for private fields in TypeScript (#10483)
Browse files Browse the repository at this point in the history
* [parser] Add support for private fields in TypeScript

* Fix flow
  • Loading branch information
nicolo-ribaudo committed Oct 29, 2019
1 parent f1bc6c4 commit 1d1fab4
Show file tree
Hide file tree
Showing 17 changed files with 931 additions and 4 deletions.
33 changes: 31 additions & 2 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -1868,7 +1868,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}

parsePostMemberNameModifiers(
methodOrProp: N.ClassMethod | N.ClassProperty,
methodOrProp: N.ClassMethod | N.ClassProperty | N.ClassPrivateProperty,
): void {
const optional = this.eat(tt.question);
if (optional) methodOrProp.optional = true;
Expand Down Expand Up @@ -2007,16 +2007,45 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (typeParameters) node.typeParameters = typeParameters;
}

parseClassProperty(node: N.ClassProperty): N.ClassProperty {
parseClassPropertyAnnotation(
node: N.ClassProperty | N.ClassPrivateProperty,
): void {
if (!node.optional && this.eat(tt.bang)) {
node.definite = true;
}

const type = this.tsTryParseTypeAnnotation();
if (type) node.typeAnnotation = type;
}

parseClassProperty(node: N.ClassProperty): N.ClassProperty {
this.parseClassPropertyAnnotation(node);
return super.parseClassProperty(node);
}

parseClassPrivateProperty(
node: N.ClassPrivateProperty,
): N.ClassPrivateProperty {
// $FlowIgnore
if (node.abstract) {
this.raise(
node.start,
"Private elements cannot have the 'abstract' modifier.",
);
}

// $FlowIgnore
if (node.accessibility) {
this.raise(
node.start,
`Private elements cannot have an accessibility modifier ('${node.accessibility}')`,
);
}

this.parseClassPropertyAnnotation(node);
return super.parseClassPrivateProperty(node);
}

pushClassMethod(
classBody: N.ClassBody,
method: N.ClassMethod,
Expand Down
9 changes: 8 additions & 1 deletion packages/babel-parser/src/types.js
Expand Up @@ -762,7 +762,14 @@ export type ClassPrivateProperty = NodeBase & {
value: ?Expression, // TODO: Not in spec that this is nullable.
static: boolean,
computed: false,
typeAnnotation?: ?TypeAnnotation, // TODO: Not in spec

// Flow and Typescript
typeAnnotation?: ?TypeAnnotationBase,

// TypeScript only
optional?: true,
definite?: true,
readonly?: true,
};

export type OptClassDeclaration = ClassBase &
Expand Down
@@ -1,4 +1,4 @@
{
"sourceType": "module",
"plugins": ["typescript", "classProperties"]
"plugins": ["typescript", "classProperties", "classPrivateProperties"]
}
@@ -0,0 +1,3 @@
abstract class A {
abstract #a;
}
@@ -0,0 +1,3 @@
{
"throws": "Private elements cannot have the 'abstract' modifier. (2:2)"
}
@@ -0,0 +1,3 @@
class A {
private #a;
}
@@ -0,0 +1,3 @@
{
"throws": "Private elements cannot have an accessibility modifier ('private') (2:2)"
}
@@ -0,0 +1,3 @@
class A {
protected #a;
}
@@ -0,0 +1,3 @@
{
"throws": "Private elements cannot have an accessibility modifier ('protected') (2:2)"
}
@@ -0,0 +1,3 @@
class A {
public #a;
}
@@ -0,0 +1,3 @@
{
"throws": "Private elements cannot have an accessibility modifier ('public') (2:2)"
}
@@ -0,0 +1,4 @@
class A {
readonly #a;
readonly #b: string;
}
@@ -0,0 +1,215 @@
{
"type": "File",
"start": 0,
"end": 49,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 49,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 49,
"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": 49,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 4,
"column": 1
}
},
"body": [
{
"type": "ClassProperty",
"start": 12,
"end": 24,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 14
}
},
"readonly": true,
"static": false,
"key": {
"type": "PrivateName",
"start": 21,
"end": 23,
"loc": {
"start": {
"line": 2,
"column": 11
},
"end": {
"line": 2,
"column": 13
}
},
"id": {
"type": "Identifier",
"start": 22,
"end": 23,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 13
},
"identifierName": "a"
},
"name": "a"
}
},
"value": null
},
{
"type": "ClassProperty",
"start": 27,
"end": 47,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 22
}
},
"readonly": true,
"static": false,
"key": {
"type": "PrivateName",
"start": 36,
"end": 38,
"loc": {
"start": {
"line": 3,
"column": 11
},
"end": {
"line": 3,
"column": 13
}
},
"id": {
"type": "Identifier",
"start": 37,
"end": 38,
"loc": {
"start": {
"line": 3,
"column": 12
},
"end": {
"line": 3,
"column": 13
},
"identifierName": "b"
},
"name": "b"
}
},
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 38,
"end": 46,
"loc": {
"start": {
"line": 3,
"column": 13
},
"end": {
"line": 3,
"column": 21
}
},
"typeAnnotation": {
"type": "TSStringKeyword",
"start": 40,
"end": 46,
"loc": {
"start": {
"line": 3,
"column": 15
},
"end": {
"line": 3,
"column": 21
}
}
}
},
"value": null
}
]
}
}
],
"directives": []
}
}
@@ -0,0 +1,4 @@
class A {
static #x;
static #y: string;
}

0 comments on commit 1d1fab4

Please sign in to comment.