Skip to content

Commit

Permalink
[flow] Deprecate $TupleMap
Browse files Browse the repository at this point in the history
Summary: Changelog: [errors] `$TupleMap` is deprecated and will be soon removed, now that mapped type works on array inputs. Existing `$TupleMap` will still work in this release, but every use will trigger a `deprecated-type` lint that is on by default.

Reviewed By: gkz

Differential Revision: D62883647

fbshipit-source-id: 0002e501efd6ddd22059ce3ddbf6c3e319cffd8e
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Sep 18, 2024
1 parent 7e6a5ac commit 927449d
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/common/lints/lintSettings.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type error = int * string
let default_explicit_values =
LintMap.empty
|> LintMap.add Lints.(DeprecatedType DeprecatedBool) (Severity.Err, None)
|> LintMap.add Lints.(DeprecatedType DeprecatedDollarTupleMap) (Severity.Err, None)
|> LintMap.add Lints.(DeprecatedType DeprecatedPredicate) (Severity.Err, None)
|> LintMap.add Lints.InternalType (Severity.Err, None)
|> LintMap.add Lints.UntypedTypeImport (Severity.Err, None)
Expand Down
11 changes: 10 additions & 1 deletion src/common/lints/lints.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type property_assignment_kind =

type deprecated_type_kind =
| DeprecatedBool
| DeprecatedDollarTupleMap
| DeprecatedPredicate

type lint_kind =
Expand Down Expand Up @@ -75,6 +76,7 @@ let string_of_sketchy_number_kind = function

let string_of_deprecated_type_kind = function
| DeprecatedBool -> "deprecated-type-bool"
| DeprecatedDollarTupleMap -> "deprecated-type-dollar-tuple-map"
| DeprecatedPredicate -> "deprecated-type-predicate"

let string_of_kind = function
Expand Down Expand Up @@ -129,8 +131,15 @@ let kinds_of_string = function
| "untyped-import" -> Some [UntypedImport]
| "internal-type" -> Some [InternalType]
| "unclear-type" -> Some [UnclearType]
| "deprecated-type" -> Some [DeprecatedType DeprecatedBool; DeprecatedType DeprecatedPredicate]
| "deprecated-type" ->
Some
[
DeprecatedType DeprecatedBool;
DeprecatedType DeprecatedDollarTupleMap;
DeprecatedType DeprecatedPredicate;
]
| "deprecated-type-bool" -> Some [DeprecatedType DeprecatedBool]
| "deprecated-type-dollar-tuple-map" -> Some [DeprecatedType DeprecatedDollarTupleMap]
| "deprecated-type-predicate" -> Some [DeprecatedType DeprecatedPredicate]
| "unsafe-getters-setters" -> Some [UnsafeGettersSetters]
| "unnecessary-optional-chain" -> Some [UnnecessaryOptionalChain]
Expand Down
1 change: 1 addition & 0 deletions src/common/lints/lints.mli
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type property_assignment_kind =

type deprecated_type_kind =
| DeprecatedBool
| DeprecatedDollarTupleMap
| DeprecatedPredicate

