Skip to content

simplify x##property#= 3 into x#property#= 3 #4986

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 2 commits into from
Mar 9, 2021
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
43 changes: 25 additions & 18 deletions jscomp/frontend/ast_exp_apply.ml
Original file line number Diff line number Diff line change
Expand Up @@ -202,25 +202,32 @@ let app_exp_mapper
]}
*)
| Some {op = "#="; loc; args = [obj; arg]} ->
begin match view_as_app obj ["##"] with
| Some { args = [obj; {
pexp_desc =
Pexp_ident {txt = Lident name}
let gen_assignment obj name name_loc =
sane_property_name_check name_loc name;
Exp.constraint_ ~loc
{ e with
pexp_desc =
Ast_uncurry_apply.method_apply loc self obj
(name ^ Literals.setter_suffix) [Nolabel,arg] }
(Ast_literal.type_unit ~loc ()) in
begin match obj.pexp_desc with
| Pexp_send (obj , {txt = name; loc = name_loc})
-> gen_assignment obj name name_loc
| _ ->
match view_as_app obj ["##"] with
| Some { args = [obj; {
pexp_desc =
Pexp_ident {txt = Lident name}
| Pexp_constant (
Pconst_string
(name, None)); pexp_loc
}
]
}
->
sane_property_name_check pexp_loc name;
Exp.constraint_ ~loc
{ e with
pexp_desc =
Ast_uncurry_apply.method_apply loc self obj
(name ^ Literals.setter_suffix) [Nolabel,arg] }
(Ast_literal.type_unit ~loc ())
| _ -> assert false
Pconst_string
(name, None)); pexp_loc = name_loc
}
]
}
->
gen_assignment obj name name_loc
| _ ->
Location.raise_errorf ~loc "invalid #= assignment"
end
| Some { op = "|."; loc; } ->
Location.raise_errorf ~loc
Expand Down
6 changes: 6 additions & 0 deletions jscomp/test/hash_sugar_desugar.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ function h4(u) {
return u.hi(1, 2);
}

function g5(u) {
u.hi = 3;

}

function h5(u) {
u.hi = 3;

Expand All @@ -45,6 +50,7 @@ exports.h1 = h1;
exports.h2 = h2;
exports.h3 = h3;
exports.h4 = h4;
exports.g5 = g5;
exports.h5 = h5;
exports.h6 = h6;
exports.h7 = h7;
Expand Down
2 changes: 2 additions & 0 deletions jscomp/test/hash_sugar_desugar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ let h3 u =
u ["hi"] (1, 2)
*)

let g5 u =
u#hi #= 3
let h5 u =
u ##hi #= 3
(* assignment
Expand Down
2 changes: 1 addition & 1 deletion jscomp/test/hash_sugar_desugar.mli
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ val h1 : < p : 'a; .. > -> 'a
val h2 : < m : (int -> int -> 'a [@bs]); .. > -> 'a
val h3 : < hi : int -> int -> 'a; .. > -> 'a
val h4 : < hi : (int -> int -> 'a [@bs.meth]); .. > -> 'a

val g5 : < hi : int [@bs.set]; .. > -> unit
val h5 : < hi : int [@bs.set]; .. > -> unit
(* The inferred type is
val h5 : < hi#= : (int -> unit [@bs.meth]); .. > -> unit
Expand Down
43 changes: 25 additions & 18 deletions lib/4.06.1/unstable/js_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -404623,25 +404623,32 @@ let app_exp_mapper
]}
*)
| Some {op = "#="; loc; args = [obj; arg]} ->
begin match view_as_app obj ["##"] with
| Some { args = [obj; {
pexp_desc =
Pexp_ident {txt = Lident name}
let gen_assignment obj name name_loc =
sane_property_name_check name_loc name;
Exp.constraint_ ~loc
{ e with
pexp_desc =
Ast_uncurry_apply.method_apply loc self obj
(name ^ Literals.setter_suffix) [Nolabel,arg] }
(Ast_literal.type_unit ~loc ()) in
begin match obj.pexp_desc with
| Pexp_send (obj , {txt = name; loc = name_loc})
-> gen_assignment obj name name_loc
| _ ->
match view_as_app obj ["##"] with
| Some { args = [obj; {
pexp_desc =
Pexp_ident {txt = Lident name}
| Pexp_constant (
Pconst_string
(name, None)); pexp_loc
}
]
}
->
sane_property_name_check pexp_loc name;
Exp.constraint_ ~loc
{ e with
pexp_desc =
Ast_uncurry_apply.method_apply loc self obj
(name ^ Literals.setter_suffix) [Nolabel,arg] }
(Ast_literal.type_unit ~loc ())
| _ -> assert false
Pconst_string
(name, None)); pexp_loc = name_loc
}
]
}
->
gen_assignment obj name name_loc
| _ ->
Location.raise_errorf ~loc "invalid #= assignment"
end
| Some { op = "|."; loc; } ->
Location.raise_errorf ~loc
Expand Down
43 changes: 25 additions & 18 deletions lib/4.06.1/whole_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -407665,25 +407665,32 @@ let app_exp_mapper
]}
*)
| Some {op = "#="; loc; args = [obj; arg]} ->
begin match view_as_app obj ["##"] with
| Some { args = [obj; {
pexp_desc =
Pexp_ident {txt = Lident name}
let gen_assignment obj name name_loc =
sane_property_name_check name_loc name;
Exp.constraint_ ~loc
{ e with
pexp_desc =
Ast_uncurry_apply.method_apply loc self obj
(name ^ Literals.setter_suffix) [Nolabel,arg] }
(Ast_literal.type_unit ~loc ()) in
begin match obj.pexp_desc with
| Pexp_send (obj , {txt = name; loc = name_loc})
-> gen_assignment obj name name_loc
| _ ->
match view_as_app obj ["##"] with
| Some { args = [obj; {
pexp_desc =
Pexp_ident {txt = Lident name}
| Pexp_constant (
Pconst_string
(name, None)); pexp_loc
}
]
}
->
sane_property_name_check pexp_loc name;
Exp.constraint_ ~loc
{ e with
pexp_desc =
Ast_uncurry_apply.method_apply loc self obj
(name ^ Literals.setter_suffix) [Nolabel,arg] }
(Ast_literal.type_unit ~loc ())
| _ -> assert false
Pconst_string
(name, None)); pexp_loc = name_loc
}
]
}
->
gen_assignment obj name name_loc
| _ ->
Location.raise_errorf ~loc "invalid #= assignment"
end
| Some { op = "|."; loc; } ->
Location.raise_errorf ~loc
Expand Down