Skip to content

Commit 0fc8efa

Browse files
committed
Allow f##”hello”; f##”hello”#=value. — special name in property access/set , fix #1000, #999
1 parent f14eec8 commit 0fc8efa

9 files changed

+156
-23
lines changed

jscomp/syntax/ast_exp_apply.ml

+20-3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ type app_pattern = {
5656
args : Parsetree.expression list
5757
}
5858

59+
let sane_property_name_check loc s =
60+
if String.contains s '#'then
61+
Location.raise_errorf ~loc
62+
"property name (%s) can not contain speical character #" s
5963
(* match fn as *)
6064
let view_as_app (fn : exp) s : app_pattern option =
6165
match fn.pexp_desc with
@@ -90,7 +94,7 @@ let app_exp_mapper
9094
| Some {op; loc} ->
9195
Location.raise_errorf ~loc "%s expect f%sproperty arg0 arg2 form" op op
9296
| None ->
93-
match view_as_app e infix_ops with
97+
(match view_as_app e infix_ops with
9498
| Some { op = "|."; args = [obj_arg; fn];loc} ->
9599
(*
96100
a |. f
@@ -161,9 +165,14 @@ let app_exp_mapper
161165
Bs_ast_invariant.warn_discarded_unused_attributes attrs ;
162166
{e with pexp_desc = Ast_util.method_apply loc self obj name (check_and_discard args)}
163167
|
164-
{pexp_desc = Pexp_ident {txt = Lident name;_ } ; _}
168+
{pexp_desc =
169+
(Pexp_ident {txt = Lident name;_ }
170+
| Pexp_constant (Const_string(name,None)))
171+
;
172+
pexp_loc}
165173
(* f##paint *)
166174
->
175+
sane_property_name_check pexp_loc name ;
167176
{ e with pexp_desc =
168177
Ast_util.js_property loc (self.expr self obj) name
169178
}
@@ -185,8 +194,15 @@ let app_exp_mapper
185194
*)
186195
| Some {op = "#="; loc; args = [obj; arg]} ->
187196
begin match view_as_app obj ["##"] with
188-
| Some { args = [obj; {pexp_desc = Pexp_ident {txt = Lident name}}]}
197+
| Some { args = [obj; {
198+
pexp_desc =
199+
Pexp_ident {txt = Lident name}
200+
| Pexp_constant (Const_string (name, None)); pexp_loc
201+
}
202+
]
203+
}
189204
->
205+
sane_property_name_check pexp_loc name;
190206
Exp.constraint_ ~loc
191207
{ e with
192208
pexp_desc =
@@ -213,3 +229,4 @@ let app_exp_mapper
213229
{e with pexp_desc = Ast_util.uncurry_fn_apply e.pexp_loc self fn (check_and_discard args) ;
214230
pexp_attributes }
215231
else {e with pexp_attributes } (* BS_NATIVE branch*)
232+
)

jscomp/test/build.ninja

+1
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ build test/map_test.cmi : cc test/map_test.mli | $stdlib
395395
build test/mario_game.cmi test/mario_game.cmj : cc test/mario_game.ml | $stdlib
396396
build test/method_chain.cmi test/method_chain.cmj : cc test/method_chain.ml | $stdlib
397397
build test/method_name_test.cmi test/method_name_test.cmj : cc test/method_name_test.ml | test/mt.cmj $stdlib
398+
build test/method_string_name.cmi test/method_string_name.cmj : cc test/method_string_name.re | $stdlib
398399
build test/minimal_test.cmi test/minimal_test.cmj : cc test/minimal_test.ml | $stdlib
399400
build test/miss_colon_test.cmi test/miss_colon_test.cmj : cc test/miss_colon_test.ml | $stdlib
400401
build test/mock_mt.cmi test/mock_mt.cmj : cc test/mock_mt.ml | test/mt.cmj $stdlib

jscomp/test/method_string_name.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
4+
var f = {
5+
"Content-Type": 3
6+
};
7+
8+
console.log(f["Content-Type"]);
9+
10+
function ff(x) {
11+
x.Hi;
12+
x["Content-Type"] = "hello";
13+
console.log(({
14+
"Content-Type": "hello"
15+
})["Content-Type"]);
16+
return /* () */0;
17+
}
18+
19+
exports.f = f;
20+
exports.ff = ff;
21+
/* Not a pure module */

jscomp/test/method_string_name.re

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
3+
let f = {"Content-Type" : 3 };
4+
5+
6+
7+
Js.log (f ## "Content-Type");
8+
9+
let ff =(x) => {
10+
x## "Hi";
11+
// x## "hi#";
12+
x##"Content-Type"#= "hello";
13+
Js.log({"Content-Type" : "hello"}##"Content-Type");
14+
}

lib/4.02.3/bsdep.ml

+20-4
Original file line numberDiff line numberDiff line change
@@ -38978,6 +38978,10 @@ type app_pattern = {
3897838978
args : Parsetree.expression list
3897938979
}
3898038980

38981+
let sane_property_name_check loc s =
38982+
if String.contains s '#'then
38983+
Location.raise_errorf ~loc
38984+
"property name (%s) can not contain speical character #" s
3898138985
(* match fn as *)
3898238986
let view_as_app (fn : exp) s : app_pattern option =
3898338987
match fn.pexp_desc with
@@ -39012,7 +39016,7 @@ let app_exp_mapper
3901239016
| Some {op; loc} ->
3901339017
Location.raise_errorf ~loc "%s expect f%sproperty arg0 arg2 form" op op
3901439018
| None ->
39015-
match view_as_app e infix_ops with
39019+
(match view_as_app e infix_ops with
3901639020
| Some { op = "|."; args = [obj_arg; fn];loc} ->
3901739021
(*
3901839022
a |. f
@@ -39083,9 +39087,14 @@ let app_exp_mapper
3908339087
Bs_ast_invariant.warn_discarded_unused_attributes attrs ;
3908439088
{e with pexp_desc = Ast_util.method_apply loc self obj name (check_and_discard args)}
3908539089
|
39086-
{pexp_desc = Pexp_ident {txt = Lident name;_ } ; _}
39090+
{pexp_desc =
39091+
(Pexp_ident {txt = Lident name;_ }
39092+
| Pexp_constant (Const_string(name,None)))
39093+
;
39094+
pexp_loc}
3908739095
(* f##paint *)
3908839096
->
39097+
sane_property_name_check pexp_loc name ;
3908939098
{ e with pexp_desc =
3909039099
Ast_util.js_property loc (self.expr self obj) name
3909139100
}
@@ -39107,8 +39116,15 @@ let app_exp_mapper
3910739116
*)
3910839117
| Some {op = "#="; loc; args = [obj; arg]} ->
3910939118
begin match view_as_app obj ["##"] with
39110-
| Some { args = [obj; {pexp_desc = Pexp_ident {txt = Lident name}}]}
39119+
| Some { args = [obj; {
39120+
pexp_desc =
39121+
Pexp_ident {txt = Lident name}
39122+
| Pexp_constant (Const_string (name, None)); pexp_loc
39123+
}
39124+
]
39125+
}
3911139126
->
39127+
sane_property_name_check pexp_loc name;
3911239128
Exp.constraint_ ~loc
3911339129
{ e with
3911439130
pexp_desc =
@@ -39135,7 +39151,7 @@ let app_exp_mapper
3913539151
{e with pexp_desc = Ast_util.uncurry_fn_apply e.pexp_loc self fn (check_and_discard args) ;
3913639152
pexp_attributes }
3913739153
else {e with pexp_attributes } (* BS_NATIVE branch*)
39138-
39154+
)
3913939155
end
3914039156
module Ast_exp_extension : sig
3914139157
#1 "ast_exp_extension.mli"

lib/4.02.3/bsppx.ml

+20-4
Original file line numberDiff line numberDiff line change
@@ -21084,6 +21084,10 @@ type app_pattern = {
2108421084
args : Parsetree.expression list
2108521085
}
2108621086

21087+
let sane_property_name_check loc s =
21088+
if String.contains s '#'then
21089+
Location.raise_errorf ~loc
21090+
"property name (%s) can not contain speical character #" s
2108721091
(* match fn as *)
2108821092
let view_as_app (fn : exp) s : app_pattern option =
2108921093
match fn.pexp_desc with
@@ -21118,7 +21122,7 @@ let app_exp_mapper
2111821122
| Some {op; loc} ->
2111921123
Location.raise_errorf ~loc "%s expect f%sproperty arg0 arg2 form" op op
2112021124
| None ->
21121-
match view_as_app e infix_ops with
21125+
(match view_as_app e infix_ops with
2112221126
| Some { op = "|."; args = [obj_arg; fn];loc} ->
2112321127
(*
2112421128
a |. f
@@ -21189,9 +21193,14 @@ let app_exp_mapper
2118921193
Bs_ast_invariant.warn_discarded_unused_attributes attrs ;
2119021194
{e with pexp_desc = Ast_util.method_apply loc self obj name (check_and_discard args)}
2119121195
|
21192-
{pexp_desc = Pexp_ident {txt = Lident name;_ } ; _}
21196+
{pexp_desc =
21197+
(Pexp_ident {txt = Lident name;_ }
21198+
| Pexp_constant (Const_string(name,None)))
21199+
;
21200+
pexp_loc}
2119321201
(* f##paint *)
2119421202
->
21203+
sane_property_name_check pexp_loc name ;
2119521204
{ e with pexp_desc =
2119621205
Ast_util.js_property loc (self.expr self obj) name
2119721206
}
@@ -21213,8 +21222,15 @@ let app_exp_mapper
2121321222
*)
2121421223
| Some {op = "#="; loc; args = [obj; arg]} ->
2121521224
begin match view_as_app obj ["##"] with
21216-
| Some { args = [obj; {pexp_desc = Pexp_ident {txt = Lident name}}]}
21225+
| Some { args = [obj; {
21226+
pexp_desc =
21227+
Pexp_ident {txt = Lident name}
21228+
| Pexp_constant (Const_string (name, None)); pexp_loc
21229+
}
21230+
]
21231+
}
2121721232
->
21233+
sane_property_name_check pexp_loc name;
2121821234
Exp.constraint_ ~loc
2121921235
{ e with
2122021236
pexp_desc =
@@ -21241,7 +21257,7 @@ let app_exp_mapper
2124121257
{e with pexp_desc = Ast_util.uncurry_fn_apply e.pexp_loc self fn (check_and_discard args) ;
2124221258
pexp_attributes }
2124321259
else {e with pexp_attributes } (* BS_NATIVE branch*)
21244-
21260+
)
2124521261
end
2124621262
module Ast_exp_extension : sig
2124721263
#1 "ast_exp_extension.mli"

lib/4.02.3/unstable/js_compiler.ml

+20-4
Original file line numberDiff line numberDiff line change
@@ -20965,6 +20965,10 @@ type app_pattern = {
2096520965
args : Parsetree.expression list
2096620966
}
2096720967

20968+
let sane_property_name_check loc s =
20969+
if String.contains s '#'then
20970+
Location.raise_errorf ~loc
20971+
"property name (%s) can not contain speical character #" s
2096820972
(* match fn as *)
2096920973
let view_as_app (fn : exp) s : app_pattern option =
2097020974
match fn.pexp_desc with
@@ -20999,7 +21003,7 @@ let app_exp_mapper
2099921003
| Some {op; loc} ->
2100021004
Location.raise_errorf ~loc "%s expect f%sproperty arg0 arg2 form" op op
2100121005
| None ->
21002-
match view_as_app e infix_ops with
21006+
(match view_as_app e infix_ops with
2100321007
| Some { op = "|."; args = [obj_arg; fn];loc} ->
2100421008
(*
2100521009
a |. f
@@ -21070,9 +21074,14 @@ let app_exp_mapper
2107021074
Bs_ast_invariant.warn_discarded_unused_attributes attrs ;
2107121075
{e with pexp_desc = Ast_util.method_apply loc self obj name (check_and_discard args)}
2107221076
|
21073-
{pexp_desc = Pexp_ident {txt = Lident name;_ } ; _}
21077+
{pexp_desc =
21078+
(Pexp_ident {txt = Lident name;_ }
21079+
| Pexp_constant (Const_string(name,None)))
21080+
;
21081+
pexp_loc}
2107421082
(* f##paint *)
2107521083
->
21084+
sane_property_name_check pexp_loc name ;
2107621085
{ e with pexp_desc =
2107721086
Ast_util.js_property loc (self.expr self obj) name
2107821087
}
@@ -21094,8 +21103,15 @@ let app_exp_mapper
2109421103
*)
2109521104
| Some {op = "#="; loc; args = [obj; arg]} ->
2109621105
begin match view_as_app obj ["##"] with
21097-
| Some { args = [obj; {pexp_desc = Pexp_ident {txt = Lident name}}]}
21106+
| Some { args = [obj; {
21107+
pexp_desc =
21108+
Pexp_ident {txt = Lident name}
21109+
| Pexp_constant (Const_string (name, None)); pexp_loc
21110+
}
21111+
]
21112+
}
2109821113
->
21114+
sane_property_name_check pexp_loc name;
2109921115
Exp.constraint_ ~loc
2110021116
{ e with
2110121117
pexp_desc =
@@ -21122,7 +21138,7 @@ let app_exp_mapper
2112221138
{e with pexp_desc = Ast_util.uncurry_fn_apply e.pexp_loc self fn (check_and_discard args) ;
2112321139
pexp_attributes }
2112421140
else {e with pexp_attributes } (* BS_NATIVE branch*)
21125-
21141+
)
2112621142
end
2112721143
module Ast_exp_extension : sig
2112821144
#1 "ast_exp_extension.mli"

lib/4.02.3/unstable/native_ppx.ml

+20-4
Original file line numberDiff line numberDiff line change
@@ -19296,6 +19296,10 @@ type app_pattern = {
1929619296
args : Parsetree.expression list
1929719297
}
1929819298

19299+
let sane_property_name_check loc s =
19300+
if String.contains s '#'then
19301+
Location.raise_errorf ~loc
19302+
"property name (%s) can not contain speical character #" s
1929919303
(* match fn as *)
1930019304
let view_as_app (fn : exp) s : app_pattern option =
1930119305
match fn.pexp_desc with
@@ -19330,7 +19334,7 @@ let app_exp_mapper
1933019334
| Some {op; loc} ->
1933119335
Location.raise_errorf ~loc "%s expect f%sproperty arg0 arg2 form" op op
1933219336
| None ->
19333-
match view_as_app e infix_ops with
19337+
(match view_as_app e infix_ops with
1933419338
| Some { op = "|."; args = [obj_arg; fn];loc} ->
1933519339
(*
1933619340
a |. f
@@ -19401,9 +19405,14 @@ let app_exp_mapper
1940119405
Bs_ast_invariant.warn_discarded_unused_attributes attrs ;
1940219406
{e with pexp_desc = Ast_util.method_apply loc self obj name (check_and_discard args)}
1940319407
|
19404-
{pexp_desc = Pexp_ident {txt = Lident name;_ } ; _}
19408+
{pexp_desc =
19409+
(Pexp_ident {txt = Lident name;_ }
19410+
| Pexp_constant (Const_string(name,None)))
19411+
;
19412+
pexp_loc}
1940519413
(* f##paint *)
1940619414
->
19415+
sane_property_name_check pexp_loc name ;
1940719416
{ e with pexp_desc =
1940819417
Ast_util.js_property loc (self.expr self obj) name
1940919418
}
@@ -19425,8 +19434,15 @@ let app_exp_mapper
1942519434
*)
1942619435
| Some {op = "#="; loc; args = [obj; arg]} ->
1942719436
begin match view_as_app obj ["##"] with
19428-
| Some { args = [obj; {pexp_desc = Pexp_ident {txt = Lident name}}]}
19437+
| Some { args = [obj; {
19438+
pexp_desc =
19439+
Pexp_ident {txt = Lident name}
19440+
| Pexp_constant (Const_string (name, None)); pexp_loc
19441+
}
19442+
]
19443+
}
1942919444
->
19445+
sane_property_name_check pexp_loc name;
1943019446
Exp.constraint_ ~loc
1943119447
{ e with
1943219448
pexp_desc =
@@ -19453,7 +19469,7 @@ let app_exp_mapper
1945319469
{e with pexp_desc = Ast_util.uncurry_fn_apply e.pexp_loc self fn (check_and_discard args) ;
1945419470
pexp_attributes }
1945519471
else {e with pexp_attributes } (* BS_NATIVE branch*)
19456-
19472+
)
1945719473
end
1945819474
module Ast_signature : sig
1945919475
#1 "ast_signature.mli"

0 commit comments

Comments
 (0)