rescan gt/lt token after TsAsExpression is parsed (#11912)

* refactor: move inType checks to flow plugin

* polish: replace hardcoded char codes

* fix: rescan greater/less token after asExpression is parsed
This commit is contained in:
Huáng Jùnliàng
2020-08-04 17:00:21 -04:00
committed by GitHub
parent c29138fd72
commit a1eabb84ea
5 changed files with 136 additions and 30 deletions

View File

@@ -1,4 +1,6 @@
x as T;
x < y as boolean; // (x < y) as boolean;
x as boolean <= y; // (x as boolean) <= y;
x === 1 as number; // x === (1 as number);
x as any as T;
x as boolean ?? y; // (x as boolean) ?? y;

View File

@@ -1,9 +1,9 @@
{
"type": "File",
"start":0,"end":106,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":14}},
"start":0,"end":192,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":42}},
"program": {
"type": "Program",
"start":0,"end":106,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":14}},
"start":0,"end":192,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":42}},
"sourceType": "module",
"interpreter": null,
"body": [
@@ -70,27 +70,23 @@
"type": "BinaryExpression",
"start":49,"end":66,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":17}},
"left": {
"type": "Identifier",
"start":49,"end":50,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":1},"identifierName":"x"},
"name": "x"
},
"operator": "===",
"right": {
"type": "TSAsExpression",
"start":55,"end":66,"loc":{"start":{"line":3,"column":6},"end":{"line":3,"column":17}},
"start":49,"end":61,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":12}},
"expression": {
"type": "NumericLiteral",
"start":55,"end":56,"loc":{"start":{"line":3,"column":6},"end":{"line":3,"column":7}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
"type": "Identifier",
"start":49,"end":50,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":1},"identifierName":"x"},
"name": "x"
},
"typeAnnotation": {
"type": "TSNumberKeyword",
"start":60,"end":66,"loc":{"start":{"line":3,"column":11},"end":{"line":3,"column":17}}
"type": "TSBooleanKeyword",
"start":54,"end":61,"loc":{"start":{"line":3,"column":5},"end":{"line":3,"column":12}}
}
},
"operator": "<=",
"right": {
"type": "Identifier",
"start":65,"end":66,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":17},"identifierName":"y"},
"name": "y"
}
},
"leadingComments": [
@@ -103,36 +99,81 @@
"trailingComments": [
{
"type": "CommentLine",
"value": " x === (1 as number);",
"value": " (x as boolean) <= y;",
"start":68,"end":91,"loc":{"start":{"line":3,"column":19},"end":{"line":3,"column":42}}
}
]
},
{
"type": "ExpressionStatement",
"start":92,"end":106,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":14}},
"start":92,"end":110,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":18}},
"expression": {
"type": "BinaryExpression",
"start":92,"end":109,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":17}},
"left": {
"type": "Identifier",
"start":92,"end":93,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":1},"identifierName":"x"},
"name": "x"
},
"operator": "===",
"right": {
"type": "TSAsExpression",
"start":98,"end":109,"loc":{"start":{"line":4,"column":6},"end":{"line":4,"column":17}},
"expression": {
"type": "NumericLiteral",
"start":98,"end":99,"loc":{"start":{"line":4,"column":6},"end":{"line":4,"column":7}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
},
"typeAnnotation": {
"type": "TSNumberKeyword",
"start":103,"end":109,"loc":{"start":{"line":4,"column":11},"end":{"line":4,"column":17}}
}
}
},
"leadingComments": [
{
"type": "CommentLine",
"value": " (x as boolean) <= y;",
"start":68,"end":91,"loc":{"start":{"line":3,"column":19},"end":{"line":3,"column":42}}
}
],
"trailingComments": [
{
"type": "CommentLine",
"value": " x === (1 as number);",
"start":111,"end":134,"loc":{"start":{"line":4,"column":19},"end":{"line":4,"column":42}}
}
]
},
{
"type": "ExpressionStatement",
"start":135,"end":149,"loc":{"start":{"line":5,"column":0},"end":{"line":5,"column":14}},
"expression": {
"type": "TSAsExpression",
"start":92,"end":105,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":13}},
"start":135,"end":148,"loc":{"start":{"line":5,"column":0},"end":{"line":5,"column":13}},
"expression": {
"type": "TSAsExpression",
"start":92,"end":100,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":8}},
"start":135,"end":143,"loc":{"start":{"line":5,"column":0},"end":{"line":5,"column":8}},
"expression": {
"type": "Identifier",
"start":92,"end":93,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":1},"identifierName":"x"},
"start":135,"end":136,"loc":{"start":{"line":5,"column":0},"end":{"line":5,"column":1},"identifierName":"x"},
"name": "x"
},
"typeAnnotation": {
"type": "TSAnyKeyword",
"start":97,"end":100,"loc":{"start":{"line":4,"column":5},"end":{"line":4,"column":8}}
"start":140,"end":143,"loc":{"start":{"line":5,"column":5},"end":{"line":5,"column":8}}
}
},
"typeAnnotation": {
"type": "TSTypeReference",
"start":104,"end":105,"loc":{"start":{"line":4,"column":12},"end":{"line":4,"column":13}},
"start":147,"end":148,"loc":{"start":{"line":5,"column":12},"end":{"line":5,"column":13}},
"typeName": {
"type": "Identifier",
"start":104,"end":105,"loc":{"start":{"line":4,"column":12},"end":{"line":4,"column":13},"identifierName":"T"},
"start":147,"end":148,"loc":{"start":{"line":5,"column":12},"end":{"line":5,"column":13},"identifierName":"T"},
"name": "T"
}
}
@@ -141,7 +182,41 @@
{
"type": "CommentLine",
"value": " x === (1 as number);",
"start":68,"end":91,"loc":{"start":{"line":3,"column":19},"end":{"line":3,"column":42}}
"start":111,"end":134,"loc":{"start":{"line":4,"column":19},"end":{"line":4,"column":42}}
}
]
},
{
"type": "ExpressionStatement",
"start":150,"end":168,"loc":{"start":{"line":6,"column":0},"end":{"line":6,"column":18}},
"expression": {
"type": "LogicalExpression",
"start":150,"end":167,"loc":{"start":{"line":6,"column":0},"end":{"line":6,"column":17}},
"left": {
"type": "TSAsExpression",
"start":150,"end":162,"loc":{"start":{"line":6,"column":0},"end":{"line":6,"column":12}},
"expression": {
"type": "Identifier",
"start":150,"end":151,"loc":{"start":{"line":6,"column":0},"end":{"line":6,"column":1},"identifierName":"x"},
"name": "x"
},
"typeAnnotation": {
"type": "TSBooleanKeyword",
"start":155,"end":162,"loc":{"start":{"line":6,"column":5},"end":{"line":6,"column":12}}
}
},
"operator": "??",
"right": {
"type": "Identifier",
"start":166,"end":167,"loc":{"start":{"line":6,"column":16},"end":{"line":6,"column":17},"identifierName":"y"},
"name": "y"
}
},
"trailingComments": [
{
"type": "CommentLine",
"value": " (x as boolean) ?? y;",
"start":169,"end":192,"loc":{"start":{"line":6,"column":19},"end":{"line":6,"column":42}}
}
]
}
@@ -156,8 +231,18 @@
},
{
"type": "CommentLine",
"value": " x === (1 as number);",
"value": " (x as boolean) <= y;",
"start":68,"end":91,"loc":{"start":{"line":3,"column":19},"end":{"line":3,"column":42}}
},
{
"type": "CommentLine",
"value": " x === (1 as number);",
"start":111,"end":134,"loc":{"start":{"line":4,"column":19},"end":{"line":4,"column":42}}
},
{
"type": "CommentLine",
"value": " (x as boolean) ?? y;",
"start":169,"end":192,"loc":{"start":{"line":6,"column":19},"end":{"line":6,"column":42}}
}
]
}