Skip to content

Commit

Permalink
add transform-typescript optional-chain call test
Browse files Browse the repository at this point in the history
  • Loading branch information
lala7573 committed Jul 2, 2021
1 parent 76abdb1 commit cbfe166
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 11 deletions.
6 changes: 5 additions & 1 deletion packages/babel-parser/src/plugins/typescript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2077,7 +2077,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.match(tt.questionDot) &&
this.lookaheadCharCode() === charCodes.lessThan
) {
state.optionalChainMember = isOptionalCall = true;
isOptionalCall = true;
if (noCalls) {
state.stop = true;
return base;
Expand Down Expand Up @@ -2124,6 +2124,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// $FlowIgnore
node.optional = false;
}
if (isOptionalCall) {
node.optional = true;
}

return this.finishCallExpression(node, state.optionalChainMember);
} else if (this.match(tt.backQuote)) {
const result = this.parseTaggedTemplateExpression(
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
foo?.foo<T>();
foo?.foo?.<T>();
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"type": "File",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"start":0,"end":31,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":16}},
"program": {
"type": "Program",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"start":0,"end":31,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":16}},
"sourceType": "script",
"interpreter": null,
"body": [
Expand Down Expand Up @@ -51,6 +51,51 @@
"optional": false
}
}
},
{
"type": "ExpressionStatement",
"start":15,"end":31,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":16}},
"expression": {
"type": "ChainExpression",
"start":15,"end":30,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":15}},
"expression": {
"type": "CallExpression",
"start":15,"end":30,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":15}},
"callee": {
"type": "MemberExpression",
"start":15,"end":23,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":8}},
"object": {
"type": "Identifier",
"start":15,"end":18,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":3},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"property": {
"type": "Identifier",
"start":20,"end":23,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":8},"identifierName":"foo"},
"name": "foo"
},
"optional": true
},
"arguments": [],
"typeParameters": {
"type": "TSTypeParameterInstantiation",
"start":25,"end":28,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":13}},
"params": [
{
"type": "TSTypeReference",
"start":26,"end":27,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":12}},
"typeName": {
"type": "Identifier",
"start":26,"end":27,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":12},"identifierName":"T"},
"name": "T"
}
}
]
},
"optional": true
}
}
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"type": "ExpressionStatement",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9}},
"expression": {
"type": "OptionalCallExpression",
"type": "CallExpression",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}},
"callee": {
"type": "Identifier",
Expand All @@ -34,14 +34,14 @@
}
]
},
"optional": false
"optional": true
}
},
{
"type": "ExpressionStatement",
"start":10,"end":22,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":12}},
"expression": {
"type": "OptionalCallExpression",
"type": "CallExpression",
"start":10,"end":21,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":11}},
"callee": {
"type": "Identifier",
Expand Down Expand Up @@ -73,14 +73,14 @@
}
]
},
"optional": false
"optional": true
}
},
{
"type": "ExpressionStatement",
"start":23,"end":33,"loc":{"start":{"line":3,"column":0},"end":{"line":4,"column":8}},
"expression": {
"type": "OptionalCallExpression",
"type": "CallExpression",
"start":23,"end":32,"loc":{"start":{"line":3,"column":0},"end":{"line":4,"column":7}},
"callee": {
"type": "Identifier",
Expand All @@ -103,7 +103,7 @@
}
]
},
"optional": false
"optional": true
}
}
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
x?.f<T>();
x?.f<T>();
x?.f?.<T>();
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
x?.f();
x?.f();
x?.f?.();

0 comments on commit cbfe166

Please sign in to comment.