[TS] Parse calls with type args in optional chains (#10631)

* [TS] Parse calls with type args in optional chains

* Test output
This commit is contained in:
Nicolò Ribaudo 2019-11-04 19:25:15 +01:00 committed by Huáng Jùnliàng
parent abce0ef49d
commit d023e105b7
6 changed files with 204 additions and 29 deletions

View File

@ -628,7 +628,7 @@ export default class ExpressionParser extends LValParser {
node.callee = base; node.callee = base;
node.arguments = this.parseCallExpressionArguments(tt.parenR, false); node.arguments = this.parseCallExpressionArguments(tt.parenR, false);
node.optional = true; node.optional = true;
return this.finishNode(node, "OptionalCallExpression"); return this.finishCallExpression(node, /* optional */ true);
} else { } else {
node.object = base; node.object = base;
node.property = this.parseIdentifier(true); node.property = this.parseIdentifier(true);
@ -683,11 +683,7 @@ export default class ExpressionParser extends LValParser {
base.type !== "Super", base.type !== "Super",
node, node,
); );
if (!state.optionalChainMember) { this.finishCallExpression(node, state.optionalChainMember);
this.finishCallExpression(node);
} else {
this.finishOptionalCallExpression(node);
}
if (state.maybeAsyncArrow && this.shouldParseAsyncArrow()) { if (state.maybeAsyncArrow && this.shouldParseAsyncArrow()) {
state.stop = true; state.stop = true;
@ -783,7 +779,10 @@ export default class ExpressionParser extends LValParser {
); );
} }
finishCallExpression(node: N.CallExpression): N.CallExpression { finishCallExpression<T: N.CallExpression | N.OptionalCallExpression>(
node: T,
optional: boolean,
): T {
if (node.callee.type === "Import") { if (node.callee.type === "Import") {
if (node.arguments.length !== 1) { if (node.arguments.length !== 1) {
this.raise(node.start, "import() requires exactly one argument"); this.raise(node.start, "import() requires exactly one argument");
@ -794,21 +793,10 @@ export default class ExpressionParser extends LValParser {
this.raise(importArg.start, "... is not allowed in import()"); this.raise(importArg.start, "... is not allowed in import()");
} }
} }
return this.finishNode(node, "CallExpression"); return this.finishNode(
} node,
optional ? "OptionalCallExpression" : "CallExpression",
finishOptionalCallExpression(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, "OptionalCallExpression");
} }
parseCallExpressionArguments( parseCallExpressionArguments(

View File

@ -2709,7 +2709,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// $FlowFixMe // $FlowFixMe
node.arguments = this.parseCallExpressionArguments(tt.parenR, false); node.arguments = this.parseCallExpressionArguments(tt.parenR, false);
node.optional = true; node.optional = true;
return this.finishNode(node, "OptionalCallExpression"); return this.finishCallExpression(node, /* optional */ true);
} else if ( } else if (
!noCalls && !noCalls &&
this.shouldParseTypes() && this.shouldParseTypes() &&
@ -2722,11 +2722,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew(); node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
this.expect(tt.parenL); this.expect(tt.parenL);
node.arguments = this.parseCallExpressionArguments(tt.parenR, false); node.arguments = this.parseCallExpressionArguments(tt.parenR, false);
if (subscriptState.optionalChainMember) { if (subscriptState.optionalChainMember) node.optional = false;
node.optional = false; return this.finishCallExpression(
return this.finishNode(node, "OptionalCallExpression"); node,
} subscriptState.optionalChainMember,
return this.finishNode(node, "CallExpression"); );
} catch (e) { } catch (e) {
if (e instanceof SyntaxError) { if (e instanceof SyntaxError) {
this.state = state; this.state = state;

View File

@ -1655,7 +1655,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
/* possibleAsync */ false, /* possibleAsync */ false,
); );
node.typeParameters = typeArguments; node.typeParameters = typeArguments;
return this.finishCallExpression(node); return this.finishCallExpression(node, state.optionalChainMember);
} else if (this.match(tt.backQuote)) { } else if (this.match(tt.backQuote)) {
return this.parseTaggedTemplateExpression( return this.parseTaggedTemplateExpression(
startPos, startPos,

View File

@ -0,0 +1 @@
example.inner?.greet<string>()

View File

@ -0,0 +1,4 @@
{
"sourceType": "module",
"plugins": ["typescript", "optionalChaining"]
}

View File

@ -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": []
}
}