Skip to content

Commit

Permalink
fix: start TypePredicate node after returnToken (#12678)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jan 23, 2021
1 parent 8c03fb0 commit 568679e
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 21 deletions.
5 changes: 2 additions & 3 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -1050,6 +1050,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
const t: N.TsTypeAnnotation = this.startNode();
this.expect(returnToken);

const node = this.startNode<N.TsTypePredicate>();

const asserts = !!this.tsTryParse(
this.tsParseTypePredicateAsserts.bind(this),
);
Expand All @@ -1061,7 +1063,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// if it turns out to be a `TSThisType`, wrap it with `TSTypePredicate`
// : asserts this
if (thisTypePredicate.type === "TSThisType") {
const node: N.TsTypePredicate = this.startNodeAtNode(t);
node.parameterName = (thisTypePredicate: N.TsThisType);
node.asserts = true;
thisTypePredicate = this.finishNode(node, "TSTypePredicate");
Expand All @@ -1082,7 +1083,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.tsParseTypeAnnotation(/* eatColon */ false, t);
}

const node: N.TsTypePredicate = this.startNodeAtNode(t);
// : asserts foo
node.parameterName = this.parseIdentifier();
node.asserts = asserts;
Expand All @@ -1092,7 +1092,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>

// : asserts foo is type
const type = this.tsParseTypeAnnotation(/* eatColon */ false);
const node = this.startNodeAtNode(t);
node.parameterName = typePredicateVariable;
node.typeAnnotation = type;
node.asserts = asserts;
Expand Down
Expand Up @@ -18,7 +18,7 @@
"start":8,"end":21,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":21}},
"typeAnnotation": {
"type": "TSTypePredicate",
"start":8,"end":21,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":21}},
"start":10,"end":21,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":21}},
"parameterName": {
"type": "Identifier",
"start":10,"end":11,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":11},"identifierName":"x"},
Expand Down
Expand Up @@ -27,7 +27,7 @@
"start":32,"end":57,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":57}},
"typeAnnotation": {
"type": "TSTypePredicate",
"start":32,"end":57,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":57}},
"start":34,"end":57,"loc":{"start":{"line":1,"column":34},"end":{"line":1,"column":57}},
"parameterName": {
"type": "Identifier",
"start":42,"end":47,"loc":{"start":{"line":1,"column":42},"end":{"line":1,"column":47},"identifierName":"value"},
Expand Down Expand Up @@ -93,7 +93,7 @@
"start":96,"end":111,"loc":{"start":{"line":2,"column":32},"end":{"line":2,"column":47}},
"typeAnnotation": {
"type": "TSTypePredicate",
"start":96,"end":111,"loc":{"start":{"line":2,"column":32},"end":{"line":2,"column":47}},
"start":98,"end":111,"loc":{"start":{"line":2,"column":34},"end":{"line":2,"column":47}},
"parameterName": {
"type": "Identifier",
"start":106,"end":111,"loc":{"start":{"line":2,"column":42},"end":{"line":2,"column":47},"identifierName":"value"},
Expand Down
Expand Up @@ -40,7 +40,7 @@
"start":15,"end":29,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":19}},
"typeAnnotation": {
"type": "TSTypePredicate",
"start":15,"end":29,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":19}},
"start":17,"end":29,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":19}},
"parameterName": {
"type": "TSThisType",
"start":25,"end":29,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":19}}
Expand Down
Expand Up @@ -54,7 +54,7 @@
"start":42,"end":67,"loc":{"start":{"line":2,"column":32},"end":{"line":2,"column":57}},
"typeAnnotation": {
"type": "TSTypePredicate",
"start":42,"end":67,"loc":{"start":{"line":2,"column":32},"end":{"line":2,"column":57}},
"start":44,"end":67,"loc":{"start":{"line":2,"column":34},"end":{"line":2,"column":57}},
"parameterName": {
"type": "Identifier",
"start":52,"end":57,"loc":{"start":{"line":2,"column":42},"end":{"line":2,"column":47},"identifierName":"value"},
Expand Down
Expand Up @@ -10,6 +10,7 @@
{
"type": "TSDeclareFunction",
"start":0,"end":63,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":63}},
"declare": true,
"id": {
"type": "Identifier",
"start":17,"end":31,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":31},"identifierName":"assertIsString"},
Expand Down Expand Up @@ -37,16 +38,15 @@
"start":47,"end":62,"loc":{"start":{"line":1,"column":47},"end":{"line":1,"column":62}},
"typeAnnotation": {
"type": "TSTypePredicate",
"start":47,"end":62,"loc":{"start":{"line":1,"column":47},"end":{"line":1,"column":62}},
"start":49,"end":62,"loc":{"start":{"line":1,"column":49},"end":{"line":1,"column":62}},
"parameterName": {
"type": "Identifier",
"start":57,"end":62,"loc":{"start":{"line":1,"column":57},"end":{"line":1,"column":62},"identifierName":"value"},
"name": "value"
},
"asserts": true
}
},
"declare": true
}
}
],
"directives": []
Expand Down
Expand Up @@ -10,6 +10,7 @@
{
"type": "TSDeclareFunction",
"start":0,"end":73,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":73}},
"declare": true,
"id": {
"type": "Identifier",
"start":17,"end":31,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":31},"identifierName":"assertIsString"},
Expand Down Expand Up @@ -37,7 +38,7 @@
"start":47,"end":72,"loc":{"start":{"line":1,"column":47},"end":{"line":1,"column":72}},
"typeAnnotation": {
"type": "TSTypePredicate",
"start":47,"end":72,"loc":{"start":{"line":1,"column":47},"end":{"line":1,"column":72}},
"start":49,"end":72,"loc":{"start":{"line":1,"column":49},"end":{"line":1,"column":72}},
"parameterName": {
"type": "Identifier",
"start":57,"end":62,"loc":{"start":{"line":1,"column":57},"end":{"line":1,"column":62},"identifierName":"value"},
Expand All @@ -53,8 +54,7 @@
},
"asserts": true
}
},
"declare": true
}
}
],
"directives": []
Expand Down
Expand Up @@ -37,7 +37,7 @@
"start":34,"end":59,"loc":{"start":{"line":1,"column":34},"end":{"line":1,"column":59}},
"typeAnnotation": {
"type": "TSTypePredicate",
"start":34,"end":59,"loc":{"start":{"line":1,"column":34},"end":{"line":1,"column":59}},
"start":36,"end":59,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":59}},
"parameterName": {
"type": "Identifier",
"start":44,"end":49,"loc":{"start":{"line":1,"column":44},"end":{"line":1,"column":49},"identifierName":"value"},
Expand Down Expand Up @@ -91,7 +91,7 @@
"start":98,"end":113,"loc":{"start":{"line":2,"column":34},"end":{"line":2,"column":49}},
"typeAnnotation": {
"type": "TSTypePredicate",
"start":98,"end":113,"loc":{"start":{"line":2,"column":34},"end":{"line":2,"column":49}},
"start":100,"end":113,"loc":{"start":{"line":2,"column":36},"end":{"line":2,"column":49}},
"parameterName": {
"type": "Identifier",
"start":108,"end":113,"loc":{"start":{"line":2,"column":44},"end":{"line":2,"column":49},"identifierName":"value"},
Expand Down
Expand Up @@ -13,6 +13,7 @@
{
"type": "TSDeclareFunction",
"start":0,"end":68,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":68}},
"declare": true,
"id": {
"type": "Identifier",
"start":17,"end":31,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":31},"identifierName":"assertIsString"},
Expand Down Expand Up @@ -40,16 +41,15 @@
"start":47,"end":67,"loc":{"start":{"line":1,"column":47},"end":{"line":1,"column":67}},
"typeAnnotation": {
"type": "TSTypePredicate",
"start":47,"end":67,"loc":{"start":{"line":1,"column":47},"end":{"line":1,"column":67}},
"start":49,"end":67,"loc":{"start":{"line":1,"column":49},"end":{"line":1,"column":67}},
"parameterName": {
"type": "Identifier",
"start":62,"end":67,"loc":{"start":{"line":1,"column":62},"end":{"line":1,"column":67},"identifierName":"value"},
"name": "value"
},
"asserts": true
}
},
"declare": true
}
}
],
"directives": []
Expand Down
Expand Up @@ -37,7 +37,7 @@
"start":18,"end":32,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":32}},
"typeAnnotation": {
"type": "TSTypePredicate",
"start":18,"end":32,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":32}},
"start":20,"end":32,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":32}},
"parameterName": {
"type": "Identifier",
"start":20,"end":21,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":21},"identifierName":"x"},
Expand Down Expand Up @@ -94,7 +94,7 @@
"start":53,"end":67,"loc":{"start":{"line":2,"column":17},"end":{"line":2,"column":31}},
"typeAnnotation": {
"type": "TSTypePredicate",
"start":53,"end":67,"loc":{"start":{"line":2,"column":17},"end":{"line":2,"column":31}},
"start":55,"end":67,"loc":{"start":{"line":2,"column":19},"end":{"line":2,"column":31}},
"parameterName": {
"type": "Identifier",
"start":55,"end":56,"loc":{"start":{"line":2,"column":19},"end":{"line":2,"column":20},"identifierName":"x"},
Expand Down

0 comments on commit 568679e

Please sign in to comment.