diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bc9af029f..fa2de7b05f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ #### :bug: Bug fix - Fix node.js ExperimentalWarning. https://github.com/rescript-lang/rescript/pull/7379 +- Fix issue with gentype and stdlib json. https://github.com/rescript-lang/rescript/pull/7378 #### :house: Internal diff --git a/compiler/gentype/TranslateTypeExprFromTypes.ml b/compiler/gentype/TranslateTypeExprFromTypes.ml index 1d84f33975..46ed3fe509 100644 --- a/compiler/gentype/TranslateTypeExprFromTypes.ml +++ b/compiler/gentype/TranslateTypeExprFromTypes.ml @@ -8,14 +8,15 @@ let rec remove_option ~(label : Asttypes.Noloc.arg_label) | Tconstr (Path.Pident id, [t], _), Optional lbl when Ident.name id = "option" -> Some (lbl, t) - | Tconstr (Pdot (Path.Pident name_space, id, _), [t], _), Optional lbl - when Ident.name name_space = "FB" && id = "option" -> - Some (lbl, t) | Tlink t, _ -> t |> remove_option ~label | _ -> None let rec path_to_list path = match path with + | Path.Pident id when String.starts_with (Ident.name id) ~prefix:"Stdlib_" -> + let name = Ident.name id in + let without_stdlib_prefix = String.sub name 7 (String.length name - 7) in + [without_stdlib_prefix; "Stdlib"] | Path.Pident id -> [id |> Ident.name] | Path.Pdot (p, s, _) -> s :: (p |> path_to_list) | Path.Papply _ -> [] @@ -75,11 +76,10 @@ let translate_constr ~config ~params_translation ~(path : Path.t) ~type_env = } in match (path |> path_to_list |> List.rev, params_translation) with - | (["FB"; "bool"] | ["bool"]), [] -> {dependencies = []; type_ = boolean_t} - | (["FB"; "int"] | ["int"]), [] -> {dependencies = []; type_ = number_t} - | (["FB"; "float"] | ["float"]), [] -> {dependencies = []; type_ = number_t} - | ( ( ["FB"; "string"] - | ["string"] + | ["bool"], [] -> {dependencies = []; type_ = boolean_t} + | ["int"], [] -> {dependencies = []; type_ = number_t} + | ["float"], [] -> {dependencies = []; type_ = number_t} + | ( ( ["string"] | ["String"; "t"] | ["Stdlib"; "String"; "t"] | ["Js"; ("String" | "String2"); "t"] ), @@ -118,9 +118,8 @@ let translate_constr ~config ~params_translation ~(path : Path.t) ~type_env = } | (["Js"; "Re"; "t"] | ["RegExp"; "t"] | ["Stdlib"; "RegExp"; "t"]), [] -> {dependencies = []; type_ = regexp_t} - | (["FB"; "unit"] | ["unit"]), [] -> {dependencies = []; type_ = unit_t} - | ( (["FB"; "array"] | ["array"] | ["Js"; ("Array" | "Array2"); "t"]), - [param_translation] ) -> + | ["unit"], [] -> {dependencies = []; type_ = unit_t} + | (["array"] | ["Js"; ("Array" | "Array2"); "t"]), [param_translation] -> {param_translation with type_ = Array (param_translation.type_, Mutable)} | ["ImmutableArray"; "t"], [param_translation] -> {param_translation with type_ = Array (param_translation.type_, Immutable)} @@ -220,7 +219,7 @@ let translate_constr ~config ~params_translation ~(path : Path.t) ~type_env = | ["Jsx"; "element"] ), [] ) -> {dependencies = []; type_ = EmitType.type_react_element} - | (["FB"; "option"] | ["option"]), [param_translation] -> + | ["option"], [param_translation] -> {param_translation with type_ = Option param_translation.type_} | ( ( ["Js"; "Undefined"; "t"] | ["Undefined"; "t"] @@ -253,6 +252,7 @@ let translate_constr ~config ~params_translation ~(path : Path.t) ~type_env = | ( (["Js"; "Dict"; "t"] | ["Dict"; "t"] | ["dict"] | ["Stdlib"; "Dict"; "t"]), [param_translation] ) -> {param_translation with type_ = Dict param_translation.type_} + | ["Stdlib"; "JSON"; "t"], [] -> {dependencies = []; type_ = unknown} | _ -> default_case () type process_variant = { diff --git a/tests/gentype_tests/typescript-react-example/src/Core.gen.tsx b/tests/gentype_tests/typescript-react-example/src/Core.gen.tsx index b4a763cad7..6001f862ea 100644 --- a/tests/gentype_tests/typescript-react-example/src/Core.gen.tsx +++ b/tests/gentype_tests/typescript-react-example/src/Core.gen.tsx @@ -68,3 +68,7 @@ export const weakset1: (x:WeakSet) => WeakSet = CoreJS.weaks export const option0: (x:(undefined | string)) => (undefined | string) = CoreJS.option0 as any; export const option1: (x:(undefined | variant)) => (undefined | variant) = CoreJS.option1 as any; + +export const jsonEncodeString1: unknown = CoreJS.jsonEncodeString1 as any; + +export const jsonEncodeString2: unknown = CoreJS.jsonEncodeString2 as any; diff --git a/tests/gentype_tests/typescript-react-example/src/Core.res b/tests/gentype_tests/typescript-react-example/src/Core.res index 51a9ddcb90..05527ff7d8 100644 --- a/tests/gentype_tests/typescript-react-example/src/Core.res +++ b/tests/gentype_tests/typescript-react-example/src/Core.res @@ -83,3 +83,9 @@ external someFunWithNullUndefinedArg: ( Nullable.t /* Can also be Null.t or option as they are subtypes */, int, ) => string = "someFunWithNullUndefinedArg" + +@genType +let jsonEncodeString1 = JSON.Encode.string("hello") + +@genType +let jsonEncodeString2: JSON.t = JSON.Encode.string("hello") diff --git a/tests/gentype_tests/typescript-react-example/src/Core.res.js b/tests/gentype_tests/typescript-react-example/src/Core.res.js index 45c65ff3a7..2f8e0e1a36 100644 --- a/tests/gentype_tests/typescript-react-example/src/Core.res.js +++ b/tests/gentype_tests/typescript-react-example/src/Core.res.js @@ -98,6 +98,10 @@ let $$Map; let $$Set; +let jsonEncodeString1 = "hello"; + +let jsonEncodeString2 = "hello"; + export { null0, null1, @@ -124,5 +128,7 @@ export { option1, someFunWithNullThenOptionalArgs, someFunWithNullUndefinedArg, + jsonEncodeString1, + jsonEncodeString2, } /* ./Core.gen Not a pure module */