Fix some reserved type handling and declare class with multiple extends (#6725)

This commit is contained in:
Brian Ng
2017-11-15 16:16:15 -06:00
committed by GitHub
parent de3597983a
commit 0f2ab2fe20
17 changed files with 315 additions and 8 deletions

View File

@@ -123,7 +123,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
flowParseDeclareClass(node: N.FlowDeclareClass): N.FlowDeclareClass {
this.next();
this.flowParseInterfaceish(node);
this.flowParseInterfaceish(node, /*isClass*/ true);
return this.finishNode(node, "DeclareClass");
}
@@ -392,8 +392,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// Interfaces
flowParseInterfaceish(node: N.FlowDeclare): void {
node.id = this.parseIdentifier();
flowParseInterfaceish(node: N.FlowDeclare, isClass?: boolean): void {
node.id = this.flowParseRestrictedIdentifier(/*liberal*/ !isClass);
if (this.isRelational("<")) {
node.typeParameters = this.flowParseTypeParameterDeclaration();
@@ -407,7 +407,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (this.eat(tt._extends)) {
do {
node.extends.push(this.flowParseInterfaceExtends());
} while (this.eat(tt.comma));
} while (!isClass && this.eat(tt.comma));
}
if (this.isContextual("mixins")) {
@@ -471,7 +471,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
declare: boolean,
): N.FlowOpaqueType {
this.expectContextual("type");
node.id = this.flowParseRestrictedIdentifier();
node.id = this.flowParseRestrictedIdentifier(/*liberal*/ true);
if (this.isRelational("<")) {
node.typeParameters = this.flowParseTypeParameterDeclaration();
@@ -1780,7 +1780,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
const implemented: N.FlowClassImplements[] = (node.implements = []);
do {
const node = this.startNode();
node.id = this.parseIdentifier();
node.id = this.flowParseRestrictedIdentifier(/*liberal*/ true);
if (this.isRelational("<")) {
node.typeParameters = this.flowParseTypeParameterInstantiation();
} else {

View File

@@ -0,0 +1 @@
declare class A extends B, C {}

View File

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

View File

@@ -0,0 +1 @@
interface string {}

View File

@@ -0,0 +1,3 @@
{
"throws": "Cannot overwrite primitive type string (1:10)"
}

View File

@@ -0,0 +1 @@
interface switch {}

View File

@@ -0,0 +1,88 @@
{
"type": "File",
"start": 0,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 19
}
},
"program": {
"type": "Program",
"start": 0,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 19
}
},
"sourceType": "module",
"body": [
{
"type": "InterfaceDeclaration",
"start": 0,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 19
}
},
"id": {
"type": "Identifier",
"start": 10,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 16
},
"identifierName": "switch"
},
"name": "switch"
},
"typeParameters": null,
"extends": [],
"mixins": [],
"body": {
"type": "ObjectTypeAnnotation",
"start": 17,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 19
}
},
"callProperties": [],
"properties": [],
"indexers": [],
"exact": false
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
class Foo implements string {}

View File

@@ -0,0 +1,3 @@
{
"throws": "Cannot overwrite primitive type string (1:21)"
}

View File

@@ -0,0 +1 @@
class Foo implements switch {}

View File

@@ -0,0 +1,118 @@
{
"type": "File",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 30
}
},
"program": {
"type": "Program",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 30
}
},
"sourceType": "module",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 30
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 9
},
"identifierName": "Foo"
},
"name": "Foo"
},
"superClass": null,
"implements": [
{
"type": "ClassImplements",
"start": 21,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 27
}
},
"id": {
"type": "Identifier",
"start": 21,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 27
},
"identifierName": "switch"
},
"name": "switch"
},
"typeParameters": null
}
],
"body": {
"type": "ClassBody",
"start": 28,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 28
},
"end": {
"line": 1,
"column": 30
}
},
"body": []
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
opaque type string = number;

View File

@@ -0,0 +1,3 @@
{
"throws": "Cannot overwrite primitive type string (1:12)"
}

View File

@@ -0,0 +1 @@
opaque type switch = number;

View File

@@ -0,0 +1,83 @@
{
"type": "File",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 28
}
},
"program": {
"type": "Program",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 28
}
},
"sourceType": "module",
"body": [
{
"type": "OpaqueType",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 28
}
},
"id": {
"type": "Identifier",
"start": 12,
"end": 18,
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 18
},
"identifierName": "switch"
},
"name": "switch"
},
"typeParameters": null,
"supertype": null,
"impltype": {
"type": "NumberTypeAnnotation",
"start": 21,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 27
}
}
}
}
],
"directives": []
}
}