Skip to content

Commit 848afb2

Browse files
authored
Merge pull request #4986 from rescript-lang/simplify_method_call
simplify x##property#= 3 into x#property#= 3
2 parents c4468b4 + 944f3e0 commit 848afb2

File tree

6 files changed

+84
-55
lines changed

6 files changed

+84
-55
lines changed

jscomp/frontend/ast_exp_apply.ml

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -202,25 +202,32 @@ let app_exp_mapper
202202
]}
203203
*)
204204
| Some {op = "#="; loc; args = [obj; arg]} ->
205-
begin match view_as_app obj ["##"] with
206-
| Some { args = [obj; {
207-
pexp_desc =
208-
Pexp_ident {txt = Lident name}
205+
let gen_assignment obj name name_loc =
206+
sane_property_name_check name_loc name;
207+
Exp.constraint_ ~loc
208+
{ e with
209+
pexp_desc =
210+
Ast_uncurry_apply.method_apply loc self obj
211+
(name ^ Literals.setter_suffix) [Nolabel,arg] }
212+
(Ast_literal.type_unit ~loc ()) in
213+
begin match obj.pexp_desc with
214+
| Pexp_send (obj , {txt = name; loc = name_loc})
215+
-> gen_assignment obj name name_loc
216+
| _ ->
217+
match view_as_app obj ["##"] with
218+
| Some { args = [obj; {
219+
pexp_desc =
220+
Pexp_ident {txt = Lident name}
209221
| Pexp_constant (
210-
Pconst_string
211-
(name, None)); pexp_loc
212-
}
213-
]
214-
}
215-
->
216-
sane_property_name_check pexp_loc name;
217-
Exp.constraint_ ~loc
218-
{ e with
219-
pexp_desc =
220-
Ast_uncurry_apply.method_apply loc self obj
221-
(name ^ Literals.setter_suffix) [Nolabel,arg] }
222-
(Ast_literal.type_unit ~loc ())
223-
| _ -> assert false
222+
Pconst_string
223+
(name, None)); pexp_loc = name_loc
224+
}
225+
]
226+
}
227+
->
228+
gen_assignment obj name name_loc
229+
| _ ->
230+
Location.raise_errorf ~loc "invalid #= assignment"
224231
end
225232
| Some { op = "|."; loc; } ->
226233
Location.raise_errorf ~loc

jscomp/test/hash_sugar_desugar.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ function h4(u) {
1919
return u.hi(1, 2);
2020
}
2121

22+
function g5(u) {
23+
u.hi = 3;
24+
25+
}
26+
2227
function h5(u) {
2328
u.hi = 3;
2429

@@ -45,6 +50,7 @@ exports.h1 = h1;
4550
exports.h2 = h2;
4651
exports.h3 = h3;
4752
exports.h4 = h4;
53+
exports.g5 = g5;
4854
exports.h5 = h5;
4955
exports.h6 = h6;
5056
exports.h7 = h7;

jscomp/test/hash_sugar_desugar.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ let h3 u =
2929
u ["hi"] (1, 2)
3030
*)
3131

