Skip to content

Commit

Permalink
[flow] Add support for parsing _ as implicit instantiation in call/…
Browse files Browse the repository at this point in the history
…new (#8883)

* [flow] Add support for parsing  as implicit instantiation in call/new

* Update flow tests and fix underscore being a reserved type

* Rebase onto flow-test

* Fix flow commit hash
  • Loading branch information
jbrown215 authored and danez committed Nov 5, 2018
1 parent c6d2f45 commit f216a7b
Show file tree
Hide file tree
Showing 18 changed files with 1,328 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Makefile
@@ -1,5 +1,5 @@
MAKEFLAGS = -j1
FLOW_COMMIT = bea8b83f50f597454941d2a7ecef6e93a881e576
FLOW_COMMIT = e192e1a4793dd8e43415fbfe8046d832cb513c8b
TEST262_COMMIT = 72f1cfa2abd66a69b29e9b7d691a8ae8c5a7a00f

# Fix color output until TravisCI fixes https://github.com/travis-ci/travis-ci/issues/7967
Expand Down Expand Up @@ -82,7 +82,7 @@ test-ci-coverage:
bootstrap-flow:
rm -rf ./build/flow
mkdir -p ./build
git clone --branch=master --single-branch --shallow-since=2017-01-01 https://github.com/facebook/flow.git ./build/flow
git clone --branch=master --single-branch --shallow-since=2018-11-01 https://github.com/facebook/flow.git ./build/flow
cd build/flow && git checkout $(FLOW_COMMIT)

test-flow:
Expand Down
49 changes: 47 additions & 2 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -23,6 +23,9 @@ const primitiveTypes = [
"true",
"typeof",
"void",
"interface",
"extends",
"_",
];

function isEsModuleType(bodyElement: N.Node): boolean {
Expand Down Expand Up @@ -490,6 +493,15 @@ 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) {
if (primitiveTypes.indexOf(word) > -1) {
this.raise(startLoc, `Cannot overwrite primitive type ${word}`);
Expand Down Expand Up @@ -654,6 +666,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 @@ -1176,6 +1209,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 @@ -1431,6 +1465,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 @@ -2582,7 +2627,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 @@ -2613,7 +2658,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 comments on commit f216a7b

Please sign in to comment.