Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flow] Add support for parsing _ as implicit instantiation in call/new #8883

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 45 additions & 2 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -484,7 +484,17 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.finishNode(node, "InterfaceDeclaration");
}

checkNotUnderscore(word: string) {
if (word === "_") {
throw this.unexpected(
null,
"`_` is only allowed as a type argument to call or new",
);
}
}

checkReservedType(word: string, startLoc: number) {
this.checkNotUnderscore(word);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this, I covered this case in #8965 by adding _ to the primitiveTypes array.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, adding to primitiveTypes sounds like a much better way to go.

if (primitiveTypes.indexOf(word) > -1) {
this.raise(startLoc, `Cannot overwrite primitive type ${word}`);
}
Expand Down Expand Up @@ -645,6 +655,27 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.finishNode(node, "TypeParameterInstantiation");
}

flowParseTypeParameterInstantiationCallOrNew(): N.TypeParameterInstantiation {
const node = this.startNode();
const oldInType = this.state.inType;
node.params = [];

this.state.inType = true;

this.expectRelational("<");
while (!this.isRelational(">")) {
node.params.push(this.flowParseTypeOrImplicitInstantiation());
if (!this.isRelational(">")) {
this.expect(tt.comma);
}
}
this.expectRelational(">");

this.state.inType = oldInType;

return this.finishNode(node, "TypeParameterInstantiation");
}

flowParseInterfaceType(): N.FlowInterfaceType {
const node = this.startNode();
this.expectContextual("interface");
Expand Down Expand Up @@ -1116,6 +1147,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.finishNode(node, "StringTypeAnnotation");

default:
this.checkNotUnderscore(id.name);
return this.flowParseGenericType(startPos, startLoc, id);
}
}
Expand Down Expand Up @@ -1359,6 +1391,17 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return type;
}

flowParseTypeOrImplicitInstantiation(): N.FlowTypeAnnotation {
if (this.state.type === tt.name && this.state.value === "_") {
const startPos = this.state.start;
const startLoc = this.state.startLoc;
const node = this.parseIdentifier();
return this.flowParseGenericType(startPos, startLoc, node);
} else {
return this.flowParseType();
}
}

flowParseTypeAnnotation(): N.FlowTypeAnnotation {
const node = this.startNode();
node.typeAnnotation = this.flowParseTypeInitialiser();
Expand Down Expand Up @@ -2510,7 +2553,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.callee = base;
const state = this.state.clone();
try {
node.typeArguments = this.flowParseTypeParameterInstantiation();
node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
this.expect(tt.parenL);
node.arguments = this.parseCallExpressionArguments(tt.parenR, false);
if (subscriptState.optionalChainMember) {
Expand Down Expand Up @@ -2541,7 +2584,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (this.shouldParseTypes() && this.isRelational("<")) {
const state = this.state.clone();
try {
targs = this.flowParseTypeParameterInstantiation();
targs = this.flowParseTypeParameterInstantiationCallOrNew();
} catch (e) {
if (e instanceof SyntaxError) {
this.state = state;
Expand Down
@@ -0,0 +1,8 @@
//@flow
test<
_,
_,
number,
_,
_,
>();
@@ -0,0 +1,283 @@
{
"type": "File",
"start": 0,
"end": 48,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 8,
"column": 4
}
},
"program": {
"type": "Program",
"start": 0,
"end": 48,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 8,
"column": 4
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 8,
"end": 48,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 8,
"column": 4
}
},
"expression": {
"type": "CallExpression",
"start": 8,
"end": 47,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 8,
"column": 3
}
},
"callee": {
"type": "Identifier",
"start": 8,
"end": 12,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 4
},
"identifierName": "test"
},
"name": "test"
},
"typeArguments": {
"type": "TypeParameterInstantiation",
"start": 12,
"end": 45,
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 8,
"column": 1
}
},
"params": [
{
"type": "GenericTypeAnnotation",
"start": 16,
"end": 17,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 3
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 16,
"end": 17,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 3
},
"identifierName": "_"
},
"name": "_"
}
},
{
"type": "GenericTypeAnnotation",
"start": 21,
"end": 22,
"loc": {
"start": {
"line": 4,
"column": 2
},
"end": {
"line": 4,
"column": 3
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 21,
"end": 22,
"loc": {
"start": {
"line": 4,
"column": 2
},
"end": {
"line": 4,
"column": 3
},
"identifierName": "_"
},
"name": "_"
}
},
{
"type": "NumberTypeAnnotation",
"start": 26,
"end": 32,
"loc": {
"start": {
"line": 5,
"column": 2
},
"end": {
"line": 5,
"column": 8
}
}
},
{
"type": "GenericTypeAnnotation",
"start": 36,
"end": 37,
"loc": {
"start": {
"line": 6,
"column": 2
},
"end": {
"line": 6,
"column": 3
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 36,
"end": 37,
"loc": {
"start": {
"line": 6,
"column": 2
},
"end": {
"line": 6,
"column": 3
},
"identifierName": "_"
},
"name": "_"
}
},
{
"type": "GenericTypeAnnotation",
"start": 41,
"end": 42,
"loc": {
"start": {
"line": 7,
"column": 2
},
"end": {
"line": 7,
"column": 3
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 41,
"end": 42,
"loc": {
"start": {
"line": 7,
"column": 2
},
"end": {
"line": 7,
"column": 3
},
"identifierName": "_"
},
"name": "_"
}
}
]
},
"arguments": []
},
"leadingComments": [
{
"type": "CommentLine",
"value": "@flow",
"start": 0,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 7
}
}
}
]
}
],
"directives": []
},
"comments": [
{
"type": "CommentLine",
"value": "@flow",
"start": 0,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 7
}
}
}
]
}
@@ -0,0 +1,2 @@
//@flow
var x: Generic<_> = 3;
@@ -0,0 +1,5 @@
{
"sourceType": "module",
"plugins": ["jsx", "flow"],
"throws": "`_` is only allowed as a type argument to call or new (2:16)"
}
@@ -0,0 +1,2 @@
//@flow
type _ = number;