[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:
parent
abce0ef49d
commit
d023e105b7
@ -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);
|
||||
@ -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;
|
||||
@ -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.arguments.length !== 1) {
|
||||
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()");
|
||||
}
|
||||
}
|
||||
return this.finishNode(node, "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");
|
||||
return this.finishNode(
|
||||
node,
|
||||
optional ? "OptionalCallExpression" : "CallExpression",
|
||||
);
|
||||
}
|
||||
|
||||
parseCallExpressionArguments(
|
||||
|
||||
@ -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() &&
|
||||
@ -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;
|
||||
|
||||
@ -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,
|
||||
|
||||
1
packages/babel-parser/test/fixtures/typescript/optional-chaining/type-arguments/input.ts
vendored
Normal file
1
packages/babel-parser/test/fixtures/typescript/optional-chaining/type-arguments/input.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
example.inner?.greet<string>()
|
||||
4
packages/babel-parser/test/fixtures/typescript/optional-chaining/type-arguments/options.json
vendored
Normal file
4
packages/babel-parser/test/fixtures/typescript/optional-chaining/type-arguments/options.json
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"plugins": ["typescript", "optionalChaining"]
|
||||
}
|
||||
182
packages/babel-parser/test/fixtures/typescript/optional-chaining/type-arguments/output.json
vendored
Normal file
182
packages/babel-parser/test/fixtures/typescript/optional-chaining/type-arguments/output.json
vendored
Normal 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": []
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user