From 812dd64574aea8a088fe515a96f167b370f7b86c Mon Sep 17 00:00:00 2001 From: Panos Vekris Date: Thu, 2 May 2024 11:26:39 -0700 Subject: [PATCH] [flow][as-const] hints propagate through "as const" Summary: Changelog: [internal] Reviewed By: SamChou19815 Differential Revision: D56888740 fbshipit-source-id: b6f972716b5bedeaaf337eb03a4c74fa3c7c872f --- src/analysis/env_builder/name_def.ml | 6 ++++-- tests/as_const/.flowconfig | 1 + tests/as_const/hint.js | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 tests/as_const/hint.js diff --git a/src/analysis/env_builder/name_def.ml b/src/analysis/env_builder/name_def.ml index 60fd69fdba6..44bc2441e44 100644 --- a/src/analysis/env_builder/name_def.ml +++ b/src/analysis/env_builder/name_def.ml @@ -568,6 +568,8 @@ let expression_is_definitely_synthesizable ~autocomplete_hooks = | Ast.Expression.TaggedTemplate _ -> false | Ast.Expression.Identifier id -> not (identifier_has_autocomplete ~autocomplete_hooks id) | Ast.Expression.StringLiteral _ -> not (literal_has_autocomplete ~autocomplete_hooks loc) + | Ast.Expression.AsConstExpression { Ast.Expression.AsConstExpression.expression; _ } -> + synthesizable expression | Ast.Expression.NumberLiteral _ | Ast.Expression.BooleanLiteral _ | Ast.Expression.NullLiteral _ @@ -587,7 +589,6 @@ let expression_is_definitely_synthesizable ~autocomplete_hooks = | Ast.Expression.TemplateLiteral _ | Ast.Expression.This _ | Ast.Expression.TypeCast _ - | Ast.Expression.AsConstExpression _ | Ast.Expression.AsExpression _ | Ast.Expression.TSSatisfies _ | Ast.Expression.Update _ @@ -2768,6 +2769,8 @@ class def_finder ~autocomplete_hooks env_info toplevel_scope = | Ast.Expression.New expr -> this#visit_new_expression ~hints loc expr | Ast.Expression.Unary expr -> this#visit_unary_expression ~hints expr | Ast.Expression.Conditional expr -> this#visit_conditional ~hints expr + | Ast.Expression.AsConstExpression { Ast.Expression.AsConstExpression.expression; _ } -> + this#visit_expression ~hints ~cond expression | Ast.Expression.Class _ | Ast.Expression.Identifier _ | Ast.Expression.Import _ @@ -2788,7 +2791,6 @@ class def_finder ~autocomplete_hooks env_info toplevel_scope = | Ast.Expression.This _ | Ast.Expression.TypeCast _ | Ast.Expression.AsExpression _ - | Ast.Expression.AsConstExpression _ | Ast.Expression.TSSatisfies _ | Ast.Expression.Update _ | Ast.Expression.Yield _ -> diff --git a/tests/as_const/.flowconfig b/tests/as_const/.flowconfig index e71c822f78e..484cee84fee 100644 --- a/tests/as_const/.flowconfig +++ b/tests/as_const/.flowconfig @@ -1,3 +1,4 @@ [options] all=true as_const=true +experimental.ts_syntax=true diff --git a/tests/as_const/hint.js b/tests/as_const/hint.js new file mode 100644 index 00000000000..2339ccbeca8 --- /dev/null +++ b/tests/as_const/hint.js @@ -0,0 +1,3 @@ +// hints propagate through "as const" +const arr: readonly [(x: string) => void] = [(x) => {}] as const; // okay +const obj: {+f: (x: string) => void} = {f: (x) => {}} as const; // okay