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

@ -680,6 +680,15 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.finishNode(node, "TSLiteralType"); return this.finishNode(node, "TSLiteralType");
} }
tsParseThisTypeOrThisTypePredicate(): N.TsThisType | N.TsTypePredicate {
const thisKeyword = this.tsParseThisTypeNode();
if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
return this.tsParseThisTypePredicate(thisKeyword);
} else {
return thisKeyword;
}
}
tsParseNonArrayType(): N.TsType { tsParseNonArrayType(): N.TsType {
switch (this.state.type) { switch (this.state.type) {
case tt.name: case tt.name:
@ -715,14 +724,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.finishNode(node, "TSLiteralType"); return this.finishNode(node, "TSLiteralType");
} }
break; break;
case tt._this: { case tt._this:
const thisKeyword = this.tsParseThisTypeNode(); return this.tsParseThisTypeOrThisTypePredicate();
if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
return this.tsParseThisTypePredicate(thisKeyword);
} else {
return thisKeyword;
}
}
case tt._typeof: case tt._typeof:
return this.tsParseTypeQuery(); return this.tsParseTypeQuery();
case tt._import: case tt._import:
@ -937,6 +940,24 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.tsParseTypePredicateAsserts.bind(this), this.tsParseTypePredicateAsserts.bind(this),
); );
if (asserts && this.match(tt._this)) {
// When asserts is false, thisKeyword is handled by tsParseNonArrayType
// : asserts this is type
let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
// if it turns out to be a `TSThisType`, wrap it with `TSTypePredicate`
// : asserts this
if (thisTypePredicate.type === "TSThisType") {
const node: N.TsTypePredicate = this.startNodeAtNode(t);
node.parameterName = (thisTypePredicate: N.TsThisType);
node.asserts = true;
thisTypePredicate = this.finishNode(node, "TSTypePredicate");
} else {
(thisTypePredicate: N.TsTypePredicate).asserts = true;
}
t.typeAnnotation = thisTypePredicate;
return this.finishNode(t, "TSTypeAnnotation");
}
const typePredicateVariable = const typePredicateVariable =
this.tsIsIdentifier() && this.tsIsIdentifier() &&
this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this)); this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
@ -947,15 +968,15 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.tsParseTypeAnnotation(/* eatColon */ false, t); return this.tsParseTypeAnnotation(/* eatColon */ false, t);
} }
const node: N.TsTypePredicate = this.startNodeAtNode(t);
// : asserts foo // : asserts foo
const node = this.startNodeAtNode(t);
node.parameterName = this.parseIdentifier(); node.parameterName = this.parseIdentifier();
node.asserts = asserts; node.asserts = asserts;
t.typeAnnotation = this.finishNode(node, "TSTypePredicate"); t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
return this.finishNode(t, "TSTypeAnnotation"); return this.finishNode(t, "TSTypeAnnotation");
} }
// : foo is type // : asserts foo is type
const type = this.tsParseTypeAnnotation(/* eatColon */ false); const type = this.tsParseTypeAnnotation(/* eatColon */ false);
const node = this.startNodeAtNode(t); const node = this.startNodeAtNode(t);
node.parameterName = typePredicateVariable; node.parameterName = typePredicateVariable;
@ -989,17 +1010,24 @@ export default (superClass: Class<Parser>): Class<Parser> =>
} }
tsParseTypePredicateAsserts(): boolean { tsParseTypePredicateAsserts(): boolean {
if (!this.tsIsIdentifier()) { if (
!this.match(tt.name) ||
this.state.value !== "asserts" ||
this.hasPrecedingLineBreak()
) {
return false;
}
const containsEsc = this.state.containsEsc;
this.next();
if (!this.match(tt.name) && !this.match(tt._this)) {
return false; return false;
} }
const id = this.parseIdentifier(); if (containsEsc) {
if ( this.raise(
id.name !== "asserts" || this.state.lastTokStart,
this.hasPrecedingLineBreak() || "Escape sequence in keyword asserts",
!this.tsIsIdentifier() );
) {
return false;
} }
return true; return true;

View File

@ -1227,6 +1227,7 @@ export type TsTypePredicate = TsTypeBase & {
type: "TSTypePredicate", type: "TSTypePredicate",
parameterName: Identifier | TsThisType, parameterName: Identifier | TsThisType,
typeAnnotation: TsTypeAnnotation, typeAnnotation: TsTypeAnnotation,
asserts?: boolean,
}; };
// `typeof` operator // `typeof` operator

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": []
}
}

View File

@ -175,11 +175,12 @@ defineType("TSTypeReference", {
defineType("TSTypePredicate", { defineType("TSTypePredicate", {
aliases: ["TSType"], aliases: ["TSType"],
visitor: ["parameterName", "typeAnnotation", "asserts"], visitor: ["parameterName", "typeAnnotation"],
builder: ["parameterName", "typeAnnotation", "asserts"],
fields: { fields: {
parameterName: validateType(["Identifier", "TSThisType"]), parameterName: validateType(["Identifier", "TSThisType"]),
typeAnnotation: validateOptionalType("TSTypeAnnotation"), typeAnnotation: validateOptionalType("TSTypeAnnotation"),
asserts: validate(bool), asserts: validateOptional(bool),
}, },
}); });