Skip to content

Commit

Permalink
flow - allow type parameter defaults in function declarations (#10084)
Browse files Browse the repository at this point in the history
* flow - allow type parameter defaults in function declarations

* fix flow test

* add intern_comments option

* fix flow parser test

* remove allowdefault from flowParseTypeParameterDeclaration

* rename test cases
  • Loading branch information
tanhauhau authored and nicolo-ribaudo committed Jun 15, 2019
1 parent 6852bf6 commit fdbbb74
Show file tree
Hide file tree
Showing 22 changed files with 938 additions and 74 deletions.
2 changes: 1 addition & 1 deletion Makefile
@@ -1,5 +1,5 @@
MAKEFLAGS = -j1
FLOW_COMMIT = 2ac56861e3ceff9ca406ae586fbafb3480c6c0b7
FLOW_COMMIT = 09669846b7a7ca5a6c23c12d56bb3bebdafd67e9
TEST262_COMMIT = de567d3aa5de4eaa11e00131d26b9fe77997dfb0

# Fix color output until TravisCI fixes https://github.com/travis-ci/travis-ci/issues/7967
Expand Down
48 changes: 10 additions & 38 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -588,16 +588,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>

// Type annotations

flowParseTypeParameter(
allowDefault?: boolean = true,
requireDefault?: boolean = false,
): N.TypeParameter {
if (!allowDefault && requireDefault) {
throw new Error(
"Cannot disallow a default value (`allowDefault`) while also requiring it (`requireDefault`).",
);
}

flowParseTypeParameter(requireDefault?: boolean = false): N.TypeParameter {
const nodeStart = this.state.start;

const node = this.startNode();
Expand All @@ -610,12 +601,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.bound = ident.typeAnnotation;

if (this.match(tt.eq)) {
if (allowDefault) {
this.eat(tt.eq);
node.default = this.flowParseType();
} else {
this.unexpected();
}
this.eat(tt.eq);
node.default = this.flowParseType();
} else {
if (requireDefault) {
this.unexpected(
Expand All @@ -629,9 +616,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.finishNode(node, "TypeParameter");
}

flowParseTypeParameterDeclaration(
allowDefault?: boolean = true,
): N.TypeParameterDeclaration {
flowParseTypeParameterDeclaration(): N.TypeParameterDeclaration {
const oldInType = this.state.inType;
const node = this.startNode();
node.params = [];
Expand All @@ -648,10 +633,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
let defaultRequired = false;

do {
const typeParameter = this.flowParseTypeParameter(
allowDefault,
defaultRequired,
);
const typeParameter = this.flowParseTypeParameter(defaultRequired);

node.params.push(typeParameter);

Expand Down Expand Up @@ -798,9 +780,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.typeParameters = null;

if (this.isRelational("<")) {
node.typeParameters = this.flowParseTypeParameterDeclaration(
/* allowDefault */ false,
);
node.typeParameters = this.flowParseTypeParameterDeclaration();
}

this.expect(tt.parenL);
Expand Down Expand Up @@ -1287,9 +1267,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>

case tt.relational:
if (this.state.value === "<") {
node.typeParameters = this.flowParseTypeParameterDeclaration(
/* allowDefault */ false,
);
node.typeParameters = this.flowParseTypeParameterDeclaration();
this.expect(tt.parenL);
tmp = this.flowParseFunctionTypeParams();
node.params = tmp.params;
Expand Down Expand Up @@ -2092,9 +2070,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
delete (method: $FlowFixMe).variance;
if (this.isRelational("<")) {
method.typeParameters = this.flowParseTypeParameterDeclaration(
/* allowDefault */ false,
);
method.typeParameters = this.flowParseTypeParameterDeclaration();
}

super.pushClassMethod(
Expand Down Expand Up @@ -2176,9 +2152,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>

// method shorthand
if (this.isRelational("<")) {
typeParameters = this.flowParseTypeParameterDeclaration(
/* allowDefault */ false,
);
typeParameters = this.flowParseTypeParameterDeclaration();
if (!this.match(tt.parenL)) this.unexpected();
}

Expand Down Expand Up @@ -2386,9 +2360,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// $FlowFixMe
const kind = node.kind;
if (kind !== "get" && kind !== "set" && this.isRelational("<")) {
node.typeParameters = this.flowParseTypeParameterDeclaration(
/* allowDefault */ false,
);
node.typeParameters = this.flowParseTypeParameterDeclaration();
}
super.parseFunctionParams(node, allowModifiers);
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

@@ -0,0 +1 @@
class A extends B<T = number> {}
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected \",\" (1:20)"
}
@@ -0,0 +1 @@
declare function foo<T = string>() {}
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected \":\" (1:35)"
}
@@ -0,0 +1 @@
var x: Array<T = number>
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected \",\" (1:15)"
}
Expand Up @@ -20,3 +20,13 @@ interface A19<T: ?string = string> {}
interface A20<S, T: ?string = string> {}
interface A21<S = number, T: ?string = string> {}
type A22<T = void> = T
function A26<T = string>() {}
;({ A28<T = string>() {} });
class A29 {
foo<T = string>() {}
}
;(class A30 {
foo<T = string>() {}
});
declare class A31 { foo<T = string>(): void }
<T = string>() => 123;

0 comments on commit fdbbb74

Please sign in to comment.