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
[ts] Check if param is assignable when parsing arrow return type #13581
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,7 +60,7 @@ export default class LValParser extends NodeUtils { | |
- RestElement is not the last element | ||
- Missing `=` in assignment pattern | ||
|
||
NOTE: There is a corresponding "isAssignable" method in flow.js. | ||
NOTE: There is a corresponding "isAssignable" method. | ||
When this one is updated, please check if also that one needs to be updated. | ||
|
||
* @param {Node} node The expression atom | ||
|
@@ -100,6 +100,7 @@ export default class LValParser extends NodeUtils { | |
case "ObjectPattern": | ||
case "ArrayPattern": | ||
case "AssignmentPattern": | ||
case "RestElement": | ||
break; | ||
|
||
case "ObjectExpression": | ||
|
@@ -229,6 +230,50 @@ export default class LValParser extends NodeUtils { | |
return exprList; | ||
} | ||
|
||
isAssignable(node: Node, isBinding?: boolean): boolean { | ||
switch (node.type) { | ||
case "Identifier": | ||
case "ObjectPattern": | ||
case "ArrayPattern": | ||
case "AssignmentPattern": | ||
case "RestElement": | ||
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. @nicolo-ribaudo What about a lookup table? This switch is slow because of 1) string comparison 2) many cases 3) hard to optimize in v8 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 don't think this switch relies on string comparisons: these strings are always constants in our code so V8 will atomize them and just compare them as pointers (but @JLHwung knows more than me about this) 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. v8 will convert this switch to a lookup, so it's faster if you do it. |
||
return true; | ||
|
||
case "ObjectExpression": { | ||
const last = node.properties.length - 1; | ||
return node.properties.every((prop, i) => { | ||
return ( | ||
prop.type !== "ObjectMethod" && | ||
(i === last || prop.type !== "SpreadElement") && | ||
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. The bug that caused the regression was that this was using
This comment was marked as resolved.
Sorry, something went wrong. 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. Why use 'every' ? It's slow vs a native for-loop |
||
this.isAssignable(prop) | ||
); | ||
}); | ||
} | ||
|
||
case "ObjectProperty": | ||
return this.isAssignable(node.value); | ||
|
||
case "SpreadElement": | ||
return this.isAssignable(node.argument); | ||
|
||
case "ArrayExpression": | ||
return node.elements.every(element => this.isAssignable(element)); | ||
|
||
case "AssignmentExpression": | ||
return node.operator === "="; | ||
|
||
case "ParenthesizedExpression": | ||
return this.isAssignable(node.expression); | ||
|
||
case "MemberExpression": | ||
case "OptionalMemberExpression": | ||
return !isBinding; | ||
|
||
default: | ||
return false; | ||
} | ||
} | ||
|
||
// Convert list of expression atoms to a list of | ||
|
||
toReferencedList( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// https://github.com/babel/babel/issues/11038 | ||
0 ? v => (sum += v) : v => 0; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":76,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":29}}, | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":76,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":29}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "ExpressionStatement", | ||
"start":47,"end":76,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":29}}, | ||
"leadingComments": [ | ||
{ | ||
"type": "CommentLine", | ||
"value": " https://github.com/babel/babel/issues/11038", | ||
"start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}} | ||
} | ||
], | ||
"expression": { | ||
"type": "ConditionalExpression", | ||
"start":47,"end":75,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":28}}, | ||
"test": { | ||
"type": "NumericLiteral", | ||
"start":47,"end":48,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":1}}, | ||
"extra": { | ||
"rawValue": 0, | ||
"raw": "0" | ||
}, | ||
"value": 0 | ||
}, | ||
"consequent": { | ||
"type": "ArrowFunctionExpression", | ||
"start":51,"end":66,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":19}}, | ||
"id": null, | ||
"generator": false, | ||
"async": false, | ||
"params": [ | ||
{ | ||
"type": "Identifier", | ||
"start":51,"end":52,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":5},"identifierName":"v"}, | ||
"name": "v" | ||
} | ||
], | ||
"body": { | ||
"type": "AssignmentExpression", | ||
"start":57,"end":65,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":18}}, | ||
"extra": { | ||
"parenthesized": true, | ||
"parenStart": 56 | ||
}, | ||
"operator": "+=", | ||
"left": { | ||
"type": "Identifier", | ||
"start":57,"end":60,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":13},"identifierName":"sum"}, | ||
"name": "sum" | ||
}, | ||
"right": { | ||
"type": "Identifier", | ||
"start":64,"end":65,"loc":{"start":{"line":2,"column":17},"end":{"line":2,"column":18},"identifierName":"v"}, | ||
"name": "v" | ||
} | ||
} | ||
}, | ||
"alternate": { | ||
"type": "ArrowFunctionExpression", | ||
"start":69,"end":75,"loc":{"start":{"line":2,"column":22},"end":{"line":2,"column":28}}, | ||
"id": null, | ||
"generator": false, | ||
"async": false, | ||
"params": [ | ||
{ | ||
"type": "Identifier", | ||
"start":69,"end":70,"loc":{"start":{"line":2,"column":22},"end":{"line":2,"column":23},"identifierName":"v"}, | ||
"name": "v" | ||
} | ||
], | ||
"body": { | ||
"type": "NumericLiteral", | ||
"start":74,"end":75,"loc":{"start":{"line":2,"column":27},"end":{"line":2,"column":28}}, | ||
"extra": { | ||
"rawValue": 0, | ||
"raw": "0" | ||
}, | ||
"value": 0 | ||
} | ||
} | ||
} | ||
} | ||
], | ||
"directives": [] | ||
}, | ||
"comments": [ | ||
{ | ||
"type": "CommentLine", | ||
"value": " https://github.com/babel/babel/issues/11038", | ||
"start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}} | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 ? v => (sum = v) : v => 0; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"throws": "Unexpected token, expected \":\" (1:27)" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 ? v => (sum = v) : v => 0 : v => 0; |
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 note can be removed then since it is moved to base parser.
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 actually like this comment: even if they are in the same file, it's still easy to miss
isAssignable
when updatingtoAssignable
.