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(parser): allow TS declare readonly fields with initializers #14817

Merged
merged 1 commit into from Jul 31, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/babel-parser/src/plugins/typescript/index.ts
Expand Up @@ -3118,7 +3118,11 @@ export default (superClass: {
parseClassProperty(node: N.ClassProperty): N.ClassProperty {
this.parseClassPropertyAnnotation(node);

if (this.state.isAmbientContext && this.match(tt.eq)) {
if (
this.state.isAmbientContext &&
!(node.readonly && !node.typeAnnotation) &&
this.match(tt.eq)
) {
this.raise(TSErrors.DeclareClassFieldHasInitializer, {
at: this.state.startLoc,
});
Expand Down
@@ -0,0 +1,4 @@
class A {
declare readonly bar: string = "test";
declare baz: string = "test";
}
@@ -0,0 +1,92 @@
{
"type": "File",
"start":0,"end":84,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":84}},
"errors": [
"SyntaxError: Initializers are not allowed in ambient contexts. (2:31)",
"SyntaxError: Initializers are not allowed in ambient contexts. (3:22)"
],
"program": {
"type": "Program",
"start":0,"end":84,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":84}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":84,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":84}},
"id": {
"type": "Identifier",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6,"index":6},"end":{"line":1,"column":7,"index":7},"identifierName":"A"},
"name": "A"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start":8,"end":84,"loc":{"start":{"line":1,"column":8,"index":8},"end":{"line":4,"column":1,"index":84}},
"body": [
{
"type": "ClassProperty",
"start":12,"end":50,"loc":{"start":{"line":2,"column":2,"index":12},"end":{"line":2,"column":40,"index":50}},
"declare": true,
"readonly": true,
"static": false,
"key": {
"type": "Identifier",
"start":29,"end":32,"loc":{"start":{"line":2,"column":19,"index":29},"end":{"line":2,"column":22,"index":32},"identifierName":"bar"},
"name": "bar"
},
"computed": false,
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":32,"end":40,"loc":{"start":{"line":2,"column":22,"index":32},"end":{"line":2,"column":30,"index":40}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":34,"end":40,"loc":{"start":{"line":2,"column":24,"index":34},"end":{"line":2,"column":30,"index":40}}
}
},
"value": {
"type": "StringLiteral",
"start":43,"end":49,"loc":{"start":{"line":2,"column":33,"index":43},"end":{"line":2,"column":39,"index":49}},
"extra": {
"rawValue": "test",
"raw": "\"test\""
},
"value": "test"
}
},
{
"type": "ClassProperty",
"start":53,"end":82,"loc":{"start":{"line":3,"column":2,"index":53},"end":{"line":3,"column":31,"index":82}},
"declare": true,
"static": false,
"key": {
"type": "Identifier",
"start":61,"end":64,"loc":{"start":{"line":3,"column":10,"index":61},"end":{"line":3,"column":13,"index":64},"identifierName":"baz"},
"name": "baz"
},
"computed": false,
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":64,"end":72,"loc":{"start":{"line":3,"column":13,"index":64},"end":{"line":3,"column":21,"index":72}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":66,"end":72,"loc":{"start":{"line":3,"column":15,"index":66},"end":{"line":3,"column":21,"index":72}}
}
},
"value": {
"type": "StringLiteral",
"start":75,"end":81,"loc":{"start":{"line":3,"column":24,"index":75},"end":{"line":3,"column":30,"index":81}},
"extra": {
"rawValue": "test",
"raw": "\"test\""
},
"value": "test"
}
}
]
}
}
],
"directives": []
}
}
@@ -0,0 +1,5 @@
class A {
declare readonly bar = "test";
declare readonly foo = 1;
declare readonly baz = a.b;
}
@@ -0,0 +1,101 @@
{
"type": "File",
"start":0,"end":102,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":1,"index":102}},
"program": {
"type": "Program",
"start":0,"end":102,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":1,"index":102}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":102,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":1,"index":102}},
"id": {
"type": "Identifier",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6,"index":6},"end":{"line":1,"column":7,"index":7},"identifierName":"A"},
"name": "A"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start":8,"end":102,"loc":{"start":{"line":1,"column":8,"index":8},"end":{"line":5,"column":1,"index":102}},
"body": [
{
"type": "ClassProperty",
"start":12,"end":42,"loc":{"start":{"line":2,"column":2,"index":12},"end":{"line":2,"column":32,"index":42}},
"declare": true,
"readonly": true,
"static": false,
"key": {
"type": "Identifier",
"start":29,"end":32,"loc":{"start":{"line":2,"column":19,"index":29},"end":{"line":2,"column":22,"index":32},"identifierName":"bar"},
"name": "bar"
},
"computed": false,
"value": {
"type": "StringLiteral",
"start":35,"end":41,"loc":{"start":{"line":2,"column":25,"index":35},"end":{"line":2,"column":31,"index":41}},
"extra": {
"rawValue": "test",
"raw": "\"test\""
},
"value": "test"
}
},
{
"type": "ClassProperty",
"start":45,"end":70,"loc":{"start":{"line":3,"column":2,"index":45},"end":{"line":3,"column":27,"index":70}},
"declare": true,
"readonly": true,
"static": false,
"key": {
"type": "Identifier",
"start":62,"end":65,"loc":{"start":{"line":3,"column":19,"index":62},"end":{"line":3,"column":22,"index":65},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"value": {
"type": "NumericLiteral",
"start":68,"end":69,"loc":{"start":{"line":3,"column":25,"index":68},"end":{"line":3,"column":26,"index":69}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
},
{
"type": "ClassProperty",
"start":73,"end":100,"loc":{"start":{"line":4,"column":2,"index":73},"end":{"line":4,"column":29,"index":100}},
"declare": true,
"readonly": true,
"static": false,
"key": {
"type": "Identifier",
"start":90,"end":93,"loc":{"start":{"line":4,"column":19,"index":90},"end":{"line":4,"column":22,"index":93},"identifierName":"baz"},
"name": "baz"
},
"computed": false,
"value": {
"type": "MemberExpression",
"start":96,"end":99,"loc":{"start":{"line":4,"column":25,"index":96},"end":{"line":4,"column":28,"index":99}},
"object": {
"type": "Identifier",
"start":96,"end":97,"loc":{"start":{"line":4,"column":25,"index":96},"end":{"line":4,"column":26,"index":97},"identifierName":"a"},
"name": "a"
},
"computed": false,
"property": {
"type": "Identifier",
"start":98,"end":99,"loc":{"start":{"line":4,"column":27,"index":98},"end":{"line":4,"column":28,"index":99},"identifierName":"b"},
"name": "b"
}
}
}
]
}
}
],
"directives": []
}
}