-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[babel 8] Recover from error when parsing <T>() => {}</T>
#14616
Merged
nicolo-ribaudo
merged 1 commit into
babel:main
from
nicolo-ribaudo:babel-8-recovery-arrow-like-jsx
May 31, 2022
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3233,10 +3233,12 @@ export default (superClass: Class<Parser>): Class<Parser> => | |
|
||
// Either way, we're looking at a '<': tt.jsxTagStart or relational. | ||
|
||
let typeParameters: ?N.TsTypeParameterDeclaration; | ||
let invalidSingleType: ?N.TsTypeParameter; | ||
state = state || this.state.clone(); | ||
// If the state was cloned in the JSX parsing branch above but there | ||
// have been any error in the tryParse call, this.state is set to state | ||
// so we still need to clone it. | ||
if (!state || state === this.state) state = this.state.clone(); | ||
|
||
let typeParameters: ?N.TsTypeParameterDeclaration; | ||
const arrow = this.tryParse(abort => { | ||
// This is similar to TypeScript's `tryParseParenthesizedArrowFunctionExpression`. | ||
typeParameters = this.tsParseTypeParameters(); | ||
|
@@ -3255,32 +3257,28 @@ export default (superClass: Class<Parser>): Class<Parser> => | |
} | ||
expr.typeParameters = typeParameters; | ||
|
||
// report error if single type parameter used without trailing comma. | ||
if ( | ||
this.hasPlugin("jsx") && | ||
expr.typeParameters.params.length === 1 && | ||
!expr.typeParameters.extra?.trailingComma | ||
) { | ||
const parameter = expr.typeParameters.params[0]; | ||
if (!parameter.constraint) { | ||
// A single type parameter must either have constraints | ||
// or a trailing comma, otherwise it's ambiguous with JSX. | ||
invalidSingleType = parameter; | ||
if (process.env.BABEL_8_BREAKING) { | ||
if ( | ||
this.hasPlugin("jsx") && | ||
expr.typeParameters.params.length === 1 && | ||
!expr.typeParameters.extra?.trailingComma | ||
) { | ||
// report error if single type parameter used without trailing comma. | ||
const parameter = expr.typeParameters.params[0]; | ||
if (!parameter.constraint) { | ||
// A single type parameter must either have constraints | ||
// or a trailing comma, otherwise it's ambiguous with JSX. | ||
this.raise(TSErrors.SingleTypeParameterWithoutTrailingComma, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved this error within the |
||
at: createPositionWithColumnOffset(parameter.loc.end, 1), | ||
typeParameterName: parameter.name.name, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return expr; | ||
}, state); | ||
|
||
if (process.env.BABEL_8_BREAKING) { | ||
if (invalidSingleType) { | ||
this.raise(TSErrors.SingleTypeParameterWithoutTrailingComma, { | ||
at: createPositionWithColumnOffset(invalidSingleType.loc.end, 1), | ||
typeParameterName: invalidSingleType.name.name, | ||
}); | ||
} | ||
} | ||
|
||
/*:: invariant(arrow.node != null) */ | ||
if (!arrow.error && !arrow.aborted) { | ||
// This error is reported outside of the this.tryParse call so that | ||
|
1 change: 1 addition & 0 deletions
1
packages/babel-parser/test/fixtures/typescript/tsx/invalid-gt-arrow-like/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 @@ | ||
<T>() => {}</T>; |
3 changes: 3 additions & 0 deletions
3
packages/babel-parser/test/fixtures/typescript/tsx/invalid-gt-arrow-like/options.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,3 @@ | ||
{ | ||
"BABEL_8_BREAKING": true | ||
} |
63 changes: 63 additions & 0 deletions
63
packages/babel-parser/test/fixtures/typescript/tsx/invalid-gt-arrow-like/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,63 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":16,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":16,"index":16}}, | ||
"errors": [ | ||
"SyntaxError: Unexpected token `>`. Did you mean `>` or `{'>'}`? (1:7)" | ||
], | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":16,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":16,"index":16}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "ExpressionStatement", | ||
"start":0,"end":16,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":16,"index":16}}, | ||
"expression": { | ||
"type": "JSXElement", | ||
"start":0,"end":15,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":15,"index":15}}, | ||
"openingElement": { | ||
"type": "JSXOpeningElement", | ||
"start":0,"end":3,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":3,"index":3}}, | ||
"name": { | ||
"type": "JSXIdentifier", | ||
"start":1,"end":2,"loc":{"start":{"line":1,"column":1,"index":1},"end":{"line":1,"column":2,"index":2}}, | ||
"name": "T" | ||
}, | ||
"attributes": [], | ||
"selfClosing": false | ||
}, | ||
"closingElement": { | ||
"type": "JSXClosingElement", | ||
"start":11,"end":15,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":15,"index":15}}, | ||
"name": { | ||
"type": "JSXIdentifier", | ||
"start":13,"end":14,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":14,"index":14}}, | ||
"name": "T" | ||
} | ||
}, | ||
"children": [ | ||
{ | ||
"type": "JSXText", | ||
"start":3,"end":9,"loc":{"start":{"line":1,"column":3,"index":3},"end":{"line":1,"column":9,"index":9}}, | ||
"extra": { | ||
"rawValue": "() => ", | ||
"raw": "() => " | ||
}, | ||
"value": "() => " | ||
}, | ||
{ | ||
"type": "JSXExpressionContainer", | ||
"start":9,"end":11,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":11,"index":11}}, | ||
"expression": { | ||
"type": "JSXEmptyExpression", | ||
"start":10,"end":10,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":10,"index":10}} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"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.
This change is because the below
tryParse
call should always receive a clone ofthis.state
, and notthis.state
itself.