Skip to content

Commit 0a3f4bb

Browse files
authored
Merge pull request #4923 from rescript-lang/no_implicit_nested_obj
fix #4922
2 parents a4ccf25 + f83822e commit 0a3f4bb

33 files changed

+148
-500
lines changed

jscomp/common/js_config.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ let no_stdlib = ref false
8686

8787
let no_export = ref false
8888

89-
let record_as_js_object = ref false (* otherwise has an attribute *)
89+
9090

9191
let as_ppx = ref false
9292

jscomp/common/js_config.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ val all_module_aliases : bool ref
9696

9797
val no_stdlib: bool ref
9898
val no_export: bool ref
99-
val record_as_js_object : bool ref
99+
100100
val as_ppx : bool ref
101101

102102
val mono_empty_array : bool ref

jscomp/others/node_path.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ external dirname : string -> string = "dirname" [@@bs.module "path"]
3535
external dirname_ext : string -> string -> string = "dirname" [@@bs.module "path"]
3636

3737
type pathObject =
38-
[%bs.obj: <
39-
dir : string ;
40-
root : string ;
41-
base : string ;
42-
name : string ;
43-
ext : string
44-
>
45-
]
38+
<
39+
dir : string ;
40+
root : string ;
41+
base : string ;
42+
name : string ;
43+
ext : string
44+
> Js.t
45+
4646
external format : pathObject -> string = "format" [@@bs.module "path"]
4747

4848
external isAbsolute : string -> bool = "isAbsolute" [@@bs.module "path"]

jscomp/syntax/ast_core_type_class_type.ml

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,11 @@ let default_typ_mapper = Bs_ast_mapper.default_mapper.typ
128128
we can only use it locally
129129
*)
130130

131-
let typ_mapper
132-
record_as_js_object
131+
let typ_mapper
133132
(self : Bs_ast_mapper.mapper)
134133
(ty : Parsetree.core_type)
135134
=
136135
match ty with
137-
| {ptyp_desc = Ptyp_extension({txt = ("bs.obj"|"obj")}, PTyp ty)}
138-
->
139-
Ext_ref.non_exn_protect record_as_js_object true
140-
(fun _ -> self.typ self ty )
141136
| {ptyp_attributes ;
142137
ptyp_desc = Ptyp_arrow (label, args, body);
143138
(* let it go without regard label names,
@@ -205,14 +200,10 @@ let typ_mapper
205200
Ast_compatible.object_field label attrs (self.typ self core_type) in
206201
process_getter_setter ~not_getter_setter ~get ~set
207202
loc label ptyp_attrs core_type acc
208-
)in
209-
let inner_type =
210-
{ ty
211-
with ptyp_desc = Ptyp_object(new_methods, closed_flag);
212-
} in
213-
if !record_as_js_object then
214-
Ast_comb.to_js_type loc inner_type
215-
else inner_type
203+
)in
204+
{ ty
205+
with ptyp_desc = Ptyp_object(new_methods, closed_flag);
206+
}
216207
| _ -> default_typ_mapper self ty
217208

218209
let handle_class_type_fields self fields =

jscomp/syntax/ast_core_type_class_type.mli

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ val handle_class_type_fields :
3030
Parsetree.class_type_field list
3131

3232
val typ_mapper :
33-
bool ref ->
3433
Bs_ast_mapper.mapper ->
3534
Parsetree.core_type ->
3635
Parsetree.core_type

jscomp/syntax/ast_exp_extension.ml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ open Ast_helper
2727

2828

2929

