flow - allow type parameter defaults in function declarations (#10084)

* 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
This commit is contained in:
Tan Li Hau 2019-06-15 18:31:12 +08:00 committed by Nicolò Ribaudo
parent 6852bf6415
commit fdbbb743b6
22 changed files with 938 additions and 74 deletions

View File

@ -1,5 +1,5 @@
MAKEFLAGS = -j1 MAKEFLAGS = -j1
FLOW_COMMIT = 2ac56861e3ceff9ca406ae586fbafb3480c6c0b7 FLOW_COMMIT = 09669846b7a7ca5a6c23c12d56bb3bebdafd67e9
TEST262_COMMIT = de567d3aa5de4eaa11e00131d26b9fe77997dfb0 TEST262_COMMIT = de567d3aa5de4eaa11e00131d26b9fe77997dfb0
# Fix color output until TravisCI fixes https://github.com/travis-ci/travis-ci/issues/7967 # Fix color output until TravisCI fixes https://github.com/travis-ci/travis-ci/issues/7967

View File

@ -588,16 +588,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// Type annotations // Type annotations
flowParseTypeParameter( flowParseTypeParameter(requireDefault?: boolean = false): N.TypeParameter {
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`).",
);
}
const nodeStart = this.state.start; const nodeStart = this.state.start;
const node = this.startNode(); const node = this.startNode();
@ -610,12 +601,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.bound = ident.typeAnnotation; node.bound = ident.typeAnnotation;
if (this.match(tt.eq)) { if (this.match(tt.eq)) {
if (allowDefault) { this.eat(tt.eq);
this.eat(tt.eq); node.default = this.flowParseType();
node.default = this.flowParseType();
} else {
this.unexpected();
}
} else { } else {
if (requireDefault) { if (requireDefault) {
this.unexpected( this.unexpected(
@ -629,9 +616,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.finishNode(node, "TypeParameter"); return this.finishNode(node, "TypeParameter");
} }
flowParseTypeParameterDeclaration( flowParseTypeParameterDeclaration(): N.TypeParameterDeclaration {
allowDefault?: boolean = true,
): N.TypeParameterDeclaration {
const oldInType = this.state.inType; const oldInType = this.state.inType;
const node = this.startNode(); const node = this.startNode();
node.params = []; node.params = [];
@ -648,10 +633,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
let defaultRequired = false; let defaultRequired = false;
do { do {
const typeParameter = this.flowParseTypeParameter( const typeParameter = this.flowParseTypeParameter(defaultRequired);
allowDefault,
defaultRequired,
);
node.params.push(typeParameter); node.params.push(typeParameter);
@ -798,9 +780,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.typeParameters = null; node.typeParameters = null;
if (this.isRelational("<")) { if (this.isRelational("<")) {
node.typeParameters = this.flowParseTypeParameterDeclaration( node.typeParameters = this.flowParseTypeParameterDeclaration();
/* allowDefault */ false,
);
} }
this.expect(tt.parenL); this.expect(tt.parenL);
@ -1287,9 +1267,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
case tt.relational: case tt.relational:
if (this.state.value === "<") { if (this.state.value === "<") {
node.typeParameters = this.flowParseTypeParameterDeclaration( node.typeParameters = this.flowParseTypeParameterDeclaration();
/* allowDefault */ false,
);
this.expect(tt.parenL); this.expect(tt.parenL);
tmp = this.flowParseFunctionTypeParams(); tmp = this.flowParseFunctionTypeParams();
node.params = tmp.params; node.params = tmp.params;
@ -2092,9 +2070,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
} }
delete (method: $FlowFixMe).variance; delete (method: $FlowFixMe).variance;
if (this.isRelational("<")) { if (this.isRelational("<")) {
method.typeParameters = this.flowParseTypeParameterDeclaration( method.typeParameters = this.flowParseTypeParameterDeclaration();
/* allowDefault */ false,
);
} }
super.pushClassMethod( super.pushClassMethod(
@ -2176,9 +2152,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// method shorthand // method shorthand
if (this.isRelational("<")) { if (this.isRelational("<")) {
typeParameters = this.flowParseTypeParameterDeclaration( typeParameters = this.flowParseTypeParameterDeclaration();
/* allowDefault */ false,
);
if (!this.match(tt.parenL)) this.unexpected(); if (!this.match(tt.parenL)) this.unexpected();
} }
@ -2386,9 +2360,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// $FlowFixMe // $FlowFixMe
const kind = node.kind; const kind = node.kind;
if (kind !== "get" && kind !== "set" && this.isRelational("<")) { if (kind !== "get" && kind !== "set" && this.isRelational("<")) {
node.typeParameters = this.flowParseTypeParameterDeclaration( node.typeParameters = this.flowParseTypeParameterDeclaration();
/* allowDefault */ false,
);
} }
super.parseFunctionParams(node, allowModifiers); super.parseFunctionParams(node, allowModifiers);
} }

View File

@ -1,3 +0,0 @@
(class A {
foo<T = string>() {}
});

View File

@ -1,3 +0,0 @@
{
"throws": "Unexpected token (2:8)"
}

View File

@ -1 +0,0 @@
({ foo<T = string>() {} });

View File

@ -1,3 +0,0 @@
{
"throws": "Unexpected token (1:9)"
}

View File

@ -1 +0,0 @@
declare class A { foo<T = string>(): void }

View File

@ -1,3 +0,0 @@
{
"throws": "Unexpected token (1:24)"
}

View File

@ -1,3 +0,0 @@
class A {
foo<T = string>() {}
}

View File

@ -1,3 +0,0 @@
{
"throws": "Unexpected token (2:8)"
}

View File

@ -1 +0,0 @@
function foo<T = string>() {}

View File

@ -1,3 +0,0 @@
{
"throws": "Unexpected token (1:15)"
}

View File

@ -0,0 +1 @@
class A extends B<T = number> {}

View File

@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected \",\" (1:20)"
}

View File

@ -0,0 +1 @@
declare function foo<T = string>() {}

View File

@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected \":\" (1:35)"
}

View File

@ -0,0 +1 @@
var x: Array<T = number>

View File

@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected \",\" (1:15)"
}

View File

@ -20,3 +20,13 @@ interface A19<T: ?string = string> {}
interface A20<S, T: ?string = string> {} interface A20<S, T: ?string = string> {}
interface A21<S = number, T: ?string = string> {} interface A21<S = number, T: ?string = string> {}
type A22<T = void> = T 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;

View File

@ -1,28 +1,28 @@
{ {
"type": "File", "type": "File",
"start": 0, "start": 0,
"end": 803, "end": 1009,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 1,
"column": 0 "column": 0
}, },
"end": { "end": {
"line": 22, "line": 32,
"column": 22 "column": 22
} }
}, },
"program": { "program": {
"type": "Program", "type": "Program",
"start": 0, "start": 0,
"end": 803, "end": 1009,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 1,
"column": 0 "column": 0
}, },
"end": { "end": {
"line": 22, "line": 32,
"column": 22 "column": 22
} }
}, },
@ -3339,6 +3339,885 @@
"name": "T" "name": "T"
} }
} }
},
{
"type": "FunctionDeclaration",
"start": 804,
"end": 833,
"loc": {
"start": {
"line": 23,
"column": 0
},
"end": {
"line": 23,
"column": 29
}
},
"id": {
"type": "Identifier",
"start": 813,
"end": 816,
"loc": {
"start": {
"line": 23,
"column": 9
},
"end": {
"line": 23,
"column": 12
},
"identifierName": "A26"
},
"name": "A26"
},
"generator": false,
"async": false,
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 816,
"end": 828,
"loc": {
"start": {
"line": 23,
"column": 12
},
"end": {
"line": 23,
"column": 24
}
},
"params": [
{
"type": "TypeParameter",
"start": 817,
"end": 827,
"loc": {
"start": {
"line": 23,
"column": 13
},
"end": {
"line": 23,
"column": 23
}
},
"name": "T",
"variance": null,
"default": {
"type": "StringTypeAnnotation",
"start": 821,
"end": 827,
"loc": {
"start": {
"line": 23,
"column": 17
},
"end": {
"line": 23,
"column": 23
}
}
}
}
]
},
"params": [],
"body": {
"type": "BlockStatement",
"start": 831,
"end": 833,
"loc": {
"start": {
"line": 23,
"column": 27
},
"end": {
"line": 23,
"column": 29
}
},
"body": [],
"directives": []
}
},
{
"type": "EmptyStatement",
"start": 834,
"end": 835,
"loc": {
"start": {
"line": 24,
"column": 0
},
"end": {
"line": 24,
"column": 1
}
}
},
{
"type": "ExpressionStatement",
"start": 835,
"end": 862,
"loc": {
"start": {
"line": 24,
"column": 1
},
"end": {
"line": 24,
"column": 28
}
},
"expression": {
"type": "ObjectExpression",
"start": 836,
"end": 860,
"loc": {
"start": {
"line": 24,
"column": 2
},
"end": {
"line": 24,
"column": 26
}
},
"properties": [
{
"type": "ObjectMethod",
"start": 838,
"end": 858,
"loc": {
"start": {
"line": 24,
"column": 4
},
"end": {
"line": 24,
"column": 24
}
},
"method": true,
"key": {
"type": "Identifier",
"start": 838,
"end": 841,
"loc": {
"start": {
"line": 24,
"column": 4
},
"end": {
"line": 24,
"column": 7
},
"identifierName": "A28"
},
"name": "A28"
},
"computed": false,
"kind": "method",
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 856,
"end": 858,
"loc": {
"start": {
"line": 24,
"column": 22
},
"end": {
"line": 24,
"column": 24
}
},
"body": [],
"directives": []
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 841,
"end": 853,
"loc": {
"start": {
"line": 24,
"column": 7
},
"end": {
"line": 24,
"column": 19
}
},
"params": [
{
"type": "TypeParameter",
"start": 842,
"end": 852,
"loc": {
"start": {
"line": 24,
"column": 8
},
"end": {
"line": 24,
"column": 18
}
},
"name": "T",
"variance": null,
"default": {
"type": "StringTypeAnnotation",
"start": 846,
"end": 852,
"loc": {
"start": {
"line": 24,
"column": 12
},
"end": {
"line": 24,
"column": 18
}
}
}
}
]
}
}
],
"extra": {
"parenthesized": true,
"parenStart": 835
}
}
},
{
"type": "ClassDeclaration",
"start": 863,
"end": 899,
"loc": {
"start": {
"line": 25,
"column": 0
},
"end": {
"line": 27,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 869,
"end": 872,
"loc": {
"start": {
"line": 25,
"column": 6
},
"end": {
"line": 25,
"column": 9
},
"identifierName": "A29"
},
"name": "A29"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 873,
"end": 899,
"loc": {
"start": {
"line": 25,
"column": 10
},
"end": {
"line": 27,
"column": 1
}
},
"body": [
{
"type": "ClassMethod",
"start": 877,
"end": 897,
"loc": {
"start": {
"line": 26,
"column": 2
},
"end": {
"line": 26,
"column": 22
}
},
"static": false,
"key": {
"type": "Identifier",
"start": 877,
"end": 880,
"loc": {
"start": {
"line": 26,
"column": 2
},
"end": {
"line": 26,
"column": 5
},
"identifierName": "foo"
},
"name": "foo"
},
"computed": false,
"kind": "method",
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 880,
"end": 892,
"loc": {
"start": {
"line": 26,
"column": 5
},
"end": {
"line": 26,
"column": 17
}
},
"params": [
{
"type": "TypeParameter",
"start": 881,
"end": 891,
"loc": {
"start": {
"line": 26,
"column": 6
},
"end": {
"line": 26,
"column": 16
}
},
"name": "T",
"variance": null,
"default": {
"type": "StringTypeAnnotation",
"start": 885,
"end": 891,
"loc": {
"start": {
"line": 26,
"column": 10
},
"end": {
"line": 26,
"column": 16
}
}
}
}
]
},
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 895,
"end": 897,
"loc": {
"start": {
"line": 26,
"column": 20
},
"end": {
"line": 26,
"column": 22
}
},
"body": [],
"directives": []
}
}
]
}
},
{
"type": "EmptyStatement",
"start": 900,
"end": 901,
"loc": {
"start": {
"line": 28,
"column": 0
},
"end": {
"line": 28,
"column": 1
}
}
},
{
"type": "ExpressionStatement",
"start": 901,
"end": 940,
"loc": {
"start": {
"line": 28,
"column": 1
},
"end": {
"line": 30,
"column": 3
}
},
"expression": {
"type": "ClassExpression",
"start": 902,
"end": 938,
"loc": {
"start": {
"line": 28,
"column": 2
},
"end": {
"line": 30,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 908,
"end": 911,
"loc": {
"start": {
"line": 28,
"column": 8
},
"end": {
"line": 28,
"column": 11
},
"identifierName": "A30"
},
"name": "A30"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 912,
"end": 938,
"loc": {
"start": {
"line": 28,
"column": 12
},
"end": {
"line": 30,
"column": 1
}
},
"body": [
{
"type": "ClassMethod",
"start": 916,
"end": 936,
"loc": {
"start": {
"line": 29,
"column": 2
},
"end": {
"line": 29,
"column": 22
}
},
"static": false,
"key": {
"type": "Identifier",
"start": 916,
"end": 919,
"loc": {
"start": {
"line": 29,
"column": 2
},
"end": {
"line": 29,
"column": 5
},
"identifierName": "foo"
},
"name": "foo"
},
"computed": false,
"kind": "method",
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 919,
"end": 931,
"loc": {
"start": {
"line": 29,
"column": 5
},
"end": {
"line": 29,
"column": 17
}
},
"params": [
{
"type": "TypeParameter",
"start": 920,
"end": 930,
"loc": {
"start": {
"line": 29,
"column": 6
},
"end": {
"line": 29,
"column": 16
}
},
"name": "T",
"variance": null,
"default": {
"type": "StringTypeAnnotation",
"start": 924,
"end": 930,
"loc": {
"start": {
"line": 29,
"column": 10
},
"end": {
"line": 29,
"column": 16
}
}
}
}
]
},
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 934,
"end": 936,
"loc": {
"start": {
"line": 29,
"column": 20
},
"end": {
"line": 29,
"column": 22
}
},
"body": [],
"directives": []
}
}
]
},
"extra": {
"parenthesized": true,
"parenStart": 901
}
}
},
{
"type": "DeclareClass",
"start": 941,
"end": 986,
"loc": {
"start": {
"line": 31,
"column": 0
},
"end": {
"line": 31,
"column": 45
}
},
"id": {
"type": "Identifier",
"start": 955,
"end": 958,
"loc": {
"start": {
"line": 31,
"column": 14
},
"end": {
"line": 31,
"column": 17
},
"identifierName": "A31"
},
"name": "A31"
},
"typeParameters": null,
"extends": [],
"implements": [],
"mixins": [],
"body": {
"type": "ObjectTypeAnnotation",
"start": 959,
"end": 986,
"loc": {
"start": {
"line": 31,
"column": 18
},
"end": {
"line": 31,
"column": 45
}
},
"callProperties": [],
"properties": [
{
"type": "ObjectTypeProperty",
"start": 961,
"end": 984,
"loc": {
"start": {
"line": 31,
"column": 20
},
"end": {
"line": 31,
"column": 43
}
},
"key": {
"type": "Identifier",
"start": 961,
"end": 964,
"loc": {
"start": {
"line": 31,
"column": 20
},
"end": {
"line": 31,
"column": 23
},
"identifierName": "foo"
},
"name": "foo"
},
"static": false,
"proto": false,
"kind": "init",
"method": true,
"value": {
"type": "FunctionTypeAnnotation",
"start": 961,
"end": 984,
"loc": {
"start": {
"line": 31,
"column": 20
},
"end": {
"line": 31,
"column": 43
}
},
"params": [],
"rest": null,
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 964,
"end": 976,
"loc": {
"start": {
"line": 31,
"column": 23
},
"end": {
"line": 31,
"column": 35
}
},
"params": [
{
"type": "TypeParameter",
"start": 965,
"end": 975,
"loc": {
"start": {
"line": 31,
"column": 24
},
"end": {
"line": 31,
"column": 34
}
},
"name": "T",
"variance": null,
"default": {
"type": "StringTypeAnnotation",
"start": 969,
"end": 975,
"loc": {
"start": {
"line": 31,
"column": 28
},
"end": {
"line": 31,
"column": 34
}
}
}
}
]
},
"returnType": {
"type": "VoidTypeAnnotation",
"start": 980,
"end": 984,
"loc": {
"start": {
"line": 31,
"column": 39
},
"end": {
"line": 31,
"column": 43
}
}
}
},
"optional": false
}
],
"indexers": [],
"internalSlots": [],
"exact": false
}
},
{
"type": "ExpressionStatement",
"start": 987,
"end": 1009,
"loc": {
"start": {
"line": 32,
"column": 0
},
"end": {
"line": 32,
"column": 22
}
},
"expression": {
"type": "ArrowFunctionExpression",
"start": 987,
"end": 1008,
"loc": {
"start": {
"line": 32,
"column": 0
},
"end": {
"line": 32,
"column": 21
}
},
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "NumericLiteral",
"start": 1005,
"end": 1008,
"loc": {
"start": {
"line": 32,
"column": 18
},
"end": {
"line": 32,
"column": 21
}
},
"extra": {
"rawValue": 123,
"raw": "123"
},
"value": 123
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 987,
"end": 999,
"loc": {
"start": {
"line": 32,
"column": 0
},
"end": {
"line": 32,
"column": 12
}
},
"params": [
{
"type": "TypeParameter",
"start": 988,
"end": 998,
"loc": {
"start": {
"line": 32,
"column": 1
},
"end": {
"line": 32,
"column": 11
}
},
"name": "T",
"variance": null,
"default": {
"type": "StringTypeAnnotation",
"start": 992,
"end": 998,
"loc": {
"start": {
"line": 32,
"column": 5
},
"end": {
"line": 32,
"column": 11
}
}
}
}
]
}
}
} }
], ],
"directives": [] "directives": []

View File

@ -9,24 +9,33 @@
# Entries should be removed incrementally as the babel parser is improved. # Entries should be removed incrementally as the babel parser is improved.
JSX_invalid/migrated_0000.js JSX_invalid/migrated_0000.js
arrow_function/tuple_return_type.js
arrow_function_invalid/migrated_0002.js arrow_function_invalid/migrated_0002.js
async_await/migrated_0007.js async_await/migrated_0007.js
async_await/migrated_0020.js async_await/migrated_0020.js
async_await/migrated_0024.js async_await/migrated_0024.js
async_await/migrated_0027.js async_await/migrated_0027.js
async_generators/migrated_0007.js async_generators/migrated_0007.js
class_properties/migrated_0000.js
class_properties/migrated_0005.js
class_properties/migrated_0011.js
class_properties/migrated_0016.js
class_properties/migrated_0021.js class_properties/migrated_0021.js
class_properties/migrated_0026.js class_properties/migrated_0026.js
decorators/migrated_0003.js decorators/migrated_0003.js
decorators/migrated_0007.js decorators/migrated_0007.js
private_class_properties/multiple.js private_class_properties/multiple.js
private_class_properties/super.js private_class_properties/super.js
private_class_properties/getter_and_field.js
private_class_properties/getter_duplicate.js
private_class_properties/setter_and_field.js
private_class_properties/setter_duplicate.js
types/member/reserved_words.js types/member/reserved_words.js
types/parameter_defaults/migrated_0032.js types/bigint_literal/migrated_0000.js
types/bigint_literal/migrated_0002.js
types/bigint_literal/migrated_0003.js
types/bigint_literal/migrated_0004.js
types/bigint_literal/migrated_0005.js
types/bigint_literal/migrated_0006.js
types/bigint_literal/migrated_0007.js
types/bigint_literal/migrated_0008.js
types/bigint_literal/migrated_0009.js
class_method_kinds/polymorphic_getter.js class_method_kinds/polymorphic_getter.js
numbers/underscored_bin.js numbers/underscored_bin.js
numbers/underscored_float.js numbers/underscored_float.js

View File

@ -113,7 +113,10 @@ const options = {
["flow", { all: true }], ["flow", { all: true }],
"flowComments", "flowComments",
"jsx", "jsx",
"classProperties",
"classPrivateProperties", "classPrivateProperties",
"classPrivateMethods",
"bigInt",
], ],
sourceType: "module", sourceType: "module",
ranges: true, ranges: true,
@ -127,6 +130,7 @@ const flowOptionsMapping = {
esproposal_nullish_coalescing: "nullishCoalescingOperator", esproposal_nullish_coalescing: "nullishCoalescingOperator",
esproposal_optional_chaining: "optionalChaining", esproposal_optional_chaining: "optionalChaining",
types: "flowComments", types: "flowComments",
intern_comments: false,
}; };
const summary = { const summary = {
@ -169,10 +173,12 @@ tests.forEach(section => {
} }
return; return;
} }
if (!flowOptionsMapping[option]) { if (!(option in flowOptionsMapping)) {
throw new Error("Parser options not mapped " + option); throw new Error("Parser options not mapped " + option);
} }
babelParserOptions.plugins.push(flowOptionsMapping[option]); if (flowOptionsMapping[option]) {
babelParserOptions.plugins.push(flowOptionsMapping[option]);
}
}); });
} }