32+
let g5 u =
33+
u#hi #= 3
3234
let h5 u =
3335
u ##hi #= 3
3436
(* assignment

jscomp/test/hash_sugar_desugar.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ val h1 : < p : 'a; .. > -> 'a
44
val h2 : < m : (int -> int -> 'a [@bs]); .. > -> 'a
55
val h3 : < hi : int -> int -> 'a; .. > -> 'a
66
val h4 : < hi : (int -> int -> 'a [@bs.meth]); .. > -> 'a
7-
7+
val g5 : < hi : int [@bs.set]; .. > -> unit
88
val h5 : < hi : int [@bs.set]; .. > -> unit
99
(* The inferred type is
1010
val h5 : < hi#= : (int -> unit [@bs.meth]); .. > -> unit

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -404623,25 +404623,32 @@ let app_exp_mapper
404623404623
]}
404624404624
*)
404625404625
| Some {op = "#="; loc; args = [obj; arg]} ->
404626-
begin match view_as_app obj ["##"] with
404627-
| Some { args = [obj; {
404628-
pexp_desc =
404629-
Pexp_ident {txt = Lident name}
404626+
let gen_assignment obj name name_loc =
404627+
sane_property_name_check name_loc name;
404628+
Exp.constraint_ ~loc
404629+
{ e with
404630+
pexp_desc =
404631+
Ast_uncurry_apply.method_apply loc self obj
404632+
(name ^ Literals.setter_suffix) [Nolabel,arg] }
404633+
(Ast_literal.type_unit ~loc ()) in
404634+
begin match obj.pexp_desc with
404635+
| Pexp_send (obj , {txt = name; loc = name_loc})
404636+
-> gen_assignment obj name name_loc
404637+
| _ ->
404638+
match view_as_app obj ["##"] with
404639+
| Some { args = [obj; {
404640+
pexp_desc =
404641+
Pexp_ident {txt = Lident name}
404630404642
| Pexp_constant (
404631-
Pconst_string
404632-
(name, None)); pexp_loc
404633-
}
404634-
]
404635-
}
404636-
->
404637-
sane_property_name_check pexp_loc name;
404638-
Exp.constraint_ ~loc
404639-
{ e with
404640-
pexp_desc =
404641-
Ast_uncurry_apply.method_apply loc self obj
404642-
(name ^ Literals.setter_suffix) [Nolabel,arg] }
404643-
(Ast_literal.type_unit ~loc ())
404644-
| _ -> assert false
404643+
Pconst_string
404644+
(name, None)); pexp_loc = name_loc
404645+
}
404646+
]
404647+
}
404648+
->
404649+
gen_assignment obj name name_loc
404650+
| _ ->
404651+
Location.raise_errorf ~loc "invalid #= assignment"
404645404652
end
404646404653
| Some { op = "|."; loc; } ->
404647404654
Location.raise_errorf ~loc

lib/4.06.1/whole_compiler.ml

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -407665,25 +407665,32 @@ let app_exp_mapper
407665407665
]}
407666407666
*)
407667407667
| Some {op = "#="; loc; args = [obj; arg]} ->
407668-
begin match view_as_app obj ["##"] with
407669-
| Some { args = [obj; {
407670-
pexp_desc =
407671-
Pexp_ident {txt = Lident name}
407668+
let gen_assignment obj name name_loc =
407669+
sane_property_name_check name_loc name;
407670+
Exp.constraint_ ~loc
407671+
{ e with
407672+
pexp_desc =
407673+
Ast_uncurry_apply.method_apply loc self obj
407674+
(name ^ Literals.setter_suffix) [Nolabel,arg] }
407675+
(Ast_literal.type_unit ~loc ()) in
407676+
begin match obj.pexp_desc with
407677+
| Pexp_send (obj , {txt = name; loc = name_loc})
407678+
-> gen_assignment obj name name_loc
407679+
| _ ->
407680+
match view_as_app obj ["##"] with
407681+
| Some { args = [obj; {
407682+
pexp_desc =
407683+
Pexp_ident {txt = Lident name}
407672407684
| Pexp_constant (
407673-
Pconst_string
407674-
(name, None)); pexp_loc
407675-
}
407676-
]
407677-
}
407678-
->
407679-
sane_property_name_check pexp_loc name;
407680-
Exp.constraint_ ~loc
407681-
{ e with
407682-
pexp_desc =
407683-
Ast_uncurry_apply.method_apply loc self obj
407684-
(name ^ Literals.setter_suffix) [Nolabel,arg] }
407685-
(Ast_literal.type_unit ~loc ())
407686-
| _ -> assert false
407685+
Pconst_string
407686+
(name, None)); pexp_loc = name_loc
407687+
}
407688+
]
407689+
}
407690+
->
407691+
gen_assignment obj name name_loc
407692+
| _ ->
407693+
Location.raise_errorf ~loc "invalid #= assignment"
407687407694
end
407688407695
| Some { op = "|."; loc; } ->
407689407696
Location.raise_errorf ~loc

0 commit comments

Comments
 (0)