Add asserts this [is type] parsing support (#10677)

This commit is contained in:
Huáng Jùnliàng
2019-11-11 17:36:10 -05:00
committed by Brian Ng
parent 4cb5e0a013
commit d413a3078c
14 changed files with 1087 additions and 20 deletions

View File

@@ -0,0 +1,4 @@
class Foo {
isBar(): asserts this is Foo {}
isBaz = (): asserts this is Foo => {}
}

View File

@@ -0,0 +1,5 @@
{
"plugins": [
"typescript", "classProperties"
]
}

View File

@@ -0,0 +1,397 @@
{
"type": "File",
"start": 0,
"end": 87,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 87,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 87,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 9
},
"identifierName": "Foo"
},
"name": "Foo"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 10,
"end": 87,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 4,
"column": 1
}
},
"body": [
{
"type": "ClassMethod",
"start": 14,
"end": 45,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 33
}
},
"static": false,
"key": {
"type": "Identifier",
"start": 14,
"end": 19,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 7
},
"identifierName": "isBar"
},
"name": "isBar"
},
"computed": false,
"kind": "method",
"id": null,
"generator": false,
"async": false,
"params": [],
"returnType": {
"type": "TSTypeAnnotation",
"start": 21,
"end": 42,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 30
}
},
"typeAnnotation": {
"type": "TSTypePredicate",
"start": 31,
"end": 42,
"loc": {
"start": {
"line": 2,
"column": 19
},
"end": {
"line": 2,
"column": 30
}
},
"parameterName": {
"type": "TSThisType",
"start": 31,
"end": 35,
"loc": {
"start": {
"line": 2,
"column": 19
},
"end": {
"line": 2,
"column": 23
}
}
},
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 39,
"end": 42,
"loc": {
"start": {
"line": 2,
"column": 27
},
"end": {
"line": 2,
"column": 30
}
},
"typeAnnotation": {
"type": "TSTypeReference",
"start": 39,
"end": 42,
"loc": {
"start": {
"line": 2,
"column": 27
},
"end": {
"line": 2,
"column": 30
}
},
"typeName": {
"type": "Identifier",
"start": 39,
"end": 42,
"loc": {
"start": {
"line": 2,
"column": 27
},
"end": {
"line": 2,
"column": 30
},
"identifierName": "Foo"
},
"name": "Foo"
}
}
},
"asserts": true
}
},
"body": {
"type": "BlockStatement",
"start": 43,
"end": 45,
"loc": {
"start": {
"line": 2,
"column": 31
},
"end": {
"line": 2,
"column": 33
}
},
"body": [],
"directives": []
}
},
{
"type": "ClassProperty",
"start": 48,
"end": 85,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 39
}
},
"static": false,
"key": {
"type": "Identifier",
"start": 48,
"end": 53,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 7
},
"identifierName": "isBaz"
},
"name": "isBaz"
},
"computed": false,
"value": {
"type": "ArrowFunctionExpression",
"start": 56,
"end": 85,
"loc": {
"start": {
"line": 3,
"column": 10
},
"end": {
"line": 3,
"column": 39
}
},
"returnType": {
"type": "TSTypeAnnotation",
"start": 58,
"end": 79,
"loc": {
"start": {
"line": 3,
"column": 12
},
"end": {
"line": 3,
"column": 33
}
},
"typeAnnotation": {
"type": "TSTypePredicate",
"start": 68,
"end": 79,
"loc": {
"start": {
"line": 3,
"column": 22
},
"end": {
"line": 3,
"column": 33
}
},
"parameterName": {
"type": "TSThisType",
"start": 68,
"end": 72,
"loc": {
"start": {
"line": 3,
"column": 22
},
"end": {
"line": 3,
"column": 26
}
}
},
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 76,
"end": 79,
"loc": {
"start": {
"line": 3,
"column": 30
},
"end": {
"line": 3,
"column": 33
}
},
"typeAnnotation": {
"type": "TSTypeReference",
"start": 76,
"end": 79,
"loc": {
"start": {
"line": 3,
"column": 30
},
"end": {
"line": 3,
"column": 33
}
},
"typeName": {
"type": "Identifier",
"start": 76,
"end": 79,
"loc": {
"start": {
"line": 3,
"column": 30
},
"end": {
"line": 3,
"column": 33
},
"identifierName": "Foo"
},
"name": "Foo"
}
}
},
"asserts": true
}
},
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 83,
"end": 85,
"loc": {
"start": {
"line": 3,
"column": 37
},
"end": {
"line": 3,
"column": 39
}
},
"body": [],
"directives": []
}
}
}
]
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1,3 @@
class C {
m(): asserts this {};
}

View File

