Flow opaque type aliases parsing (#643)

* Add support for Flow opaque type aliases

* Add tests for Flow opaque type aliases
This commit is contained in:
jbrown215
2017-07-21 10:48:28 -04:00
committed by Henry Zhu
parent c88af90c0a
commit e7e7593ca5
54 changed files with 1595 additions and 3 deletions

View File

@@ -0,0 +1 @@
declare opaque type ID = number;

View File

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

View File

@@ -0,0 +1,2 @@
opaque type opaque = number;
opaque type not_transparent = opaque;

View File

@@ -0,0 +1,150 @@
{
"type": "File",
"start": 0,
"end": 66,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 37
}
},
"program": {
"type": "Program",
"start": 0,
"end": 66,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 37
}
},
"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": "opaque"
},
"name": "opaque"
},
"typeParameters": null,
"supertype": null,
"impltype": {
"type": "NumberTypeAnnotation",
"start": 21,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 27
}
}
}
},
{
"type": "OpaqueType",
"start": 29,
"end": 66,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 37
}
},
"id": {
"type": "Identifier",
"start": 41,
"end": 56,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 27
},
"identifierName": "not_transparent"
},
"name": "not_transparent"
},
"typeParameters": null,
"supertype": null,
"impltype": {
"type": "GenericTypeAnnotation",
"start": 59,
"end": 65,
"loc": {
"start": {
"line": 2,
"column": 30
},
"end": {
"line": 2,
"column": 36
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 59,
"end": 65,
"loc": {
"start": {
"line": 2,
"column": 30
},
"end": {
"line": 2,
"column": 36
},
"identifierName": "opaque"
},
"name": "opaque"
}
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
declare export opaque type Foo

View File

@@ -0,0 +1,85 @@
{
"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": "DeclareExportDeclaration",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 30
}
},
"declaration": {
"type": "DeclareOpaqueType",
"start": 15,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 30
}
},
"id": {
"type": "Identifier",
"start": 27,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 27
},
"end": {
"line": 1,
"column": 30
},
"identifierName": "Foo"
},
"name": "Foo"
},
"typeParameters": null,
"supertype": null,
"impltype": null
},
"default": false
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
declare export opaque type Foo: Bar

View File

@@ -0,0 +1,117 @@
{
"type": "File",
"start": 0,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 35
}
},
"program": {
"type": "Program",
"start": 0,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 35
}
},
"sourceType": "module",
"body": [
{
"type": "DeclareExportDeclaration",
"start": 0,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 35
}
},
"declaration": {
"type": "DeclareOpaqueType",
"start": 15,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 35
}
},
"id": {
"type": "Identifier",
"start": 27,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 27
},
"end": {
"line": 1,
"column": 30
},
"identifierName": "Foo"
},
"name": "Foo"
},
"typeParameters": null,
"supertype": {
"type": "GenericTypeAnnotation",
"start": 32,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 32
},
"end": {
"line": 1,
"column": 35
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 32,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 32
},
"end": {
"line": 1,
"column": 35
},
"identifierName": "Bar"
},
"name": "Bar"
}
},
"impltype": null
},
"default": false
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
declare export opaque type Foo: Bar = Baz

View File

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

View File

@@ -0,0 +1 @@
declare export opaque type Foo = Bar

View File

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

View File

@@ -0,0 +1 @@
declare opaque type Foo

View File

@@ -0,0 +1,69 @@
{
"type": "File",
"start": 0,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 23
}
},
"program": {
"type": "Program",
"start": 0,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 23
}
},
"sourceType": "module",
"body": [
{
"type": "DeclareOpaqueType",
"start": 0,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 23
}
},
"id": {
"type": "Identifier",
"start": 20,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 20
},
"end": {
"line": 1,
"column": 23
},
"identifierName": "Foo"
},
"name": "Foo"
},
"typeParameters": null,
"supertype": null,
"impltype": null
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
declare opaque type Test: Foo

View File

@@ -0,0 +1,101 @@
{
"type": "File",
"start": 0,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 29
}
},
"program": {
"type": "Program",
"start": 0,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 29
}
},
"sourceType": "module",
"body": [
{
"type": "DeclareOpaqueType",
"start": 0,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 29
}
},
"id": {
"type": "Identifier",
"start": 20,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 20
},
"end": {
"line": 1,
"column": 24
},
"identifierName": "Test"
},
"name": "Test"
},
"typeParameters": null,
"supertype": {
"type": "GenericTypeAnnotation",
"start": 26,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 26
},
"end": {
"line": 1,
"column": 29
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 26,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 26
},
"end": {
"line": 1,
"column": 29
},
"identifierName": "Foo"
},
"name": "Foo"
}
},
"impltype": null
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
declare opaque type Foo: Bar = Baz

