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

fix(ts): parenthesized assert and assign #12933

Merged
merged 3 commits into from Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -2680,6 +2680,16 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return super.toAssignable(this.typeCastToParameter(node), isLHS);
case "TSParameterProperty":
return super.toAssignable(node, isLHS);
case "ParenthesizedExpression":
fedeci marked this conversation as resolved.
Show resolved Hide resolved
if (
node.expression.type === "TSAsExpression" ||
node.expression.type === "ParenthesizedExpression"
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess TSNonNullExpression and TSTypeAssertion should be here too

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I tried running some tests on those, but they work like magic😁

) {
node.expression = this.toAssignable(node.expression, isLHS);
return node;
} else {
return super.toAssignable(node, isLHS);
}
case "TSAsExpression":
case "TSNonNullExpression":
case "TSTypeAssertion":
Expand Down
@@ -0,0 +1 @@
({}) = x
@@ -0,0 +1,3 @@
{
"createParenthesizedExpressions": true
}
@@ -0,0 +1,39 @@
{
"type": "File",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}},
"errors": [
"SyntaxError: Invalid parenthesized assignment pattern (1:0)"
],
"program": {
"type": "Program",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}},
"expression": {
"type": "AssignmentExpression",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}},
"operator": "=",
"left": {
"type": "ParenthesizedExpression",
"start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}},
"expression": {
"type": "ObjectPattern",
"start":1,"end":3,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":3}},
"properties": []
}
},
"right": {
"type": "Identifier",
"start":7,"end":8,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":8},"identifierName":"x"},
"name": "x"
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
(a as any) = null
@@ -0,0 +1,3 @@
{
"createParenthesizedExpressions": true
}
@@ -0,0 +1,43 @@
{
"type": "File",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}},
"program": {
"type": "Program",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}},
"expression": {
"type": "AssignmentExpression",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}},
"operator": "=",
"left": {
"type": "ParenthesizedExpression",
"start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}},
"expression": {
"type": "TSAsExpression",
"start":1,"end":9,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":9}},
"expression": {
"type": "Identifier",
"start":1,"end":2,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":2},"identifierName":"a"},
"name": "a"
},
"typeAnnotation": {
"type": "TSAnyKeyword",
"start":6,"end":9,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":9}}
}
}
},
"right": {
"type": "NullLiteral",
"start":13,"end":17,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":17}}
}
}
}
],
"directives": []
}
}