@@ -0,0 +1,187 @@
{
"type": "File",
"start": 0,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "C"
},
"name": "C"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 8,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ClassMethod",
"start": 12,
"end": 32,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 22
}
},
"static": false,
"key": {
"type": "Identifier",
"start": 12,
"end": 13,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 3
},
"identifierName": "m"
},
"name": "m"
},
"computed": false,
"kind": "method",
"id": null,
"generator": false,
"async": false,
"params": [],
"returnType": {
"type": "TSTypeAnnotation",
"start": 15,
"end": 29,
"loc": {
"start": {
"line": 2,
"column": 5
},
"end": {
"line": 2,
"column": 19
}
},
"typeAnnotation": {
"type": "TSTypePredicate",
"start": 15,
"end": 29,
"loc": {
"start": {
"line": 2,
"column": 5
},
"end": {
"line": 2,
"column": 19
}
},
"parameterName": {
"type": "TSThisType",
"start": 25,
"end": 29,
"loc": {
"start": {
"line": 2,
"column": 15
},
"end": {
"line": 2,
"column": 19
}
}
},
"asserts": true
}
},
"body": {
"type": "BlockStatement",
"start": 30,
"end": 32,
"loc": {
"start": {
"line": 2,
"column": 20
},
"end": {
"line": 2,
"column": 22
}
},
"body": [],
"directives": []
}
}
]
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1,3 @@
class C {
assertIsString(value: unknown): asserts value is string {}
}

View File

@@ -0,0 +1,267 @@
{
"type": "File",
"start": 0,
"end": 72,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 72,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 72,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "C"
},
"name": "C"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 8,
"end": 72,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ClassMethod",
"start": 12,
"end": 70,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 60
}
},
"static": false,
"key": {
"type": "Identifier",
"start": 12,
"end": 26,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 16
},
"identifierName": "assertIsString"
},
"name": "assertIsString"
},
"computed": false,
"kind": "method",
"id": null,
"generator": false,
"async": false,
"params": [
{
"type": "Identifier",
"start": 27,
"end": 41,
"loc": {
"start": {
"line": 2,
"column": 17
},
"end": {
"line": 2,
"column": 31
},
"identifierName": "value"
},
"name": "value",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 32,
"end": 41,
"loc": {
"start": {
"line": 2,
"column": 22
},
"end": {
"line": 2,
"column": 31
}
},
"typeAnnotation": {
"type": "TSUnknownKeyword",
"start": 34,
"end": 41,
"loc": {
"start": {
"line": 2,
"column": 24
},
"end": {
"line": 2,
"column": 31
}
}
}
}
}
],
"returnType": {
"type": "TSTypeAnnotation",
"start": 42,
"end": 67,
"loc": {
"start": {
"line": 2,
"column": 32
},
"end": {
"line": 2,
"column": 57
}
},
"typeAnnotation": {
"type": "TSTypePredicate",
"start": 42,
"end": 67,
"loc": {
"start": {
"line": 2,
"column": 32
},
"end": {
"line": 2,
"column": 57
}
},
"parameterName": {
"type": "Identifier",
"start": 52,
"end": 57,
"loc": {
"start": {
"line": 2,
"column": 42
},
"end": {
"line": 2,
"column": 47
},
"identifierName": "value"
},
"name": "value"
},
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 61,
"end": 67,
"loc": {
"start": {
"line": 2,
"column": 51
},
"end": {
"line": 2,
"column": 57
}
},
"typeAnnotation": {
"type": "TSStringKeyword",
"start": 61,
"end": 67,
"loc": {
"start": {
"line": 2,
"column": 51
},
"end": {
"line": 2,
"column": 57
}
}
}
},
"asserts": true
}
},
"body": {
"type": "BlockStatement",
"start": 68,
"end": 70,
"loc": {
"start": {
"line": 2,
"column": 58
},
"end": {
"line": 2,
"column": 60
}
},
"body": [],
"directives": []
}
}
]
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
declare function assertIsString(value: unknown): \u{61}sserts value;

View File

@@ -0,0 +1,170 @@
{
"type": "File",
"start": 0,
"end": 68,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 68
}
},
"errors": [
"SyntaxError: Escape sequence in keyword asserts (1:49)"
],
"program": {
"type": "Program",
"start": 0,
"end": 68,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 68
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSDeclareFunction",
"start": 0,
"end": 68,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 68
}
},
"id": {
"type": "Identifier",
"start": 17,
"end": 31,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 31
},
"identifierName": "assertIsString"
},
"name": "assertIsString"
},
"generator": false,
"async": false,
"params": [
{
"type": "Identifier",
"start": 32,
"end": 46,
"loc": {
"start": {
"line": 1,
"column": 32
},
"end": {
"line": 1,
"column": 46
},
"identifierName": "value"
},
"name": "value",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 37,
"end": 46,
"loc": {
"start": {
"line": 1,
"column": 37
},
"end": {
"line": 1,
"column": 46
}
},
"typeAnnotation": {
"type": "TSUnknownKeyword",
"start": 39,
"end": 46,
"loc": {
"start": {
"line": 1,
"column": 39
},
"end": {
"line": 1,
"column": 46
}
}
}
}
}
],
"returnType": {
"type": "TSTypeAnnotation",
"start": 47,
"end": 67,
"loc": {
"start": {
"line": 1,
"column": 47
},
"end": {
"line": 1,
"column": 67
}
},
"typeAnnotation": {
"type": "TSTypePredicate",
"start": 47,
"end": 67,
"loc": {
"start": {
"line": 1,
"column": 47
},
"end": {
"line": 1,
"column": 67
}
},
"parameterName": {
"type": "Identifier",
"start": 62,
"end": 67,
"loc": {
"start": {
"line": 1,
"column": 62
},
"end": {
"line": 1,
"column": 67
},
"identifierName": "value"
},
"name": "value"
},
"asserts": true
}
},
"declare": true
}
],
"directives": []
}
}