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

[ts] Check if param is assignable when parsing arrow return type #13581

Merged
merged 3 commits into from Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion packages/babel-parser/src/parser/expression.js
Expand Up @@ -1544,7 +1544,7 @@ export default class ExpressionParser extends LValParser {
return parenExpression;
}

// eslint-disable-next-line no-unused-vars
// eslint-disable-next-line no-unused-vars -- `params` is used in typescript plugin
shouldParseArrow(params: Array<N.Node>): boolean {
return !this.canInsertSemicolon();
}
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-parser/src/parser/lval.js
Expand Up @@ -100,6 +100,7 @@ export default class LValParser extends NodeUtils {
case "ObjectPattern":
case "ArrayPattern":
case "AssignmentPattern":
case "RestElement":
break;

case "ObjectExpression":
Expand Down Expand Up @@ -235,6 +236,7 @@ export default class LValParser extends NodeUtils {
case "ObjectPattern":
case "ArrayPattern":
case "AssignmentPattern":
case "RestElement":
Copy link

Choose a reason for hiding this comment

The 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

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 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)

Copy link

Choose a reason for hiding this comment

The 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": {
Expand Down
@@ -0,0 +1 @@
0 ? v => (...rest) : v => 0 : v => 0;
@@ -0,0 +1,107 @@
{
"type": "File",
"start":0,"end":37,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":37}},
"program": {
"type": "Program",
"start":0,"end":37,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":37}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":37,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":37}},
"expression": {
"type": "ConditionalExpression",
"start":0,"end":36,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":36}},
"test": {
"type": "NumericLiteral",
"start":0,"end":1,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":1}},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
},
"consequent": {
"type": "ArrowFunctionExpression",
"start":4,"end":27,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":27}},
"id": null,
"generator": false,
"async": false,
"params": [
{
"type": "Identifier",
"start":4,"end":5,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":5},"identifierName":"v"},
"name": "v"
}
],
"body": {
"type": "ArrowFunctionExpression",
"start":9,"end":27,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":27}},
"returnType": {
"type": "TSTypeAnnotation",
"start":19,"end":22,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":22}},
"typeAnnotation": {
"type": "TSTypeReference",
"start":21,"end":22,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":22}},
"typeName": {
"type": "Identifier",
"start":21,"end":22,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":22},"identifierName":"v"},
"name": "v"
}
}
},
"id": null,
"generator": false,
"async": false,
"params": [
{
"type": "RestElement",
"start":10,"end":17,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":17}},
"argument": {
"type": "Identifier",
"start":13,"end":17,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":17},"identifierName":"rest"},
"name": "rest"
}
}
],
"body": {
"type": "NumericLiteral",
"start":26,"end":27,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":27}},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
}
},
"alternate": {
"type": "ArrowFunctionExpression",
"start":30,"end":36,"loc":{"start":{"line":1,"column":30},"end":{"line":1,"column":36}},
"id": null,
"generator": false,
"async": false,
"params": [
{
"type": "Identifier",
"start":30,"end":31,"loc":{"start":{"line":1,"column":30},"end":{"line":1,"column":31},"identifierName":"v"},
"name": "v"
}
],
"body": {
"type": "NumericLiteral",
"start":35,"end":36,"loc":{"start":{"line":1,"column":35},"end":{"line":1,"column":36}},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
}
}
}
],
"directives": []
}
}