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

[babel 8] Recover from error when parsing <T>() => {}</T> #14616

Merged
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
44 changes: 21 additions & 23 deletions packages/babel-parser/src/plugins/typescript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Member Author

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 of this.state, and not this.state itself.


let typeParameters: ?N.TsTypeParameterDeclaration;
const arrow = this.tryParse(abort => {
// This is similar to TypeScript's `tryParseParenthesizedArrowFunctionExpression`.
typeParameters = this.tsParseTypeParameters();
Expand All @@ -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, {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this error within the this.tryParse call, so that it know that there is a problem with the function. Otherwise, when parsing <T>() => {}</T> it would give the precedence to the arrow function because it thought that <T>() => {} could be pared without errors.

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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<T>() => {}</T>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": true
}
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 `&gt;` 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": []
}
}