Skip to content

Commit 5cd7de5

Browse files
authored
Fix issue with gentype and stdlib json. (#7378)
* Fix issue with gentype and stdlib json. Fixes #7157 * Allow both `Stdlib.X` and `Stdlib_X`. * format * Update CHANGELOG.md * Update CHANGELOG.md
1 parent 7861628 commit 5cd7de5

File tree

5 files changed

+29
-12
lines changed

5 files changed

+29
-12
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#### :bug: Bug fix
1616

1717
- Fix node.js ExperimentalWarning. https://github.com/rescript-lang/rescript/pull/7379
18+
- Fix issue with gentype and stdlib json. https://github.com/rescript-lang/rescript/pull/7378
1819

1920
#### :house: Internal
2021

Diff for: compiler/gentype/TranslateTypeExprFromTypes.ml

+12-12
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ let rec remove_option ~(label : Asttypes.Noloc.arg_label)
88
| Tconstr (Path.Pident id, [t], _), Optional lbl when Ident.name id = "option"
99
->
1010
Some (lbl, t)
11-
| Tconstr (Pdot (Path.Pident name_space, id, _), [t], _), Optional lbl
12-
when Ident.name name_space = "FB" && id = "option" ->
13-
Some (lbl, t)
1411
| Tlink t, _ -> t |> remove_option ~label
1512
| _ -> None
1613

1714
let rec path_to_list path =
1815
match path with
16+
| Path.Pident id when String.starts_with (Ident.name id) ~prefix:"Stdlib_" ->
17+
let name = Ident.name id in
18+
let without_stdlib_prefix = String.sub name 7 (String.length name - 7) in
19+
[without_stdlib_prefix; "Stdlib"]
1920
| Path.Pident id -> [id |> Ident.name]
2021
| Path.Pdot (p, s, _) -> s :: (p |> path_to_list)
2122
| Path.Papply _ -> []
@@ -75,11 +76,10 @@ let translate_constr ~config ~params_translation ~(path : Path.t) ~type_env =
7576
}
7677
in
7778
match (path |> path_to_list |> List.rev, params_translation) with
78-
| (["FB"; "bool"] | ["bool"]), [] -> {dependencies = []; type_ = boolean_t}
79-
| (["FB"; "int"] | ["int"]), [] -> {dependencies = []; type_ = number_t}
80-
| (["FB"; "float"] | ["float"]), [] -> {dependencies = []; type_ = number_t}
81-
| ( ( ["FB"; "string"]
82-
| ["string"]
79+
| ["bool"], [] -> {dependencies = []; type_ = boolean_t}
80+
| ["int"], [] -> {dependencies = []; type_ = number_t}
81+
| ["float"], [] -> {dependencies = []; type_ = number_t}
82+
| ( ( ["string"]
8383
| ["String"; "t"]
8484
| ["Stdlib"; "String"; "t"]
8585
| ["Js"; ("String" | "String2"); "t"] ),
@@ -118,9 +118,8 @@ let translate_constr ~config ~params_translation ~(path : Path.t) ~type_env =
118118
}
119119
| (["Js"; "Re"; "t"] | ["RegExp"; "t"] | ["Stdlib"; "RegExp"; "t"]), [] ->
120120
{dependencies = []; type_ = regexp_t}
121-
| (["FB"; "unit"] | ["unit"]), [] -> {dependencies = []; type_ = unit_t}
122-
| ( (["FB"; "array"] | ["array"] | ["Js"; ("Array" | "Array2"); "t"]),
123-
[param_translation] ) ->
121+
| ["unit"], [] -> {dependencies = []; type_ = unit_t}
122+
| (["array"] | ["Js"; ("Array" | "Array2"); "t"]), [param_translation] ->
124123
{param_translation with type_ = Array (param_translation.type_, Mutable)}
125124
| ["ImmutableArray"; "t"], [param_translation] ->
126125
{param_translation with type_ = Array (param_translation.type_, Immutable)}
@@ -220,7 +219,7 @@ let translate_constr ~config ~params_translation ~(path : Path.t) ~type_env =
220219
| ["Jsx"; "element"] ),
221220
[] ) ->
222221
{dependencies = []; type_ = EmitType.type_react_element}
223-
| (["FB"; "option"] | ["option"]), [param_translation] ->
222+
| ["option"], [param_translation] ->
224223
{param_translation with type_ = Option param_translation.type_}
225224
| ( ( ["Js"; "Undefined"; "t"]
226225
| ["Undefined"; "t"]
@@ -253,6 +252,7 @@ let translate_constr ~config ~params_translation ~(path : Path.t) ~type_env =
253252
| ( (["Js"; "Dict"; "t"] | ["Dict"; "t"] | ["dict"] | ["Stdlib"; "Dict"; "t"]),
254253
[param_translation] ) ->
255254
{param_translation with type_ = Dict param_translation.type_}
255+
| ["Stdlib"; "JSON"; "t"], [] -> {dependencies = []; type_ = unknown}
256256
| _ -> default_case ()
257257

258258
type process_variant = {

Diff for: tests/gentype_tests/typescript-react-example/src/Core.gen.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ export const weakset1: (x:WeakSet<number[]>) => WeakSet<number[]> = CoreJS.weaks
6868
export const option0: (x:(undefined | string)) => (undefined | string) = CoreJS.option0 as any;
6969

7070
export const option1: (x:(undefined | variant)) => (undefined | variant) = CoreJS.option1 as any;
71+
72+
export const jsonEncodeString1: unknown = CoreJS.jsonEncodeString1 as any;
73+
74+
export const jsonEncodeString2: unknown = CoreJS.jsonEncodeString2 as any;

Diff for: tests/gentype_tests/typescript-react-example/src/Core.res

+6
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,9 @@ external someFunWithNullUndefinedArg: (
8383
Nullable.t<string> /* Can also be Null.t or option as they are subtypes */,
8484
int,
8585
) => string = "someFunWithNullUndefinedArg"
86+
87+
@genType
88+
let jsonEncodeString1 = JSON.Encode.string("hello")
89+
90+
@genType
91+
let jsonEncodeString2: JSON.t = JSON.Encode.string("hello")

Diff for: tests/gentype_tests/typescript-react-example/src/Core.res.js

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)