Skip to content

Commit

Permalink
add a type guard kind property
Browse files Browse the repository at this point in the history
Summary: Instead of having an `asserts: true/false` property in `TypePredicate`, we now have a `kind` property that is `null` for the default case, and `"asserts"` for the assertion case. In the coming diff we'll add another variant to this.

Reviewed By: avp

Differential Revision: D56595627

fbshipit-source-id: 9282a1cd89842060599930d472c22027f666b927
  • Loading branch information
panagosg7 authored and facebook-github-bot committed Apr 30, 2024
1 parent 0888914 commit 79e253b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion include/hermes/AST/ESTree.def
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ ESTREE_NODE_3_ARGS(
TypePredicate, Flow,
NodePtr, parameterName, false,
NodePtr, typeAnnotation, true,
NodeBoolean, asserts, false)
NodeString, kind, true)

ESTREE_NODE_2_ARGS(
InterfaceTypeAnnotation, Flow,
Expand Down
5 changes: 3 additions & 2 deletions lib/Parser/JSParserImpl-flow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1633,7 +1633,8 @@ Optional<ESTree::Node *> JSParserImpl::parseReturnTypeAnnotationFlow(
returnType = setLocation(
start,
getPrevTokenEndLoc(),
new (context_) ESTree::TypePredicateNode(id, typeAnnotation, true));
new (context_)
ESTree::TypePredicateNode(id, typeAnnotation, assertsIdent_));
} else {
returnType = *optType;
}
Expand All @@ -1656,7 +1657,7 @@ Optional<ESTree::Node *> JSParserImpl::parseReturnTypeAnnotationFlow(
returnType = setLocation(
start,
getPrevTokenEndLoc(),
new (context_) ESTree::TypePredicateNode(*optId, *optType, false));
new (context_) ESTree::TypePredicateNode(*optId, *optType, nullptr));
} else {
returnType = *optType;
}
Expand Down
16 changes: 8 additions & 8 deletions test/Parser/flow/predicate-asserts.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function foo(x: mixed): asserts x is number {}
// CHECK-NEXT: "typeAnnotation": {
// CHECK-NEXT: "type": "NumberTypeAnnotation"
// CHECK-NEXT: },
// CHECK-NEXT: "asserts": true
// CHECK-NEXT: "kind": "asserts"
// CHECK-NEXT: }
// CHECK-NEXT: },
// CHECK-NEXT: "generator": false,
Expand Down Expand Up @@ -84,7 +84,7 @@ function foo(x: mixed): asserts x {}
// CHECK-NEXT: "name": "x"
// CHECK-NEXT: },
// CHECK-NEXT: "typeAnnotation": null,
// CHECK-NEXT: "asserts": true
// CHECK-NEXT: "kind": "asserts"
// CHECK-NEXT: }
// CHECK-NEXT: },
// CHECK-NEXT: "generator": false,
Expand Down Expand Up @@ -124,7 +124,7 @@ function foo(x: mixed): asserts x {}
// CHECK-NEXT: "typeAnnotation": {
// CHECK-NEXT: "type": "NumberTypeAnnotation"
// CHECK-NEXT: },
// CHECK-NEXT: "asserts": true
// CHECK-NEXT: "kind": "asserts"
// CHECK-NEXT: }
// CHECK-NEXT: },
// CHECK-NEXT: "expression": true,
Expand Down Expand Up @@ -164,7 +164,7 @@ function foo(x: mixed): asserts x {}
// CHECK-NEXT: "name": "x"
// CHECK-NEXT: },
// CHECK-NEXT: "typeAnnotation": null,
// CHECK-NEXT: "asserts": true
// CHECK-NEXT: "kind": "asserts"
// CHECK-NEXT: }
// CHECK-NEXT: },
// CHECK-NEXT: "expression": true,
Expand Down Expand Up @@ -229,7 +229,7 @@ class C { m(): asserts x is D { return x !== null } }
// CHECK-NEXT: },
// CHECK-NEXT: "typeParameters": null
// CHECK-NEXT: },
// CHECK-NEXT: "asserts": true
// CHECK-NEXT: "kind": "asserts"
// CHECK-NEXT: }
// CHECK-NEXT: },
// CHECK-NEXT: "generator": false,
Expand Down Expand Up @@ -292,7 +292,7 @@ class C { m(): asserts x { return x !== null } }
// CHECK-NEXT: "name": "x"
// CHECK-NEXT: },
// CHECK-NEXT: "typeAnnotation": null,
// CHECK-NEXT: "asserts": true
// CHECK-NEXT: "kind": "asserts"
// CHECK-NEXT: }
// CHECK-NEXT: },
// CHECK-NEXT: "generator": false,
Expand Down Expand Up @@ -339,7 +339,7 @@ type T = (x: mixed) => asserts x is number;
// CHECK-NEXT: "typeAnnotation": {
// CHECK-NEXT: "type": "NumberTypeAnnotation"
// CHECK-NEXT: },
// CHECK-NEXT: "asserts": true
// CHECK-NEXT: "kind": "asserts"
// CHECK-NEXT: },
// CHECK-NEXT: "rest": null,
// CHECK-NEXT: "typeParameters": null
Expand Down Expand Up @@ -377,7 +377,7 @@ type T = (x: mixed) => asserts x;
// CHECK-NEXT: "name": "x"
// CHECK-NEXT: },
// CHECK-NEXT: "typeAnnotation": null,
// CHECK-NEXT: "asserts": true
// CHECK-NEXT: "kind": "asserts"
// CHECK-NEXT: },
// CHECK-NEXT: "rest": null,
// CHECK-NEXT: "typeParameters": null
Expand Down
16 changes: 8 additions & 8 deletions test/Parser/flow/predicate-is.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function foo(x: mixed): x is number {}
// CHECK-NEXT: "typeAnnotation": {
// CHECK-NEXT: "type": "NumberTypeAnnotation"
// CHECK-NEXT: },
// CHECK-NEXT: "asserts": false
// CHECK-NEXT: "kind": null
// CHECK-NEXT: }
// CHECK-NEXT: },
// CHECK-NEXT: "generator": false,
Expand Down Expand Up @@ -85,7 +85,7 @@ declare function foo(x: mixed): x is number;
// CHECK-NEXT: "typeAnnotation": {
// CHECK-NEXT: "type": "NumberTypeAnnotation"
// CHECK-NEXT: },
// CHECK-NEXT: "asserts": false
// CHECK-NEXT: "kind": null
// CHECK-NEXT: },
// CHECK-NEXT: "rest": null,
// CHECK-NEXT: "typeParameters": null
Expand Down Expand Up @@ -131,7 +131,7 @@ declare function foo(x: mixed): x is number;
// CHECK-NEXT: "typeAnnotation": {
// CHECK-NEXT: "type": "NumberTypeAnnotation"
// CHECK-NEXT: },
// CHECK-NEXT: "asserts": false
// CHECK-NEXT: "kind": null
// CHECK-NEXT: }
// CHECK-NEXT: },
// CHECK-NEXT: "generator": false,
Expand Down Expand Up @@ -173,7 +173,7 @@ declare function foo(x: mixed): x is number;
// CHECK-NEXT: "typeAnnotation": {
// CHECK-NEXT: "type": "NumberTypeAnnotation"
// CHECK-NEXT: },
// CHECK-NEXT: "asserts": false
// CHECK-NEXT: "kind": null
// CHECK-NEXT: }
// CHECK-NEXT: },
// CHECK-NEXT: "expression": true,
Expand Down Expand Up @@ -215,7 +215,7 @@ async (x: mixed): x is number => true;
// CHECK-NEXT: "typeAnnotation": {
// CHECK-NEXT: "type": "NumberTypeAnnotation"
// CHECK-NEXT: },
// CHECK-NEXT: "asserts": false
// CHECK-NEXT: "kind": null
// CHECK-NEXT: }
// CHECK-NEXT: },
// CHECK-NEXT: "expression": true,
Expand Down Expand Up @@ -280,7 +280,7 @@ class C { m(): x is D { return x !== null } }
// CHECK-NEXT: },
// CHECK-NEXT: "typeParameters": null
// CHECK-NEXT: },
// CHECK-NEXT: "asserts": false
// CHECK-NEXT: "kind": null
// CHECK-NEXT: }
// CHECK-NEXT: },
// CHECK-NEXT: "generator": false,
Expand Down Expand Up @@ -350,7 +350,7 @@ class C { m(): this is D { return x !== null } }
// CHECK-NEXT: },
// CHECK-NEXT: "typeParameters": null
// CHECK-NEXT: },
// CHECK-NEXT: "asserts": false
// CHECK-NEXT: "kind": null
// CHECK-NEXT: }
// CHECK-NEXT: },
// CHECK-NEXT: "generator": false,
Expand Down Expand Up @@ -397,7 +397,7 @@ type T = (x: mixed) => x is number;
// CHECK-NEXT: "typeAnnotation": {
// CHECK-NEXT: "type": "NumberTypeAnnotation"
// CHECK-NEXT: },
// CHECK-NEXT: "asserts": false
// CHECK-NEXT: "kind": null
// CHECK-NEXT: },
// CHECK-NEXT: "rest": null,
// CHECK-NEXT: "typeParameters": null
Expand Down

0 comments on commit 79e253b

Please sign in to comment.