From bd4a233350c610e395f7cc27322b80f149c664c8 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 4 Apr 2025 12:07:24 +0200 Subject: [PATCH 1/5] Fix issue with gentype and stdlib json. Fixes https://github.com/rescript-lang/rescript/issues/7157 --- CHANGELOG.md | 1 + .../gentype/TranslateTypeExprFromTypes.ml | 20 ++++++++----------- .../typescript-react-example/src/Core.gen.tsx | 2 ++ .../typescript-react-example/src/Core.res | 3 +++ .../typescript-react-example/src/Core.res.js | 3 +++ 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bc9af029f..f4608207e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ - Fix `Error.fromException`. https://github.com/rescript-lang/rescript/pull/7364 - Fix signature of `throw`. https://github.com/rescript-lang/rescript/pull/7365 - Fix formatter adds superfluous parens in pipe chain. https://github.com/rescript-lang/rescript/pull/7370 +- Fix issue wiht 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..57d48335be 100644 --- a/compiler/gentype/TranslateTypeExprFromTypes.ml +++ b/compiler/gentype/TranslateTypeExprFromTypes.ml @@ -8,9 +8,6 @@ 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 @@ -75,11 +72,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 +114,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 +215,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 +248,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..5419a27b16 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,5 @@ 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 jsonEncodeString: unknown = CoreJS.jsonEncodeString 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..3b53ecbf87 100644 --- a/tests/gentype_tests/typescript-react-example/src/Core.res +++ b/tests/gentype_tests/typescript-react-example/src/Core.res @@ -83,3 +83,6 @@ external someFunWithNullUndefinedArg: ( Nullable.t /* Can also be Null.t or option as they are subtypes */, int, ) => string = "someFunWithNullUndefinedArg" + +@genType +let jsonEncodeString = 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..d00f3643b3 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,8 @@ let $$Map; let $$Set; +let jsonEncodeString = "hello"; + export { null0, null1, @@ -124,5 +126,6 @@ export { option1, someFunWithNullThenOptionalArgs, someFunWithNullUndefinedArg, + jsonEncodeString, } /* ./Core.gen Not a pure module */ From 6d634813645094ecf94c9a91dc32c1eefbe013c9 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 4 Apr 2025 12:38:58 +0200 Subject: [PATCH 2/5] Allow both `Stdlib.X` and `Stdlib_X`. --- compiler/gentype/TranslateTypeExprFromTypes.ml | 6 +++++- .../typescript-react-example/src/Core.gen.tsx | 4 +++- tests/gentype_tests/typescript-react-example/src/Core.res | 5 ++++- .../gentype_tests/typescript-react-example/src/Core.res.js | 7 +++++-- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/compiler/gentype/TranslateTypeExprFromTypes.ml b/compiler/gentype/TranslateTypeExprFromTypes.ml index 57d48335be..46ed3fe509 100644 --- a/compiler/gentype/TranslateTypeExprFromTypes.ml +++ b/compiler/gentype/TranslateTypeExprFromTypes.ml @@ -13,6 +13,10 @@ let rec remove_option ~(label : Asttypes.Noloc.arg_label) 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 _ -> [] @@ -248,7 +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} + | ["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 5419a27b16..6001f862ea 100644 --- a/tests/gentype_tests/typescript-react-example/src/Core.gen.tsx +++ b/tests/gentype_tests/typescript-react-example/src/Core.gen.tsx @@ -69,4 +69,6 @@ export const option0: (x:(undefined | string)) => (undefined | string) = CoreJS. export const option1: (x:(undefined | variant)) => (undefined | variant) = CoreJS.option1 as any; -export const jsonEncodeString: unknown = CoreJS.jsonEncodeString 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 3b53ecbf87..1174aed73f 100644 --- a/tests/gentype_tests/typescript-react-example/src/Core.res +++ b/tests/gentype_tests/typescript-react-example/src/Core.res @@ -85,4 +85,7 @@ external someFunWithNullUndefinedArg: ( ) => string = "someFunWithNullUndefinedArg" @genType -let jsonEncodeString = JSON.Encode.string("hello") +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 d00f3643b3..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,7 +98,9 @@ let $$Map; let $$Set; -let jsonEncodeString = "hello"; +let jsonEncodeString1 = "hello"; + +let jsonEncodeString2 = "hello"; export { null0, @@ -126,6 +128,7 @@ export { option1, someFunWithNullThenOptionalArgs, someFunWithNullUndefinedArg, - jsonEncodeString, + jsonEncodeString1, + jsonEncodeString2, } /* ./Core.gen Not a pure module */ From af2535d80de334c8d2f0d44e9eefdfb3a8b24314 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 4 Apr 2025 12:48:24 +0200 Subject: [PATCH 3/5] format --- tests/gentype_tests/typescript-react-example/src/Core.res | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/gentype_tests/typescript-react-example/src/Core.res b/tests/gentype_tests/typescript-react-example/src/Core.res index 1174aed73f..05527ff7d8 100644 --- a/tests/gentype_tests/typescript-react-example/src/Core.res +++ b/tests/gentype_tests/typescript-react-example/src/Core.res @@ -88,4 +88,4 @@ external someFunWithNullUndefinedArg: ( let jsonEncodeString1 = JSON.Encode.string("hello") @genType -let jsonEncodeString2 : JSON.t = JSON.Encode.string("hello") +let jsonEncodeString2: JSON.t = JSON.Encode.string("hello") From 5623e557052a0648d1fde6cb899ca8c94e56a79a Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Mon, 7 Apr 2025 08:10:59 +0200 Subject: [PATCH 4/5] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4608207e6..5e7ee2bd47 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 wiht gentype and stdlib json. https://github.com/rescript-lang/rescript/pull/7378 #### :house: Internal @@ -27,7 +28,6 @@ - Fix `Error.fromException`. https://github.com/rescript-lang/rescript/pull/7364 - Fix signature of `throw`. https://github.com/rescript-lang/rescript/pull/7365 - Fix formatter adds superfluous parens in pipe chain. https://github.com/rescript-lang/rescript/pull/7370 -- Fix issue wiht gentype and stdlib json. https://github.com/rescript-lang/rescript/pull/7378 #### :house: Internal From 7e45785bc5484140e7d49259838f04acaa6d13da Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Mon, 7 Apr 2025 08:11:25 +0200 Subject: [PATCH 5/5] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e7ee2bd47..fa2de7b05f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ #### :bug: Bug fix - Fix node.js ExperimentalWarning. https://github.com/rescript-lang/rescript/pull/7379 -- Fix issue wiht gentype and stdlib json. https://github.com/rescript-lang/rescript/pull/7378 +- Fix issue with gentype and stdlib json. https://github.com/rescript-lang/rescript/pull/7378 #### :house: Internal