View File

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

View File

@@ -0,0 +1 @@
declare opaque type Foo = Bar

View File

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

View File

@@ -0,0 +1,2 @@
var opaque = 0;
opaque += 4;

View File

@@ -0,0 +1,172 @@
{
"type": "File",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 12
}
},
"program": {
"type": "Program",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 12
}
},
"sourceType": "module",
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 15,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 15
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 4,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 14
}
},
"id": {
"type": "Identifier",
"start": 4,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 10
},
"identifierName": "opaque"
},
"name": "opaque"
},
"init": {
"type": "NumericLiteral",
"start": 13,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 14
}
},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
}
],
"kind": "var"
},
{
"type": "ExpressionStatement",
"start": 16,
"end": 28,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 12
}
},
"expression": {
"type": "AssignmentExpression",
"start": 16,
"end": 27,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 11
}
},
"operator": "+=",
"left": {
"type": "Identifier",
"start": 16,
"end": 22,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 6
},
"identifierName": "opaque"
},
"name": "opaque"
},
"right": {
"type": "NumericLiteral",
"start": 26,
"end": 27,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 2,
"column": 11
}
},
"extra": {
"rawValue": 4,
"raw": "4"
},
"value": 4
}
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
opaque(0);

View File

@@ -0,0 +1,103 @@
{
"type": "File",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"program": {
"type": "Program",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"sourceType": "module",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"expression": {
"type": "CallExpression",
"start": 0,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 9
}
},
"callee": {
"type": "Identifier",
"start": 0,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 6
},
"identifierName": "opaque"
},
"name": "opaque"
},
"arguments": [
{
"type": "NumericLiteral",
"start": 7,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 8
}
},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
]
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
export opaque potato ID = number;

View File

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

View File

@@ -0,0 +1 @@
opaque potato ID = number;

View File

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

View File

@@ -0,0 +1 @@
declare export opaque = potato;

View File

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

View File

@@ -0,0 +1 @@
declare export opaque type ID = number;

View File

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

View File

@@ -0,0 +1 @@
declare export opaque potato ID = number;

View File

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

View File

@@ -0,0 +1 @@
opaque type Counter: Box<T> = Container<T>;

View File