30-
let handle_extension record_as_js_object e (self : Bs_ast_mapper.mapper)
30+
let handle_extension e (self : Bs_ast_mapper.mapper)
3131
(({txt ; loc} , payload) : Parsetree.extension) =
3232
begin match txt with
3333
| "bs.raw" | "raw" ->
@@ -134,11 +134,15 @@ let handle_extension record_as_js_object e (self : Bs_ast_mapper.mapper)
134134
{e with pexp_desc = Ast_exp_handle_external.handle_debugger loc payload}
135135
| "bs.obj" | "obj" ->
136136
begin match payload with
137-
| PStr [{pstr_desc = Pstr_eval (e,_)}]
137+
|PStr [{pstr_desc = Pstr_eval ({pexp_desc = Pexp_record(label_exprs, None)} as e,_)}]
138138
->
139-
Ext_ref.non_exn_protect record_as_js_object true
140-
(fun () -> self.expr self e )
141-
| _ -> Location.raise_errorf ~loc "Expect an expression here"
139+
{e
140+
with
141+
pexp_desc =
142+
Ast_util.record_as_js_object e.pexp_loc self label_exprs
143+
}
144+
145+
| _ -> Location.raise_errorf ~loc "Expect a record expression here"
142146
end
143147
| _ ->
144148
e (* For an unknown extension, we don't really need to process further*)

jscomp/syntax/ast_exp_extension.mli

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525

2626
val handle_extension :
27-
bool ref ->
2827
Parsetree.expression ->
2928
Bs_ast_mapper.mapper ->
3029
Parsetree.extension ->

jscomp/syntax/bs_builtin_ppx.ml

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ let expr_mapper (self : mapper) (e : Parsetree.expression) =
7575
match e.pexp_desc with
7676
(** Its output should not be rewritten anymore *)
7777
| Pexp_extension extension ->
78-
Ast_exp_extension.handle_extension Js_config.record_as_js_object e self extension
78+
Ast_exp_extension.handle_extension e self extension
7979
| Pexp_constant (
8080
Pconst_string
8181
(s, (Some delim)))
@@ -134,26 +134,6 @@ let expr_mapper (self : mapper) (e : Parsetree.expression) =
134134
end
135135
| Pexp_apply (fn, args ) ->
136136
Ast_exp_apply.app_exp_mapper e self fn args
137-
| Pexp_record (label_exprs, opt_exp) ->
138-
(* could be supported using `Object.assign`?
139-
type
140-
{[
141-
external update : 'a Js.t -> 'b Js.t -> 'a Js.t = ""
142-
constraint 'b :> 'a
143-
]}
144-
*)
145-
if !Js_config.record_as_js_object then
146-
(match opt_exp with
147-
| None ->
148-
{ e with
149-
pexp_desc =
150-
Ast_util.record_as_js_object e.pexp_loc self label_exprs;
151-
}
152-
| Some e ->
153-
Location.raise_errorf
154-
~loc:e.pexp_loc "`with` construct is not supported in js obj ")
155-
else
156-
default_expr_mapper self e
157137
| Pexp_object {pcstr_self; pcstr_fields} ->
158138
(match Ast_attributes.process_bs e.pexp_attributes with
159139
| true, pexp_attributes
@@ -204,7 +184,7 @@ let expr_mapper (self : mapper) (e : Parsetree.expression) =
204184

205185

206186
let typ_mapper (self : mapper) (typ : Parsetree.core_type) =
207-
Ast_core_type_class_type.typ_mapper Js_config.record_as_js_object self typ
187+
Ast_core_type_class_type.typ_mapper self typ
208188

209189
let class_type_mapper (self : mapper) ({pcty_attributes; pcty_loc} as ctd : Parsetree.class_type) =
210190
match Ast_attributes.process_bs pcty_attributes with

jscomp/test/arity.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,16 @@ function h(u) {
2727
return m(1, 2);
2828
}
2929

30+
var nested = {
31+
x: {
32+
y: 3
33+
}
34+
};
35+
3036
exports.u = u;
3137
exports.u2 = u2;
3238
exports.f = f;
3339
exports.add = add;
3440
exports.h = h;
41+
exports.nested = nested;
3542
/* No side effect */

jscomp/test/arity.re

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,7 @@ let u = obj => {
5959
let h = u => {
6060
let m = u##hi ;
6161
m(.1,2);
62-
}
62+
}
63+
64+
//
65+
let nested = ({ "x" : {"y" : 3 }} : {. "x" : {. "y" : int }})

0 commit comments

Comments
 (0)