Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(parser): allow TS declare readonly fields with initializers (#14817)
- Loading branch information
Showing
5 changed files
with
207 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...r/test/fixtures/typescript/class/declare-readonly-field-initializer-w-annotation/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class A { | ||
declare readonly bar: string = "test"; | ||
declare baz: string = "test"; | ||
} |
92 changes: 92 additions & 0 deletions
92
...est/fixtures/typescript/class/declare-readonly-field-initializer-w-annotation/output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": [] | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...s/babel-parser/test/fixtures/typescript/class/declare-readonly-field-initializer/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class A { | ||
declare readonly bar = "test"; | ||
declare readonly foo = 1; | ||
declare readonly baz = a.b; | ||
} |
101 changes: 101 additions & 0 deletions
101
...abel-parser/test/fixtures/typescript/class/declare-readonly-field-initializer/output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": [] | ||
} | ||
} |