Skip to content

Commit

Permalink
[TS] Parse calls with type args in optional chains (#10631)
Browse files Browse the repository at this point in the history
* [TS] Parse calls with type args in optional chains

* Test output
  • Loading branch information
nicolo-ribaudo authored and JLHwung committed Nov 4, 2019
1 parent abce0ef commit d023e10
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 29 deletions.
32 changes: 10 additions & 22 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -628,7 +628,7 @@ export default class ExpressionParser extends LValParser {
node.callee = base;
node.arguments = this.parseCallExpressionArguments(tt.parenR, false);
node.optional = true;
return this.finishNode(node, "OptionalCallExpression");
return this.finishCallExpression(node, /* optional */ true);
} else {
node.object = base;
node.property = this.parseIdentifier(true);
Expand Down Expand Up @@ -683,11 +683,7 @@ export default class ExpressionParser extends LValParser {
base.type !== "Super",
node,
);
if (!state.optionalChainMember) {
this.finishCallExpression(node);
} else {
this.finishOptionalCallExpression(node);
}
this.finishCallExpression(node, state.optionalChainMember);

if (state.maybeAsyncArrow && this.shouldParseAsyncArrow()) {
state.stop = true;
Expand Down Expand Up @@ -783,21 +779,10 @@ export default class ExpressionParser extends LValParser {
);
}

finishCallExpression(node: N.CallExpression): N.CallExpression {
if (node.callee.type === "Import") {
if (node.arguments.length !== 1) {
this.raise(node.start, "import() requires exactly one argument");
}

const importArg = node.arguments[0];
if (importArg && importArg.type === "SpreadElement") {
this.raise(importArg.start, "... is not allowed in import()");
}
}
return this.finishNode(node, "CallExpression");
}

finishOptionalCallExpression(node: N.CallExpression): N.CallExpression {
finishCallExpression<T: N.CallExpression | N.OptionalCallExpression>(
node: T,
optional: boolean,
): T {
if (node.callee.type === "Import") {
if (node.arguments.length !== 1) {
this.raise(node.start, "import() requires exactly one argument");
Expand All @@ -808,7 +793,10 @@ export default class ExpressionParser extends LValParser {
this.raise(importArg.start, "... is not allowed in import()");
}
}
return this.finishNode(node, "OptionalCallExpression");
return this.finishNode(
node,
optional ? "OptionalCallExpression" : "CallExpression",
);
}

parseCallExpressionArguments(
Expand Down
12 changes: 6 additions & 6 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -2709,7 +2709,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// $FlowFixMe
node.arguments = this.parseCallExpressionArguments(tt.parenR, false);
node.optional = true;
return this.finishNode(node, "OptionalCallExpression");
return this.finishCallExpression(node, /* optional */ true);
} else if (
!noCalls &&
this.shouldParseTypes() &&
Expand All @@ -2722,11 +2722,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
this.expect(tt.parenL);
node.arguments = this.parseCallExpressionArguments(tt.parenR, false);
if (subscriptState.optionalChainMember) {
node.optional = false;
return this.finishNode(node, "OptionalCallExpression");
}
return this.finishNode(node, "CallExpression");
if (subscriptState.optionalChainMember) node.optional = false;
return this.finishCallExpression(
node,
subscriptState.optionalChainMember,
);
} catch (e) {
if (e instanceof SyntaxError) {
this.state = state;
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -1655,7 +1655,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
/* possibleAsync */ false,
);
node.typeParameters = typeArguments;
return this.finishCallExpression(node);
return this.finishCallExpression(node, state.optionalChainMember);
} else if (this.match(tt.backQuote)) {
return this.parseTaggedTemplateExpression(
startPos,
Expand Down
@@ -0,0 +1 @@
example.inner?.greet<string>()
@@ -0,0 +1,4 @@
{
"sourceType": "module",
"plugins": ["typescript", "optionalChaining"]
}
@@ -0,0 +1,182 @@
{
"type": "File",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 30
}
},
"program": {
"type": "Program",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 30
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 30
}
},
"expression": {
"type": "OptionalCallExpression",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 30
}
},
"callee": {
"type": "OptionalMemberExpression",
"start": 0,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 20
}
},
"object": {
"type": "MemberExpression",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"object": {
"type": "Identifier",
"start": 0,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "example"
},
"name": "example"
},
"property": {
"type": "Identifier",
"start": 8,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 13
},
"identifierName": "inner"
},
"name": "inner"
},
"computed": false
},
"property": {
"type": "Identifier",
"start": 15,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 20
},
"identifierName": "greet"
},
"name": "greet"
},
"computed": false,
"optional": true
},
"arguments": [],
"typeParameters": {
"type": "TSTypeParameterInstantiation",
"start": 20,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 20
},
"end": {
"line": 1,
"column": 28
}
},
"params": [
{
"type": "TSStringKeyword",
"start": 21,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 27
}
}
}
]
}
}
}
],
"directives": []
}
}

0 comments on commit d023e10

Please sign in to comment.