Skip to content

Commit

Permalink
[flow][ast] add one-sided (implies) type guard variant
Browse files Browse the repository at this point in the history
Summary:
We will be adding a qualifier to type guard definition to express the notion of a "one-sided" refinement (see [relevant TS issue](microsoft/TypeScript#15048)). The new syntax will be:
```
function foo(x: T): implies x is R { ... }
```
A one-sided type guard will only refine the then-branch of a conditional.

We are adding this feature to unblock stricter [consistency checking](https://flow.org/en/docs/types/type-guards/#predicate-type-is-consistent-with-refined-type) for the else case of default type guards (two-sided). One-sided type guards continue to have the existing consistency checking. See this exchange for more context: https://twitter.com/danvdk/status/1765745099554668830.

This diff adds AST support.

Changelog: [internal]

Reviewed By: SamChou19815

Differential Revision: D56506513

fbshipit-source-id: fb2adb11ca184bbf983973fa4d7369c5aa5f7cdb
  • Loading branch information
panagosg7 authored and facebook-github-bot committed Apr 26, 2024
1 parent d1fcac6 commit 97e798a
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/parser/estree_translator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,7 @@ with type t = Impl.t = struct
match kind with
| Default -> null
| Asserts -> string "asserts"
| Implies -> string "implies"
in
node
?comments:(format_internal_comments comments)
Expand Down
1 change: 1 addition & 0 deletions src/parser/flow_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ and Type : sig
and kind =
| Default
| Asserts
| Implies

and ('M, 'T) t' = {
kind: kind;
Expand Down
1 change: 1 addition & 0 deletions src/parser_utils/output/js_layout_generator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3571,6 +3571,7 @@ and type_guard ~opts ~needs_parens guard =
match kind with
| Default -> []
| Asserts -> [Atom "asserts"]
| Implies -> [Atom "implies"]
in
let id_part = identifier x in
let type_part =
Expand Down
1 change: 1 addition & 0 deletions src/typing/errors/flow_intermediate_error.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3828,6 +3828,7 @@ let to_printable_error :
match kind with
| Flow_ast.Type.TypeGuard.Default -> "This kind of type guard is"
| Flow_ast.Type.TypeGuard.Asserts -> "Type guard assertions are"
| Flow_ast.Type.TypeGuard.Implies -> "One-sided type guards are"
in
[text (kind_str ^ " not yet supported.")]
| MessageUnsupportedSyntax (UnsupportedInternalSlot { name; static = false }) ->
Expand Down

0 comments on commit 97e798a

Please sign in to comment.