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
Throw a syntax error for a declare function with a body #12054
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a212c3b
Throw a syntax error for a declare function with a body
sosukesuzuki a472fcd
Address review comment
sosukesuzuki 3471de1
Add isDeclareContext to state
sosukesuzuki c242660
Add test for declare module
sosukesuzuki f2ff434
Use super.parseFunctionBodyAndFinish
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
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
1 change: 1 addition & 0 deletions
1
packages/babel-parser/test/fixtures/typescript/declare/function/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 @@ | ||
declare function foo() {} |
35 changes: 35 additions & 0 deletions
35
packages/babel-parser/test/fixtures/typescript/declare/function/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,35 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":25,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":25}}, | ||
"errors": [ | ||
"SyntaxError: An implementation cannot be declared in ambient contexts. (1:0)" | ||
], | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":25,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":25}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "TSDeclareFunction", | ||
"start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}}, | ||
"declare": true, | ||
"id": { | ||
"type": "Identifier", | ||
"start":17,"end":20,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":20},"identifierName":"foo"}, | ||
"name": "foo" | ||
}, | ||
"generator": false, | ||
"async": false, | ||
"params": [] | ||
}, | ||
{ | ||
"type": "BlockStatement", | ||
"start":23,"end":25,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":25}}, | ||
"body": [], | ||
"directives": [] | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-parser/test/fixtures/typescript/declare/module/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 @@ | ||
declare module m { | ||
function foo() {} | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/babel-parser/test/fixtures/typescript/declare/module/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,50 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, | ||
"errors": [ | ||
"SyntaxError: An implementation cannot be declared in ambient contexts. (2:2)" | ||
], | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "TSModuleDeclaration", | ||
"start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":15,"end":16,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":16},"identifierName":"m"}, | ||
"name": "m" | ||
}, | ||
"body": { | ||
"type": "TSModuleBlock", | ||
"start":17,"end":40,"loc":{"start":{"line":1,"column":17},"end":{"line":3,"column":1}}, | ||
"body": [ | ||
{ | ||
"type": "FunctionDeclaration", | ||
"start":21,"end":38,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":19}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":30,"end":33,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":14},"identifierName":"foo"}, | ||
"name": "foo" | ||
}, | ||
"generator": false, | ||
"async": false, | ||
"params": [], | ||
"body": { | ||
"type": "BlockStatement", | ||
"start":36,"end":38,"loc":{"start":{"line":2,"column":17},"end":{"line":2,"column":19}}, | ||
"body": [], | ||
"directives": [] | ||
} | ||
} | ||
] | ||
}, | ||
"declare": true | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-parser/test/fixtures/typescript/declare/namespace/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 @@ | ||
declare namespace n { | ||
function foo() {} | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/babel-parser/test/fixtures/typescript/declare/namespace/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,50 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, | ||
"errors": [ | ||
"SyntaxError: An implementation cannot be declared in ambient contexts. (2:2)" | ||
], | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "TSModuleDeclaration", | ||
"start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":18,"end":19,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":19},"identifierName":"n"}, | ||
"name": "n" | ||
}, | ||
"body": { | ||
"type": "TSModuleBlock", | ||
"start":20,"end":43,"loc":{"start":{"line":1,"column":20},"end":{"line":3,"column":1}}, | ||
"body": [ | ||
{ | ||
"type": "FunctionDeclaration", | ||
"start":24,"end":41,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":19}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":33,"end":36,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":14},"identifierName":"foo"}, | ||
"name": "foo" | ||
}, | ||
"generator": false, | ||
"async": false, | ||
"params": [], | ||
"body": { | ||
"type": "BlockStatement", | ||
"start":39,"end":41,"loc":{"start":{"line":2,"column":17},"end":{"line":2,"column":19}}, | ||
"body": [], | ||
"directives": [] | ||
} | ||
} | ||
] | ||
}, | ||
"declare": true | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
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.
Question: Why do we need to special-case this, rather than just parsing the (invalid) function body?
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 does
special-case
mean?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.
I don't know if it's the same as what you're pointing out, I noticed a my mistake. I should have used
super.parseFunctionBodyAndFinish
.EDIT: I've fixed at f2ff434
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.
I think you can remove this whole
if
, sinceparseFunctionBodyAndFinish
would already be done anyway at the end of the function.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.
I got it. But I think we need this
if
to usebodilessType
as the function node type.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.
Ohhh I missed it 🙃