From 7df19bfd91c75e78daed6f5ed24f42d4ec44e33d Mon Sep 17 00:00:00 2001 From: George Zahariev Date: Thu, 2 May 2024 00:52:39 -0700 Subject: [PATCH] [flow][tuples] Add option for gating inexact tuple types syntax Summary: Changelog: [internal] The boilerplate of adding an option to gate the future implementation of inexact tuple types. Reviewed By: SamChou19815 Differential Revision: D56713880 fbshipit-source-id: d73f3eb47751e55bdabaeeee32574cf3e899592e --- src/commands/commandUtils.ml | 1 + src/commands/config/flowConfig.ml | 7 +++++++ src/commands/config/flowConfig.mli | 2 ++ src/common/options.ml | 3 +++ src/flow_dot_js.ml | 1 + .../code_action/__tests__/refactor_extract_utils_tests.ml | 1 + src/typing/__tests__/type_hint_test.ml | 1 + src/typing/__tests__/typed_ast_test.ml | 1 + src/typing/context.ml | 4 ++++ src/typing/context.mli | 3 +++ src/typing/type_annotation.ml | 2 +- src/typing/type_sig_merge.ml | 2 +- tests/inexact_tuples_unsupported/.flowconfig | 1 + tests/tuples/.flowconfig | 1 + tests/tuples/inexact.js | 1 + 15 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 tests/tuples/inexact.js diff --git a/src/commands/commandUtils.ml b/src/commands/commandUtils.ml index e4bdf8853f8..82b55734497 100644 --- a/src/commands/commandUtils.ml +++ b/src/commands/commandUtils.ml @@ -1396,6 +1396,7 @@ let make_options options_flags.include_warnings || options_flags.max_warnings <> None || FlowConfig.include_warnings flowconfig; + opt_inexact_tuple_types_syntax = FlowConfig.inexact_tuple_types_syntax flowconfig; opt_max_header_tokens = FlowConfig.max_header_tokens flowconfig; opt_haste_module_ref_prefix = FlowConfig.haste_module_ref_prefix flowconfig; opt_haste_module_ref_prefix_LEGACY_INTEROP = diff --git a/src/commands/config/flowConfig.ml b/src/commands/config/flowConfig.ml index 387f3c83ca1..9365bcfe6a7 100644 --- a/src/commands/config/flowConfig.ml +++ b/src/commands/config/flowConfig.ml @@ -91,6 +91,7 @@ module Opts = struct haste_paths_includes: string list; ignore_non_literal_requires: bool; include_warnings: bool; + inexact_tuple_types_syntax: bool; lazy_mode: lazy_mode option; log_saving: Options.log_saving SMap.t; long_lived_workers: bool; @@ -221,6 +222,7 @@ module Opts = struct haste_paths_includes = ["/.*"]; ignore_non_literal_requires = false; include_warnings = false; + inexact_tuple_types_syntax = false; lazy_mode = None; log_saving = SMap.empty; long_lived_workers = false; @@ -957,6 +959,9 @@ module Opts = struct ("gc.worker.space_overhead", gc_worker_space_overhead_parser); ("gc.worker.window_size", gc_worker_window_size_parser); ("include_warnings", boolean (fun opts v -> Ok { opts with include_warnings = v })); + ( "inexact_tuple_types_syntax", + boolean (fun opts v -> Ok { opts with inexact_tuple_types_syntax = v }) + ); ("lazy_mode", lazy_mode_parser); ("log_saving", log_saving_parser); ("max_header_tokens", uint (fun opts v -> Ok { opts with max_header_tokens = v })); @@ -1636,6 +1641,8 @@ let ignore_non_literal_requires c = c.options.Opts.ignore_non_literal_requires let include_warnings c = c.options.Opts.include_warnings +let inexact_tuple_types_syntax c = c.options.Opts.inexact_tuple_types_syntax + let lazy_mode c = c.options.Opts.lazy_mode (* global defaults for lint severities and strict mode *) diff --git a/src/commands/config/flowConfig.mli b/src/commands/config/flowConfig.mli index c8e524cdd21..aa65e5cbe48 100644 --- a/src/commands/config/flowConfig.mli +++ b/src/commands/config/flowConfig.mli @@ -158,6 +158,8 @@ val ignore_non_literal_requires : config -> bool val include_warnings : config -> bool +val inexact_tuple_types_syntax : config -> bool + val lazy_mode : config -> lazy_mode option (* global defaults for lint suppressions and strict mode *) diff --git a/src/common/options.ml b/src/common/options.ml index 417069d33c7..7dd797bca20 100644 --- a/src/common/options.ml +++ b/src/common/options.ml @@ -110,6 +110,7 @@ type t = { opt_ignore_non_literal_requires: bool; opt_include_suppressions: bool; opt_include_warnings: bool; + opt_inexact_tuple_types_syntax: bool; opt_lazy_mode: bool; opt_lint_severities: Severity.severity LintSettings.t; opt_log_file: File_path.t; @@ -265,6 +266,8 @@ let haste_paths_includes opts = opts.opt_haste_paths_includes let include_suppressions opts = opts.opt_include_suppressions +let inexact_tuple_types_syntax opts = opts.opt_inexact_tuple_types_syntax + let is_debug_mode opts = opts.opt_debug let is_quiet opts = opts.opt_quiet diff --git a/src/flow_dot_js.ml b/src/flow_dot_js.ml index fa048f0cac8..c2e01a1d7e2 100644 --- a/src/flow_dot_js.ml +++ b/src/flow_dot_js.ml @@ -132,6 +132,7 @@ let stub_metadata ~root ~checked = enable_as_const = false; enable_const_params = false; enable_enums = true; + enable_inexact_tuple_types_syntax = true; enable_relay_integration = false; exact_by_default = true; facebook_fbs = None; diff --git a/src/services/code_action/__tests__/refactor_extract_utils_tests.ml b/src/services/code_action/__tests__/refactor_extract_utils_tests.ml index 732bf4170f8..510909430a2 100644 --- a/src/services/code_action/__tests__/refactor_extract_utils_tests.ml +++ b/src/services/code_action/__tests__/refactor_extract_utils_tests.ml @@ -45,6 +45,7 @@ let stub_metadata ~root ~checked = enable_as_const = false; enable_const_params = false; enable_enums = true; + enable_inexact_tuple_types_syntax = false; enable_relay_integration = false; exact_by_default = false; facebook_fbs = None; diff --git a/src/typing/__tests__/type_hint_test.ml b/src/typing/__tests__/type_hint_test.ml index 80fbb603d80..9de27cbcb02 100644 --- a/src/typing/__tests__/type_hint_test.ml +++ b/src/typing/__tests__/type_hint_test.ml @@ -37,6 +37,7 @@ let metadata = enable_as_const = false; enable_const_params = false; enable_enums = true; + enable_inexact_tuple_types_syntax = false; enable_relay_integration = false; exact_by_default = true; facebook_fbs = None; diff --git a/src/typing/__tests__/typed_ast_test.ml b/src/typing/__tests__/typed_ast_test.ml index dcea9f8f937..ef65ad6b4e6 100644 --- a/src/typing/__tests__/typed_ast_test.ml +++ b/src/typing/__tests__/typed_ast_test.ml @@ -35,6 +35,7 @@ let metadata = enable_as_const = false; enable_const_params = false; enable_enums = true; + enable_inexact_tuple_types_syntax = false; enable_relay_integration = false; exact_by_default = false; facebook_fbs = None; diff --git a/src/typing/context.ml b/src/typing/context.ml index 18ab7ae1f42..4cd0c212565 100644 --- a/src/typing/context.ml +++ b/src/typing/context.ml @@ -46,6 +46,7 @@ type metadata = { enable_as_const: bool; enable_const_params: bool; enable_enums: bool; + enable_inexact_tuple_types_syntax: bool; enable_relay_integration: bool; exact_by_default: bool; facebook_fbs: string option; @@ -264,6 +265,7 @@ let metadata_of_options options = enable_as_const = Options.as_const options; enable_const_params = Options.enable_const_params options; enable_enums = Options.enums options; + enable_inexact_tuple_types_syntax = Options.inexact_tuple_types_syntax options; enable_relay_integration = Options.enable_relay_integration options; exact_by_default = Options.exact_by_default options; facebook_fbs = Options.facebook_fbs options; @@ -474,6 +476,8 @@ let enable_const_params cx = let enable_enums cx = cx.metadata.enable_enums +let enable_inexact_tuple_types_syntax cx = cx.metadata.enable_inexact_tuple_types_syntax + let enable_relay_integration cx = cx.metadata.enable_relay_integration && Relay_options.enabled_for_file cx.metadata.relay_integration_excludes (file cx) diff --git a/src/typing/context.mli b/src/typing/context.mli index 40cb67aab66..04fe1daabf5 100644 --- a/src/typing/context.mli +++ b/src/typing/context.mli @@ -83,6 +83,7 @@ type metadata = { enable_as_const: bool; enable_const_params: bool; enable_enums: bool; + enable_inexact_tuple_types_syntax: bool; enable_relay_integration: bool; exact_by_default: bool; facebook_fbs: string option; @@ -173,6 +174,8 @@ val enable_const_params : t -> bool val enable_enums : t -> bool +val enable_inexact_tuple_types_syntax : t -> bool + val enable_relay_integration : t -> bool val relay_integration_esmodules : t -> bool diff --git a/src/typing/type_annotation.ml b/src/typing/type_annotation.ml index 64cbf775cef..85445163ad1 100644 --- a/src/typing/type_annotation.ml +++ b/src/typing/type_annotation.ml @@ -483,7 +483,7 @@ module Make (ConsGen : Type_annotation_sig.ConsGen) (Statement : Statement_sig.S in let (unresolved, els_asts) = (List.rev unresolved_rev, List.rev els_asts_rev) in let t = - if inexact then ( + if inexact && not (Context.enable_inexact_tuple_types_syntax env.cx) then ( Flow_js_utils.add_output env.cx (Error_message.EUnsupportedSyntax (loc, Flow_intermediate_error_types.InexactTupleType)); diff --git a/src/typing/type_sig_merge.ml b/src/typing/type_sig_merge.ml index 3757028ad95..b584470dc88 100644 --- a/src/typing/type_sig_merge.ml +++ b/src/typing/type_sig_merge.ml @@ -557,7 +557,7 @@ and merge_annot env file = function let ts = Base.List.map ~f:(merge env file) ts in Type.(IntersectionT (reason, InterRep.make t0 t1 ts)) | Tuple { loc; elems_rev; inexact } -> - if inexact then + if inexact && not (Context.enable_inexact_tuple_types_syntax file.cx) then Type.AnyT.at Type.AnnotatedAny loc else let reason = Reason.(mk_annot_reason RTupleType loc) in diff --git a/tests/inexact_tuples_unsupported/.flowconfig b/tests/inexact_tuples_unsupported/.flowconfig index 6b25c6ec049..0d011c1a142 100644 --- a/tests/inexact_tuples_unsupported/.flowconfig +++ b/tests/inexact_tuples_unsupported/.flowconfig @@ -1,3 +1,4 @@ [options] all=true casting_syntax=as +inexact_tuple_types_syntax=false diff --git a/tests/tuples/.flowconfig b/tests/tuples/.flowconfig index 830bbf45e3e..5a54df7bec7 100644 --- a/tests/tuples/.flowconfig +++ b/tests/tuples/.flowconfig @@ -2,3 +2,4 @@ no_flowlib=false lazy_mode=fs all=true +inexact_tuple_types_syntax=true diff --git a/tests/tuples/inexact.js b/tests/tuples/inexact.js new file mode 100644 index 00000000000..64fd5661f7b --- /dev/null +++ b/tests/tuples/inexact.js @@ -0,0 +1 @@ +type EmptyInexact = [...]; // OK