type lint_kind =
Expand Down
1 change: 1 addition & 0 deletions src/typing/debug_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1626,6 +1626,7 @@ let dump_error_message =
| EInternalType (loc, _) -> spf "EInternalType (%s)" (string_of_aloc loc)
| EUnclearType loc -> spf "EUnclearType (%s)" (string_of_aloc loc)
| EDeprecatedBool loc -> spf "EDeprecatedBool (%s)" (string_of_aloc loc)
| EDeprecatedDollarTupleMap loc -> spf "EDeprecatedDollarTupleMap (%s)" (string_of_aloc loc)
| EDeprecatedPredicate loc -> spf "EDeprecatedPredicate (%s)" (string_of_aloc loc)
| EIncorrectTypeWithReplacement { loc; kind } ->
let deprecated_name = Flow_intermediate_error_types.IncorrectType.incorrect_of_kind kind in
Expand Down
8 changes: 8 additions & 0 deletions src/typing/errors/error_message.ml
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ and 'loc t' =
| ENonstrictImport of 'loc
| EUnclearType of 'loc
| EDeprecatedBool of 'loc
| EDeprecatedDollarTupleMap of 'loc
| EDeprecatedPredicate of 'loc
| EInternalType of 'loc * internal_type
| EIncorrectTypeWithReplacement of {
Expand Down Expand Up @@ -1138,6 +1139,7 @@ let rec map_loc_of_error_message (f : 'a -> 'b) : 'a t' -> 'b t' =
| ENonstrictImport loc -> ENonstrictImport (f loc)
| EUnclearType loc -> EUnclearType (f loc)
| EDeprecatedBool loc -> EDeprecatedBool (f loc)
| EDeprecatedDollarTupleMap loc -> EDeprecatedDollarTupleMap (f loc)
| EDeprecatedPredicate loc -> EDeprecatedPredicate (f loc)
| EInternalType (loc, kind) -> EInternalType (f loc, kind)
| EIncorrectTypeWithReplacement { loc; kind } ->
Expand Down Expand Up @@ -1644,6 +1646,7 @@ let util_use_op_of_msg nope util = function
| ENonstrictImport _
| EUnclearType _
| EDeprecatedBool _
| EDeprecatedDollarTupleMap _
| EDeprecatedPredicate _
| EInternalType _
| EIncorrectTypeWithReplacement _
Expand Down Expand Up @@ -1828,6 +1831,7 @@ let loc_of_msg : 'loc t' -> 'loc option = function
| ENonstrictImport loc
| EUnclearType loc
| EDeprecatedBool loc
| EDeprecatedDollarTupleMap loc
| EDeprecatedPredicate loc
| EInternalType (loc, _)
| EIncorrectTypeWithReplacement { loc; _ }
Expand Down Expand Up @@ -1984,6 +1988,7 @@ let kind_of_msg =
| EInternalType _ -> LintError Lints.InternalType
| EUnclearType _ -> LintError Lints.UnclearType
| EDeprecatedBool _ -> LintError Lints.(DeprecatedType DeprecatedBool)
| EDeprecatedDollarTupleMap _ -> LintError Lints.(DeprecatedType DeprecatedDollarTupleMap)
| EDeprecatedPredicate _ -> LintError Lints.(DeprecatedType DeprecatedPredicate)
| EUnsafeGettersSetters _ -> LintError Lints.UnsafeGettersSetters
| ESketchyNullLint { kind; _ } -> LintError (Lints.SketchyNull kind)
Expand Down Expand Up @@ -2605,6 +2610,7 @@ let friendly_message_of_msg = function
| ENonstrictImport _ -> Normal MessageNonStrictImport
| EUnclearType _ -> Normal MessageUnclearType
| EDeprecatedBool _ -> Normal MessageDeprecatedBool
| EDeprecatedDollarTupleMap _ -> Normal MessageDeprecatedDollarTupleMap
| EDeprecatedPredicate _ -> Normal MessageDeprecatedPredicate
| EInternalType (_, kind) -> Normal (MessageInternalType kind)
| EIncorrectTypeWithReplacement { kind; _ } -> Normal (MessageIncorrectType kind)
Expand Down Expand Up @@ -2901,6 +2907,7 @@ let defered_in_speculation = function
| ENonstrictImport _
| EUnclearType _
| EDeprecatedBool _
| EDeprecatedDollarTupleMap _
| EDeprecatedPredicate _
| EInternalType _
| EUnsafeGettersSetters _
Expand Down Expand Up @@ -3211,6 +3218,7 @@ let error_code_of_message err : error_code option =
| EInternalType _
| EUnclearType _
| EDeprecatedBool _
| EDeprecatedDollarTupleMap _
| EDeprecatedPredicate _
| EUnsafeGettersSetters _
| ESketchyNullLint _
Expand Down
6 changes: 6 additions & 0 deletions src/typing/errors/flow_intermediate_error.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2410,6 +2410,12 @@ let to_printable_error :
@ tl_recur
@ (text ". Please add an annotation to " :: annot_message)
| MessageDeprecatedBool -> [text "Deprecated type. Use "; code "boolean"; text " instead."]
| MessageDeprecatedDollarTupleMap ->
[
text "Deprecated type. Use mapped types instead. ";
text
"See https://flow.org/en/docs/types/mapped-types/ for more information on conditional types.";
]
| MessageDeprecatedPredicate ->
[
text "Deprecated type. Use type guards instead. ";
Expand Down
1 change: 1 addition & 0 deletions src/typing/errors/flow_intermediate_error_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ type 'loc message =
annot_locs: 'loc Env_api.annot_loc list;
}
| MessageDeprecatedBool
| MessageDeprecatedDollarTupleMap
| MessageDeprecatedPredicate
| MessageDevOnlyRefinedLocInfo of { refining_locs: 'loc list }
| MessageDevOnlyInvalidatedRefinementInfo of ('loc * Refinement_invalidation.reason) list
Expand Down
1 change: 1 addition & 0 deletions src/typing/type_annotation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,7 @@ module Make (Statement : Statement_sig.S) : Type_annotation_sig.S = struct
| _ -> error_type cx loc (Error_message.EExportsAnnot loc) t_ast
)
| "$TupleMap" ->
Flow_js_utils.add_output cx (Error_message.EDeprecatedDollarTupleMap loc);
check_type_arg_arity cx loc t_ast targs 2 (fun () ->
let (t1, t2, targs) =
match convert_type_params () with
Expand Down
1 change: 1 addition & 0 deletions tests/init_command/init_command.exp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Default .flowconfig looks like this:
untyped-type-import=error
internal-type=error
deprecated-type-bool=error
deprecated-type-dollar-tuple-map=error
deprecated-type-predicate=error

[options]
Expand Down
1 change: 1 addition & 0 deletions tests/new_generics/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function f<X>(x: Array<X>) {
}

// fast path for TupleMap
// $FlowExpectedError[deprecated-type]
function h1<X: [number]>(x: X): $TupleMap<X, (number) => string> {
return ['a']; // existing unsoundness
}
Expand Down
38 changes: 19 additions & 19 deletions tests/new_generics/new_generics.exp
Original file line number Diff line number Diff line change
Expand Up @@ -1030,37 +1030,37 @@ References:
^ [2]


Error ---------------------------------------------------------------------------------------------------- misc.js:57:10
Error ---------------------------------------------------------------------------------------------------- misc.js:58:10

Cannot return array literal because array literal [1] is incompatible with type including generic type(s) `X` [2].
[incompatible-return]

misc.js:57:10
57| return ['a']; // error, mapped type doesn't have the unsoundness issue
misc.js:58:10
58| return ['a']; // error, mapped type doesn't have the unsoundness issue
^^^^^ [1]

References:
misc.js:56:47
56| function h2<X: [number]>(x: X): {[_K in keyof X]: string} {
misc.js:57:47
57| function h2<X: [number]>(x: X): {[_K in keyof X]: string} {
^ [2]


Error ---------------------------------------------------------------------------------------------------- misc.js:73:28
Error ---------------------------------------------------------------------------------------------------- misc.js:74:28

Cannot call `Object.keys(...).reduce` because: [incompatible-call]
- Either object literal [1] is incompatible with type including generic type(s) `TFormData` [2].
- Or no more than 1 argument is expected by function type [3].

misc.js:73:28
73| return Object.keys(data).reduce( // error: cannot satisfy generic mapped type
misc.js:74:28
74| return Object.keys(data).reduce( // error: cannot satisfy generic mapped type
^^^^^^

References:
misc.js:76:5
76| {},
misc.js:77:5
77| {},
^^ [1]
misc.js:74:32
74| <K: $Keys<TFormData>>(acc: {[K in keyof TFormData]: ?string}, k: K): {[K in keyof TFormData]: ?string} =>
misc.js:75:32
75| <K: $Keys<TFormData>>(acc: {[K in keyof TFormData]: ?string}, k: K): {[K in keyof TFormData]: ?string} =>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [2]
<BUILTINS>/core.js:1055:5
v------
Expand All @@ -1070,20 +1070,20 @@ References:
---^ [3]


Error ---------------------------------------------------------------------------------------------------- misc.js:138:3
Error ---------------------------------------------------------------------------------------------------- misc.js:139:3

Cannot return `otherData` because object type [1] is incompatible with `$ReadOnly<T>` [2]. [incompatible-return]

misc.js:138:3
138| otherData;
misc.js:139:3
139| otherData;
^^^^^^^^^

References:
misc.js:137:54
137| const directAccount = <T: LLETR>(data: T, otherData: LLETR): $ReadOnly<T> =>
misc.js:138:54
138| const directAccount = <T: LLETR>(data: T, otherData: LLETR): $ReadOnly<T> =>
^^^^^ [1]
misc.js:137:72
137| const directAccount = <T: LLETR>(data: T, otherData: LLETR): $ReadOnly<T> =>
misc.js:138:72
138| const directAccount = <T: LLETR>(data: T, otherData: LLETR): $ReadOnly<T> =>
^ [2]


Expand Down
3 changes: 3 additions & 0 deletions tests/tuples/.flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
no_flowlib=false
lazy_mode=fs
all=true

[lints]
deprecated-type-dollar-tuple-map=off

0 comments on commit 927449d

Please sign in to comment.