Skip to content

Commit

Permalink
flow - allow type parameter defaults in function declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Jun 11, 2019
1 parent f92c2ae commit 950593d
Show file tree
Hide file tree
Showing 13 changed files with 907 additions and 40 deletions.
20 changes: 5 additions & 15 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -798,9 +798,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 +1285,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 +2088,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 +2170,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 +2378,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
@@ -1,3 +1 @@
(class A {
foo<T = string>() {}
});
declare function foo<T = string>() {}
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (2:8)"
}
"throws": "Unexpected token, expected \":\" (1:35)"
}
@@ -1 +1 @@
({ foo<T = string>() {} });
var x: Array<T = number>
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:9)"
}
"throws": "Unexpected token, expected \",\" (1:15)"
}
@@ -1 +1 @@
declare class A { foo<T = string>(): void }
class A extends B<T = number> {}
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:24)"
}
"throws": "Unexpected token, expected \",\" (1:20)"
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

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 950593d

Please sign in to comment.