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
Support TypeScript 4.3 get/set type members #13089
Merged
nicolo-ribaudo
merged 12 commits into
babel:feat-7.14.0/parser-updates
from
sosukesuzuki:ts4.3-setter-getter-types
Apr 12, 2021
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fdf2ff4
Add parsing support TS 4.3 get/set
sosukesuzuki 4adfa29
Update existing snapshots
sosukesuzuki 48d5f8e
Add Syntax Errors
sosukesuzuki 0ffbbd9
Add test cases for Syntax Errors
sosukesuzuki 7b51174
Add support in babel-types babel-generator
sosukesuzuki 190f015
Update packages/babel-parser/src/plugins/typescript/index.js
sosukesuzuki 81b97c6
Avoid lookahead
sosukesuzuki 4d57c56
Raise errror for invalid this parameters
sosukesuzuki f81ff17
Raise errors for optional parameters
sosukesuzuki 6a0c387
Raise error for rest parameters
sosukesuzuki fe00e0e
Update typescript allowlist
sosukesuzuki 64e389b
Update tests
sosukesuzuki File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
packages/babel-generator/test/fixtures/typescript/interface-method-kind/input.js
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 @@ | ||
interface Foo { | ||
get foo(); | ||
set bar(v); | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/babel-generator/test/fixtures/typescript/interface-method-kind/output.js
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 @@ | ||
interface Foo { | ||
get foo(); | ||
set bar(v); | ||
} |
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
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
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
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
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
6 changes: 6 additions & 0 deletions
6
packages/babel-parser/test/fixtures/typescript/interface/get-set-ambiguous/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,6 @@ | ||
interface Foo { | ||
get | ||
foo(): string; | ||
set | ||
bar(v); | ||
} |
86 changes: 86 additions & 0 deletions
86
packages/babel-parser/test/fixtures/typescript/interface/get-set-ambiguous/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,86 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}}, | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "TSInterfaceDeclaration", | ||
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13},"identifierName":"Foo"}, | ||
"name": "Foo" | ||
}, | ||
"body": { | ||
"type": "TSInterfaceBody", | ||
"start":14,"end":56,"loc":{"start":{"line":1,"column":14},"end":{"line":6,"column":1}}, | ||
"body": [ | ||
{ | ||
"type": "TSPropertySignature", | ||
"start":18,"end":21,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":5}}, | ||
"key": { | ||
"type": "Identifier", | ||
"start":18,"end":21,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":5},"identifierName":"get"}, | ||
"name": "get" | ||
}, | ||
"computed": false | ||
}, | ||
{ | ||
"type": "TSMethodSignature", | ||
"start":24,"end":38,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":16}}, | ||
"key": { | ||
"type": "Identifier", | ||
"start":24,"end":27,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":5},"identifierName":"foo"}, | ||
"name": "foo" | ||
}, | ||
"computed": false, | ||
"parameters": [], | ||
"typeAnnotation": { | ||
"type": "TSTypeAnnotation", | ||
"start":29,"end":37,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":15}}, | ||
"typeAnnotation": { | ||
"type": "TSStringKeyword", | ||
"start":31,"end":37,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":15}} | ||
} | ||
}, | ||
"kind": "method" | ||
}, | ||
{ | ||
"type": "TSPropertySignature", | ||
"start":41,"end":44,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":5}}, | ||
"key": { | ||
"type": "Identifier", | ||
"start":41,"end":44,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":5},"identifierName":"set"}, | ||
"name": "set" | ||
}, | ||
"computed": false | ||
}, | ||
{ | ||
"type": "TSMethodSignature", | ||
"start":47,"end":54,"loc":{"start":{"line":5,"column":2},"end":{"line":5,"column":9}}, | ||
"key": { | ||
"type": "Identifier", | ||
"start":47,"end":50,"loc":{"start":{"line":5,"column":2},"end":{"line":5,"column":5},"identifierName":"bar"}, | ||
"name": "bar" | ||
}, | ||
"computed": false, | ||
"parameters": [ | ||
{ | ||
"type": "Identifier", | ||
"start":51,"end":52,"loc":{"start":{"line":5,"column":6},"end":{"line":5,"column":7},"identifierName":"v"}, | ||
"name": "v" | ||
} | ||
], | ||
"kind": "method" | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...bel-parser/test/fixtures/typescript/interface/get-set-invalid-optional-parameter/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,3 @@ | ||
interface Foo { | ||
set bar(foo?: string); | ||
} |
58 changes: 58 additions & 0 deletions
58
...-parser/test/fixtures/typescript/interface/get-set-invalid-optional-parameter/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,58 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":42,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, | ||
"errors": [ | ||
"SyntaxError: A 'set' accessor cannot have an optional parameter. (3:1)" | ||
], | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":42,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "TSInterfaceDeclaration", | ||
"start":0,"end":42,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13},"identifierName":"Foo"}, | ||
"name": "Foo" | ||
}, | ||
"body": { | ||
"type": "TSInterfaceBody", | ||
"start":14,"end":42,"loc":{"start":{"line":1,"column":14},"end":{"line":3,"column":1}}, | ||
"body": [ | ||
{ | ||
"type": "TSMethodSignature", | ||
"start":18,"end":40,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":24}}, | ||
"key": { | ||
"type": "Identifier", | ||
"start":22,"end":25,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9},"identifierName":"bar"}, | ||
"name": "bar" | ||
}, | ||
"computed": false, | ||
"kind": "set", | ||
"parameters": [ | ||
{ | ||
"type": "Identifier", | ||
"start":26,"end":38,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":22},"identifierName":"foo"}, | ||
"name": "foo", | ||
"optional": true, | ||
"typeAnnotation": { | ||
"type": "TSTypeAnnotation", | ||
"start":30,"end":38,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":22}}, | ||
"typeAnnotation": { | ||
"type": "TSStringKeyword", | ||
"start":32,"end":38,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":22}} | ||
} | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/babel-parser/test/fixtures/typescript/interface/get-set-invalid-parameters/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 @@ | ||
interface Foo { | ||
get foo(param): string; | ||
set foo(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using
lookahead
(which is expensive) intsParseMethodSignatureKind
, can we:this.parsePropertyName
get
orset
:tsNextTokenCanFollowModifier
, then we are parsing an accessor and we callthis.parsePropertyName
againget
orset
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we parse class accessors using the same strategy mentioned above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What the AST babel should return for below code?
In current behaviour, two properties named
set
andfoo
are returned (with recoverable errorMissing semicolon
). If we should keep it, we should detect if the property is a method signature after firstthis.parsePropertyName
call. But then the token is the name of the property, so I don't know how to do that without lookahead...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's ok to throw "Expected (":
;
and that the name of the first one was exactlyset
.