Skip to content

Commit

Permalink
fix(ts): skip func-type param start on parsing (#14293)
Browse files Browse the repository at this point in the history
* fix(ts): skip func-type param start on parsing

Currently we skip the param start at tokenizing level, assuming `}` only matches `{` and `]` only matches `[`. However, as ES evolves such assumptions are no longer valid. Furthermore, we reinterpret `}` as template continuation in parseTemplateSubstitution, therefore, the skip param routine should be moved to parsing level instead of tokenizing.

* fix: tt.braceL is consumed in parseObjectLike
  • Loading branch information
JLHwung committed Feb 23, 2022
1 parent aa5ff36 commit a53c2fa
Show file tree
Hide file tree
Showing 30 changed files with 659 additions and 21 deletions.
41 changes: 20 additions & 21 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -1233,33 +1233,32 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}

if (this.match(tt.braceL)) {
let braceStackCounter = 1;
this.next();

while (braceStackCounter > 0) {
if (this.match(tt.braceL)) {
++braceStackCounter;
} else if (this.match(tt.braceR)) {
--braceStackCounter;
}
this.next();
// Return true if we can parse an object pattern without errors
const { errors } = this.state;
const previousErrorCount = errors.length;
try {
this.parseObjectLike(tt.braceR, true);
return errors.length === previousErrorCount;
} catch {
return false;
}
return true;
}

if (this.match(tt.bracketL)) {
let braceStackCounter = 1;
this.next();

while (braceStackCounter > 0) {
if (this.match(tt.bracketL)) {
++braceStackCounter;
} else if (this.match(tt.bracketR)) {
--braceStackCounter;
}
this.next();
// Return true if we can parse an array pattern without errors
const { errors } = this.state;
const previousErrorCount = errors.length;
try {
this.parseBindingList(
tt.bracketR,
charCodes.rightSquareBracket,
true,
);
return errors.length === previousErrorCount;
} catch {
return false;
}
return true;
}

return false;
Expand Down
@@ -0,0 +1 @@
type F = ([ x = #[0] ]) => {}
@@ -0,0 +1,4 @@
{
"sourceType": "module",
"plugins": ["typescript", ["recordAndTuple", { "syntaxType": "hash" }]]
}
@@ -0,0 +1,67 @@
{
"type": "File",
"start":0,"end":29,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":29,"index":29}},
"program": {
"type": "Program",
"start":0,"end":29,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":29,"index":29}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSTypeAliasDeclaration",
"start":0,"end":29,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":29,"index":29}},
"id": {
"type": "Identifier",
"start":5,"end":6,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":6,"index":6},"identifierName":"F"},
"name": "F"
},
"typeAnnotation": {
"type": "TSFunctionType",
"start":9,"end":29,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":29,"index":29}},
"parameters": [
{
"type": "ArrayPattern",
"start":10,"end":22,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":22,"index":22}},
"elements": [
{
"type": "AssignmentPattern",
"start":12,"end":20,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":20,"index":20}},
"left": {
"type": "Identifier",
"start":12,"end":13,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":13,"index":13},"identifierName":"x"},
"name": "x"
},
"right": {
"type": "TupleExpression",
"start":16,"end":20,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":20,"index":20}},
"elements": [
{
"type": "NumericLiteral",
"start":18,"end":19,"loc":{"start":{"line":1,"column":18,"index":18},"end":{"line":1,"column":19,"index":19}},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
]
}
}
]
}
],
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":24,"end":29,"loc":{"start":{"line":1,"column":24,"index":24},"end":{"line":1,"column":29,"index":29}},
"typeAnnotation": {
"type": "TSTypeLiteral",
"start":27,"end":29,"loc":{"start":{"line":1,"column":27,"index":27},"end":{"line":1,"column":29,"index":29}},
"members": []
}
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
type F = ([
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token (1:11)"
}
@@ -0,0 +1 @@
type F = ({
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token (1:11)"
}
@@ -0,0 +1 @@
type F = ({ x = #{} }) => {}
@@ -0,0 +1,12 @@
{
"sourceType": "module",
"plugins": [
"typescript",
[
"recordAndTuple",
{
"syntaxType": "hash"
}
]
]
}
@@ -0,0 +1,72 @@
{
"type": "File",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":28,"index":28}},
"program": {
"type": "Program",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":28,"index":28}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSTypeAliasDeclaration",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":28,"index":28}},
"id": {
"type": "Identifier",
"start":5,"end":6,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":6,"index":6},"identifierName":"F"},
"name": "F"
},
"typeAnnotation": {
"type": "TSFunctionType",
"start":9,"end":28,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":28,"index":28}},
"parameters": [
{
"type": "ObjectPattern",
"start":10,"end":21,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":21,"index":21}},
"properties": [
{
"type": "ObjectProperty",
"start":12,"end":19,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":19,"index":19}},
"key": {
"type": "Identifier",
"start":12,"end":13,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":13,"index":13},"identifierName":"x"},
"name": "x"
},
"computed": false,
"method": false,
"shorthand": true,
"value": {
"type": "AssignmentPattern",
"start":12,"end":19,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":19,"index":19}},
"left": {
"type": "Identifier",
"start":12,"end":13,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":13,"index":13},"identifierName":"x"},
"name": "x"
},
"right": {
"type": "RecordExpression",
"start":16,"end":19,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":19,"index":19}},
"properties": []
}
},
"extra": {
"shorthand": true
}
}
]
}
],
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":23,"end":28,"loc":{"start":{"line":1,"column":23,"index":23},"end":{"line":1,"column":28,"index":28}},
"typeAnnotation": {
"type": "TSTypeLiteral",
"start":26,"end":28,"loc":{"start":{"line":1,"column":26,"index":26},"end":{"line":1,"column":28,"index":28}},
"members": []
}
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
type F = ({ x = `${0}` }) => {}
@@ -0,0 +1,106 @@
{
"type": "File",
"start":0,"end":31,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":31,"index":31}},
"program": {
"type": "Program",
"start":0,"end":31,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":31,"index":31}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSTypeAliasDeclaration",
"start":0,"end":31,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":31,"index":31}},
"id": {
"type": "Identifier",
"start":5,"end":6,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":6,"index":6},"identifierName":"F"},
"name": "F"
},
"typeAnnotation": {
"type": "TSFunctionType",
"start":9,"end":31,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":31,"index":31}},
"parameters": [
{
"type": "ObjectPattern",
"start":10,"end":24,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":24,"index":24}},
"properties": [
{
"type": "ObjectProperty",
"start":12,"end":22,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":22,"index":22}},
"key": {
"type": "Identifier",
"start":12,"end":13,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":13,"index":13},"identifierName":"x"},
"name": "x"
},
"computed": false,
"method": false,
"shorthand": true,
"value": {
"type": "AssignmentPattern",
"start":12,"end":22,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":22,"index":22}},
"left": {
"type": "Identifier",
"start":12,"end":13,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":13,"index":13},"identifierName":"x"},
"name": "x"
},
"right": {
"type": "TemplateLiteral",
"start":16,"end":22,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":22,"index":22}},
"expressions": [
{
"type": "TSLiteralType",
"start":19,"end":20,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":20,"index":20}},
"literal": {
"type": "NumericLiteral",
"start":19,"end":20,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":20,"index":20}},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
}
],
"quasis": [
{
"type": "TemplateElement",
"start":17,"end":17,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":17,"index":17}},
"value": {
"raw": "",
"cooked": ""
},
"tail": false
},
{
"type": "TemplateElement",
"start":21,"end":21,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":21,"index":21}},
"value": {
"raw": "",
"cooked": ""
},
"tail": true
}
]
}
},
"extra": {
"shorthand": true
}
}
]
}
],
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":26,"end":31,"loc":{"start":{"line":1,"column":26,"index":26},"end":{"line":1,"column":31,"index":31}},
"typeAnnotation": {
"type": "TSTypeLiteral",
"start":29,"end":31,"loc":{"start":{"line":1,"column":29,"index":29},"end":{"line":1,"column":31,"index":31}},
"members": []
}
}
}
}
],
"directives": []
}
}
@@ -0,0 +1,5 @@
{
"sourceType": "module",
"plugins": ["typescript"],
"BABEL_8_BREAKING": false
}
@@ -0,0 +1 @@
type F = ([ x = #[0] ]) => {}
@@ -0,0 +1,4 @@
{
"sourceType": "module",
"plugins": ["typescript", ["recordAndTuple", { "syntaxType": "hash" }]]
}

0 comments on commit a53c2fa

Please sign in to comment.