Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support auto accessors with TypeScript annotations #15209

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/babel-parser/src/plugins/typescript/index.ts
Expand Up @@ -3224,6 +3224,16 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
}

pushClassAccessorProperty(
classBody: Undone<N.ClassBody>,
prop: N.ClassAccessorProperty,
isPrivate: boolean,
) {
const typeAnnotation = this.tsTryParseTypeAnnotation();
if (typeAnnotation) prop.typeAnnotation = typeAnnotation;
super.pushClassAccessorProperty(classBody, prop, isPrivate);
}

declareClassPrivateMethodInScope(
node: N.ClassPrivateMethod | N.EstreeMethodDefinition | N.TSDeclareMethod,
kind: number,
Expand Down
@@ -0,0 +1,9 @@
abstract class Foo {
accessor prop: number = 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add more test cases? So we can cover cases like static, private, computed, etc.

static accessor prop2: number = 1;
accessor #prop3: number = 1;
accessor [prop4]: number = 1;
private accessor prop5: number = 1;
abstract accessor prop6: number;
declare accessor prop7: number;
}
@@ -0,0 +1,6 @@
{
"plugins": [
"typescript",
"decoratorAutoAccessors"
]
}
@@ -0,0 +1,217 @@
{
"type": "File",
"start":0,"end":258,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":9,"column":1,"index":258}},
"program": {
"type": "Program",
"start":0,"end":258,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":9,"column":1,"index":258}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":258,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":9,"column":1,"index":258}},
"abstract": true,
"id": {
"type": "Identifier",
"start":15,"end":18,"loc":{"start":{"line":1,"column":15,"index":15},"end":{"line":1,"column":18,"index":18},"identifierName":"Foo"},
"name": "Foo"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start":19,"end":258,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":9,"column":1,"index":258}},
"body": [
{
"type": "ClassAccessorProperty",
"start":23,"end":49,"loc":{"start":{"line":2,"column":2,"index":23},"end":{"line":2,"column":28,"index":49}},
"static": false,
"key": {
"type": "Identifier",
"start":32,"end":36,"loc":{"start":{"line":2,"column":11,"index":32},"end":{"line":2,"column":15,"index":36},"identifierName":"prop"},
"name": "prop"
},
"computed": false,
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":36,"end":44,"loc":{"start":{"line":2,"column":15,"index":36},"end":{"line":2,"column":23,"index":44}},
"typeAnnotation": {
"type": "TSNumberKeyword",
"start":38,"end":44,"loc":{"start":{"line":2,"column":17,"index":38},"end":{"line":2,"column":23,"index":44}}
}
},
"value": {
"type": "NumericLiteral",
"start":47,"end":48,"loc":{"start":{"line":2,"column":26,"index":47},"end":{"line":2,"column":27,"index":48}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
},
{
"type": "ClassAccessorProperty",
"start":52,"end":86,"loc":{"start":{"line":3,"column":2,"index":52},"end":{"line":3,"column":36,"index":86}},
"static": true,
"key": {
"type": "Identifier",
"start":68,"end":73,"loc":{"start":{"line":3,"column":18,"index":68},"end":{"line":3,"column":23,"index":73},"identifierName":"prop2"},
"name": "prop2"
},
"computed": false,
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":73,"end":81,"loc":{"start":{"line":3,"column":23,"index":73},"end":{"line":3,"column":31,"index":81}},
"typeAnnotation": {
"type": "TSNumberKeyword",
"start":75,"end":81,"loc":{"start":{"line":3,"column":25,"index":75},"end":{"line":3,"column":31,"index":81}}
}
},
"value": {
"type": "NumericLiteral",
"start":84,"end":85,"loc":{"start":{"line":3,"column":34,"index":84},"end":{"line":3,"column":35,"index":85}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
},
{
"type": "ClassAccessorProperty",
"start":89,"end":117,"loc":{"start":{"line":4,"column":2,"index":89},"end":{"line":4,"column":30,"index":117}},
"static": false,
"key": {
"type": "PrivateName",
"start":98,"end":104,"loc":{"start":{"line":4,"column":11,"index":98},"end":{"line":4,"column":17,"index":104}},
"id": {
"type": "Identifier",
"start":99,"end":104,"loc":{"start":{"line":4,"column":12,"index":99},"end":{"line":4,"column":17,"index":104},"identifierName":"prop3"},
"name": "prop3"
}
},
"computed": false,
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":104,"end":112,"loc":{"start":{"line":4,"column":17,"index":104},"end":{"line":4,"column":25,"index":112}},
"typeAnnotation": {
"type": "TSNumberKeyword",
"start":106,"end":112,"loc":{"start":{"line":4,"column":19,"index":106},"end":{"line":4,"column":25,"index":112}}
}
},
"value": {
"type": "NumericLiteral",
"start":115,"end":116,"loc":{"start":{"line":4,"column":28,"index":115},"end":{"line":4,"column":29,"index":116}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
},
{
"type": "ClassAccessorProperty",
"start":120,"end":149,"loc":{"start":{"line":5,"column":2,"index":120},"end":{"line":5,"column":31,"index":149}},
"static": false,
"key": {
"type": "Identifier",
"start":130,"end":135,"loc":{"start":{"line":5,"column":12,"index":130},"end":{"line":5,"column":17,"index":135},"identifierName":"prop4"},
"name": "prop4"
},
"computed": true,
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":136,"end":144,"loc":{"start":{"line":5,"column":18,"index":136},"end":{"line":5,"column":26,"index":144}},
"typeAnnotation": {
"type": "TSNumberKeyword",
"start":138,"end":144,"loc":{"start":{"line":5,"column":20,"index":138},"end":{"line":5,"column":26,"index":144}}
}
},
"value": {
"type": "NumericLiteral",
"start":147,"end":148,"loc":{"start":{"line":5,"column":29,"index":147},"end":{"line":5,"column":30,"index":148}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
},
{
"type": "ClassAccessorProperty",
"start":152,"end":187,"loc":{"start":{"line":6,"column":2,"index":152},"end":{"line":6,"column":37,"index":187}},
"accessibility": "private",
"static": false,
"key": {
"type": "Identifier",
"start":169,"end":174,"loc":{"start":{"line":6,"column":19,"index":169},"end":{"line":6,"column":24,"index":174},"identifierName":"prop5"},
"name": "prop5"
},
"computed": false,
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":174,"end":182,"loc":{"start":{"line":6,"column":24,"index":174},"end":{"line":6,"column":32,"index":182}},
"typeAnnotation": {
"type": "TSNumberKeyword",
"start":176,"end":182,"loc":{"start":{"line":6,"column":26,"index":176},"end":{"line":6,"column":32,"index":182}}
}
},
"value": {
"type": "NumericLiteral",
"start":185,"end":186,"loc":{"start":{"line":6,"column":35,"index":185},"end":{"line":6,"column":36,"index":186}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
},
{
"type": "ClassAccessorProperty",
"start":190,"end":222,"loc":{"start":{"line":7,"column":2,"index":190},"end":{"line":7,"column":34,"index":222}},
"abstract": true,
"static": false,
"key": {
"type": "Identifier",
"start":208,"end":213,"loc":{"start":{"line":7,"column":20,"index":208},"end":{"line":7,"column":25,"index":213},"identifierName":"prop6"},
"name": "prop6"
},
"computed": false,
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":213,"end":221,"loc":{"start":{"line":7,"column":25,"index":213},"end":{"line":7,"column":33,"index":221}},
"typeAnnotation": {
"type": "TSNumberKeyword",
"start":215,"end":221,"loc":{"start":{"line":7,"column":27,"index":215},"end":{"line":7,"column":33,"index":221}}
}
},
"value": null
},
{
"type": "ClassAccessorProperty",
"start":225,"end":256,"loc":{"start":{"line":8,"column":2,"index":225},"end":{"line":8,"column":33,"index":256}},
"declare": true,
"static": false,
"key": {
"type": "Identifier",
"start":242,"end":247,"loc":{"start":{"line":8,"column":19,"index":242},"end":{"line":8,"column":24,"index":247},"identifierName":"prop7"},
"name": "prop7"
},
"computed": false,
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":247,"end":255,"loc":{"start":{"line":8,"column":24,"index":247},"end":{"line":8,"column":32,"index":255}},
"typeAnnotation": {
"type": "TSNumberKeyword",
"start":249,"end":255,"loc":{"start":{"line":8,"column":26,"index":249},"end":{"line":8,"column":32,"index":255}}
}
},
"value": null
}
]
}
}
],
"directives": []
}
}
6 changes: 4 additions & 2 deletions packages/babel-plugin-transform-typescript/src/index.ts
Expand Up @@ -120,7 +120,8 @@ export default declare((api, opts: Options) => {
const classMemberVisitors = {
field(
path: NodePath<
(t.ClassPrivateProperty | t.ClassProperty) & ExtraNodeProps
(t.ClassPrivateProperty | t.ClassProperty | t.ClassAccessorProperty) &
ExtraNodeProps
>,
) {
const { node } = path;
Expand Down Expand Up @@ -525,7 +526,8 @@ export default declare((api, opts: Options) => {
}
} else if (
child.isClassProperty() ||
child.isClassPrivateProperty()
child.isClassPrivateProperty() ||
child.isClassAccessorProperty()
) {
classMemberVisitors.field(child);
}
Expand Down
@@ -0,0 +1,9 @@
abstract class Foo {
accessor prop: number = 1;
static accessor prop2: number = 1;
accessor #prop3: number = 1;
accessor [prop4]: number = 1;
private accessor prop5: number = 1;
abstract accessor prop6: number;
declare accessor prop7: number
}
@@ -0,0 +1,6 @@
{
"plugins": [["transform-typescript", { "allowDeclareFields": true }]],
"parserOpts": {
"plugins": ["decoratorAutoAccessors"]
}
}
@@ -0,0 +1,8 @@
class Foo {
accessor prop = 1;
static accessor prop2 = 1;
accessor #prop3 = 1;
accessor [prop4] = 1;
accessor prop5 = 1;
accessor prop6;
}