Skip to content

Fix issue with gentype and stdlib json. #7378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 12 additions & 12 deletions compiler/gentype/TranslateTypeExprFromTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 _ -> []
Expand Down Expand Up @@ -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"] ),
Expand Down Expand Up @@ -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)}
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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 = {
Expand Down
4 changes: 4 additions & 0 deletions tests/gentype_tests/typescript-react-example/src/Core.gen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ export const weakset1: (x:WeakSet<number[]>) => WeakSet<number[]> = 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;
6 changes: 6 additions & 0 deletions tests/gentype_tests/typescript-react-example/src/Core.res
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,9 @@ external someFunWithNullUndefinedArg: (
Nullable.t<string> /* 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")
6 changes: 6 additions & 0 deletions tests/gentype_tests/typescript-react-example/src/Core.res.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.