@@ -0,0 +1,231 @@
{
"type": "File",
"start": 0,
"end": 43,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 43
}
},
"program": {
"type": "Program",
"start": 0,
"end": 43,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 43
}
},
"sourceType": "module",
"body": [
{
"type": "OpaqueType",
"start": 0,
"end": 43,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 43
}
},
"id": {
"type": "Identifier",
"start": 12,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 19
},
"identifierName": "Counter"
},
"name": "Counter"
},
"typeParameters": null,
"supertype": {
"type": "GenericTypeAnnotation",
"start": 21,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 27
}
},
"typeParameters": {
"type": "TypeParameterInstantiation",
"start": 24,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 24
},
"end": {
"line": 1,
"column": 27
}
},
"params": [
{
"type": "GenericTypeAnnotation",
"start": 25,
"end": 26,
"loc": {
"start": {
"line": 1,
"column": 25
},
"end": {
"line": 1,
"column": 26
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 25,
"end": 26,
"loc": {
"start": {
"line": 1,
"column": 25
},
"end": {
"line": 1,
"column": 26
},
"identifierName": "T"
},
"name": "T"
}
}
]
},
"id": {
"type": "Identifier",
"start": 21,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 24
},
"identifierName": "Box"
},
"name": "Box"
}
},
"impltype": {
"type": "GenericTypeAnnotation",
"start": 30,
"end": 42,
"loc": {
"start": {
"line": 1,
"column": 30
},
"end": {
"line": 1,
"column": 42
}
},
"typeParameters": {
"type": "TypeParameterInstantiation",
"start": 39,
"end": 42,
"loc": {
"start": {
"line": 1,
"column": 39
},
"end": {
"line": 1,
"column": 42
}
},
"params": [
{
"type": "GenericTypeAnnotation",
"start": 40,
"end": 41,
"loc": {
"start": {
"line": 1,
"column": 40
},
"end": {
"line": 1,
"column": 41
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 40,
"end": 41,
"loc": {
"start": {
"line": 1,
"column": 40
},
"end": {
"line": 1,
"column": 41
},
"identifierName": "T"
},
"name": "T"
}
}
]
},
"id": {
"type": "Identifier",
"start": 30,
"end": 39,
"loc": {
"start": {
"line": 1,
"column": 30
},
"end": {
"line": 1,
"column": 39
},
"identifierName": "Container"
},
"name": "Container"
}
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1,3 @@
declare module 'foo' {
declare export opaque type MyType: number = string;
}

View File

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

View File

@@ -0,0 +1 @@
export opaque type Counter: Box<T> = Container<S>;

View File

@@ -0,0 +1,249 @@
{
"type": "File",
"start": 0,
"end": 50,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 50
}
},
"program": {
"type": "Program",
"start": 0,
"end": 50,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 50
}
},
"sourceType": "module",
"body": [
{
"type": "ExportNamedDeclaration",
"start": 0,
"end": 50,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 50
}
},
"specifiers": [],
"source": null,
"exportKind": "type",
"declaration": {
"type": "OpaqueType",
"start": 7,
"end": 50,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 50
}
},
"id": {
"type": "Identifier",
"start": 19,
"end": 26,
"loc": {
"start": {
"line": 1,
"column": 19
},
"end": {
"line": 1,
"column": 26
},
"identifierName": "Counter"
},
"name": "Counter"
},
"typeParameters": null,
"supertype": {
"type": "GenericTypeAnnotation",
"start": 28,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 28
},
"end": {
"line": 1,
"column": 34
}
},
"typeParameters": {
"type": "TypeParameterInstantiation",
"start": 31,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 31
},
"end": {
"line": 1,
"column": 34
}
},
"params": [
{
"type": "GenericTypeAnnotation",
"start": 32,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 32
},
"end": {
"line": 1,
"column": 33
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 32,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 32
},
"end": {
"line": 1,
"column": 33
},
"identifierName": "T"
},
"name": "T"
}
}
]
},
"id": {
"type": "Identifier",
"start": 28,
"end": 31,
"loc": {
"start": {
"line": 1,
"column": 28
},
"end": {
"line": 1,
"column": 31
},
"identifierName": "Box"
},
"name": "Box"
}
},
"impltype": {
"type": "GenericTypeAnnotation",
"start": 37,
"end": 49,
"loc": {
"start": {
"line": 1,
"column": 37
},
"end": {
"line": 1,
"column": 49
}
},
"typeParameters": {
"type": "TypeParameterInstantiation",
"start": 46,
"end": 49,
"loc": {
"start": {
"line": 1,
"column": 46
},
"end": {
"line": 1,
"column": 49
}
},
"params": [
{
"type": "GenericTypeAnnotation",
"start": 47,
"end": 48,
"loc": {
"start": {
"line": 1,
"column": 47
},
"end": {
"line": 1,
"column": 48
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 47,
"end": 48,
"loc": {
"start": {
"line": 1,
"column": 47
},
"end": {
"line": 1,
"column": 48
},
"identifierName": "S"
},
"name": "S"
}
}
]
},
"id": {
"type": "Identifier",
"start": 37,
"end": 46,
"loc": {
"start": {
"line": 1,
"column": 37
},
"end": {
"line": 1,
"column": 46
},
"identifierName": "Container"
},
"name": "Container"
}
}
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
opaque Ident: Type = SuperType;

View File

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

View File

@@ -0,0 +1 @@
export opaque potato: ID = number;

View File

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

View File

@@ -0,0 +1 @@
opaque stuff: Box<T> = Container<T>

View File

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

View File

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

View File

@@ -0,0 +1,83 @@
{
"type": "File",
"start": 0,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 24
}
},
"program": {
"type": "Program",
"start": 0,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 24
}
},
"sourceType": "module",
"body": [
{
"type": "OpaqueType",
"start": 0,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 24
}
},
"id": {
"type": "Identifier",
"start": 12,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 14
},
"identifierName": "ID"
},
"name": "ID"
},
"typeParameters": null,
"supertype": null,
"impltype": {
"type": "NumberTypeAnnotation",
"start": 17,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 23
}
}
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1,3 @@
declare module 'foo' {
declare export opaque type MyType = string;
}

View File

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

View File

@@ -0,0 +1 @@
export opaque type ID = number;

View File

@@ -0,0 +1,101 @@
{
"type": "File",
"start": 0,
"end": 31,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 31
}
},
"program": {
"type": "Program",
"start": 0,
"end": 31,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 31
}
},
"sourceType": "module",
"body": [
{
"type": "ExportNamedDeclaration",
"start": 0,
"end": 31,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 31
}
},
"specifiers": [],
"source": null,
"exportKind": "type",
"declaration": {
"type": "OpaqueType",
"start": 7,
"end": 31,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 31
}
},
"id": {
"type": "Identifier",
"start": 19,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 19
},
"end": {
"line": 1,
"column": 21
},
"identifierName": "ID"
},
"name": "ID"
},
"typeParameters": null,
"supertype": null,
"impltype": {
"type": "NumberTypeAnnotation",
"start": 24,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 24
},
"end": {
"line": 1,
"column": 30
}
}
}
}
}
],
"directives": []
}
}