Skip to content

Commit 2d2a5da

Browse files
anmonteirochenglou
authored andcommitted
Syntax sugar ["literal_string"] for SHARPOPs ## and #= (#2051)
* Syntax sugar `["literal_string"]` for SHARPOPs `##` and `#=` * rebase on master after #2050, parse `#=` with higher precedence, parens unnecessary on the right side * array, string and bigarray get also get `#=` -> `=` * rebase on fast pipe fix * small refactor * Fix printer after rebase (caused by earlier refactor) * fix tests after rebase * Fixes after rebase
1 parent d82b960 commit 2d2a5da

File tree

9 files changed

+199
-87
lines changed

9 files changed

+199
-87
lines changed

formatTest/unit_tests/expected_output/basicStructures.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,4 +832,4 @@ it("should remove parens", a => {
832832
});
833833

834834
/* https://github.com/facebook/reason/issues/1554 */
835-
(curNode^)##childNodes;
835+
curNode^["childNodes"];

formatTest/unit_tests/expected_output/bucklescript.re

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,59 @@ bla #= Some(10);
44

55
bla #= someFunc(Some(10));
66

7-
test##var #= Some(-10);
7+
test["var"] = Some(-10);
88

99
obj##.prop;
1010

1111
obj##.prod := exp;
1212

13-
preview##style##border
14-
#= Js.string("1px black dashed");
13+
preview["style"]["border"] =
14+
Js.string("1px black dashed");
1515

16-
(preview##(style##border) #= args)(somenum);
16+
(preview##(style["border"]) #= args)(somenum);
1717

18-
x##y##z #= xxxx##yyyy##zzzz;
18+
x["y"]["z"] = xxxx["yyyy"]["zzzz"];
1919

2020
let result =
2121
js_method_run1((!react)#createElement, foo);
2222

23-
add(zz##yy, xx##ww);
23+
add(zz["yy"], xx["ww"]);
2424

2525
/* These should print the same */
26-
let res = x##y + z##q; /* AST */
27-
let res = x##y + z##q; /* Minimum parens */
26+
let res = x["y"] + z["q"]; /* AST */
27+
let res = x["y"] + z["q"]; /* Minimum parens */
2828

2929
/* These should print the same */
30-
let res = y + z##q##a; /* AST */
31-
let res = y + z##q##a; /* Min parens */
30+
let res = y + z["q"]["a"]; /* AST */
31+
let res = y + z["q"]["a"]; /* Min parens */
3232

3333
/* Make sure it's actually parsed as left precedence
3434
* and that is maintained when printed */
35-
let res = z##(q##a); /* AST */
36-
let res = z##(q##a); /* Min parens */
35+
let res = z##(q["a"]); /* AST */
36+
let res = z##(q["a"]); /* Min parens */
3737

3838
/* These should print the same */
39-
let res = !x##y; /* AST */
40-
let res = !x##y; /* Minimum parens */
39+
let res = !x["y"]; /* AST */
40+
let res = !x["y"]; /* Minimum parens */
4141

4242
/* These should print the same */
43-
let res = !z##q##a; /* AST */
44-
let res = !z##q##a; /* Min parens */
43+
let res = !z["q"]["a"]; /* AST */
44+
let res = !z["q"]["a"]; /* Min parens */
4545

4646
/* These should print the same */
47-
let res = ?!!x##y; /* AST */
48-
let res = ?!!x##y; /* Minimum parens */
47+
let res = ?!!x["y"]; /* AST */
48+
let res = ?!!x["y"]; /* Minimum parens */
4949

5050
/* These should print the same */
51-
let res = ?!!z##(q##a); /* AST */
52-
let res = ?!!z##(q##a); /* Min parens */
51+
let res = ?!!z##(q["a"]); /* AST */
52+
let res = ?!!z##(q["a"]); /* Min parens */
5353

54-
res #= ?!!z##q;
55-
res #= ?!!z##(q##a);
54+
res #= ?!!z["q"];
55+
res #= ?!!z##(q["a"]);
5656

57-
let result = myFunction(x(y)##z, a(b) #= c);
57+
let result = myFunction(x(y)["z"], a(b) #= c);
5858

59-
(!x)##y##(b##c);
59+
(!x)["y"]##(b["c"]);
6060

6161
type a = {. "foo": bar};
6262

@@ -75,7 +75,7 @@ let b = {
7575
let c = {
7676
"a": a,
7777
"b": b,
78-
"func": a => a##c #= func(10),
78+
"func": a => a["c"] = func(10),
7979
};
8080

8181
let d = {

formatTest/unit_tests/expected_output/fastPipe.re

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,19 @@ foo !== bar->baz;
5959
x |> y >>= foo->bar->baz;
6060
let m = f => foo->bar->f;
6161

62-
obj##x->foo->bar;
62+
obj["x"]->foo->bar;
6363

6464
event->target[0];
6565

6666
event->target[0];
6767

68-
event->target##value;
68+
event->target["value"];
6969

70-
event->target##value;
70+
event->target["value"];
7171

72-
event->target##value[0];
72+
event->target["value"][0];
7373

74-
event->(target##value[0]);
74+
event->(target["value"][0]);
7575

7676
event->target(foo);
7777

@@ -85,18 +85,22 @@ foo->bar := baz;
8585

8686
foo->bar === baz;
8787

88-
event->target##value(foo);
88+
event->target["value"](foo);
8989

9090
event->target##(value(foo));
9191

9292
(foo^)->bar;
9393

94-
location##streets.foo[1];
94+
location["streets"].foo[1];
9595

96-
(event->target^)##value;
96+
event->target^["value"];
9797

9898
event->target^ #= value;
9999

100+
event["target"].{0};
101+
102+
event->target.{0};
103+
100104
foo->f(. a, b);
101105
foo->f(. a, b)->g(. c, d);
102106
foo->([@attr] f(. a, b))->([@attr2] f(. a, b));
@@ -110,9 +114,9 @@ foo->([@attr] f(.))->([@attr] g(.));
110114
!foo->bar;
111115
(!foo)->bar;
112116

113-
a->(b##c);
117+
a->(b["c"]);
114118

115-
a->b##c;
119+
a->b["c"];
116120

117121
(
118122
switch (saveStatus) {
@@ -188,7 +192,7 @@ Foo.Bar.reactClass->setNavigationOptions(
188192
),
189193
);
190194

191-
foo##bar
195+
foo["bar"]
192196
->setNavigationOptions(
193197
NavigationOptions.t(
194198
~title="Title",

formatTest/unit_tests/expected_output/jsx.re

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ let icon =
121121

122122
<MessengerSharedPhotosAlbumViewPhotoReact
123123
ref=?{
124-
foo##bar === baz ?
124+
foo["bar"] === baz ?
125125
Some(
126126
foooooooooooooooooooooooo(setRefChild),
127127
) :
128128
None
129129
}
130-
key=node##legacy_attachment_id
130+
key=node["legacy_attachment_id"]
131131
/>;
132132

133133
/* punning */
@@ -248,21 +248,21 @@ let (/></) = (a, b) => a + b;
248248
let x = foo /></ bar;
249249

250250
/* https://github.com/facebook/reason/issues/870 */
251-
<div onClick=this##handleClick>
251+
<div onClick=this["handleClick"]>
252252
<> foo </>
253253
</div>;
254254

255-
<div onClick=this##handleClick>
255+
<div onClick=this["handleClick"]>
256256
<> {foo(bar)} </>
257257
</div>;
258258

259259
/* function application */
260-
<div onClick=this##handleClick>
260+
<div onClick=this["handleClick"]>
261261
<> {foo(bar)} </>
262262
</div>;
263263

264264
/* tuple, not function application */
265-
<div onClick=this##handleClick>
265+
<div onClick=this["handleClick"]>
266266
<> foo bar </>
267267
</div>;
268268

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
foo #= bar[0];
22

3-
foo##bar[0] = 3;
3+
foo["bar"][0] = 3;
44

5-
foo##bar[0]##baz[1] = 3;
5+
foo["bar"][0]["baz"][1] = 3;
66

7-
foo##bar[0]##baz[1];
7+
foo["bar"][0]["baz"][1];
88

9-
foo##bar #= bar[0];
9+
foo["bar"] = bar[0];
1010

11-
foo##bar##baz #= bar##baz[0];
11+
foo["bar"]["baz"] = bar["baz"][0];
1212

1313
foo[bar + 1];
1414

@@ -21,3 +21,23 @@ foo.[bar + 1] = 1;
2121
foo.{bar + 1} = 1;
2222

2323
foo[bar + 1] = 1;
24+
25+
bla #= Constr(x);
26+
27+
bla #= M.(someFunc(Some(10)));
28+
29+
bla["foo"] = 3;
30+
31+
bla["foo"][0] = 3;
32+
33+
bla["foo"].qux["x"] = 3;
34+
35+
bla["foo"].qux[0]["bar"] = 3;
36+
37+
bla[0]["foo"].qux[0]["bar"] = 3;
38+
39+
bla[0]["foo"].qux[0].[0]["bar"] = 3;
40+
41+
bla[0]["foo"].qux[0].{0}["bar"] = 3;
42+
43+
bla[0]["foo"].qux[0].{0, 1, 2}["bar"] = 3;

formatTest/unit_tests/input/fastPipe.re

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ event->target##(value(foo));
9797

9898
event->target^ #= value;
9999

100+
event##target.{0};
101+
102+
event->target.{0};
103+
100104
foo->f(. a, b);
101105
foo->f(. a, b)->g(. c, d);
102106
foo->([@attr] f(. a, b))->([@attr2] f(. a, b));

formatTest/unit_tests/input/sharpop.re

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,23 @@ foo.[bar + 1] = 1;
2121
foo.{bar + 1} = 1;
2222

2323
foo[bar + 1] = 1;
24+
25+
bla #= (Constr(x));
26+
27+
bla #= M.(someFunc(Some(10)));
28+
29+
bla["foo"] = 3;
30+
31+
bla["foo"][0] = 3;
32+
33+
bla["foo"].qux["x"] = 3;
34+
35+
bla["foo"].qux[0]["bar"] = 3;
36+
37+
bla[0]["foo"].qux[0]["bar"] = 3;
38+
39+
bla[0]["foo"].qux[0].[0]["bar"] = 3;
40+
41+
bla[0]["foo"].qux[0].{0}["bar"] = 3;
42+
43+
bla[0]["foo"].qux[0].{0,1,2}["bar"] = 3;

src/reason-parser/reason_parser.mly

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2881,10 +2881,20 @@ mark_position_exp
28812881
| simple_expr DOT as_loc(label_longident) EQUAL expr
28822882
{ mkexp(Pexp_setfield($1, $3, $5)) }
28832883
| simple_expr LBRACKET expr RBRACKET EQUAL expr
2884-
{ let loc = mklocation $symbolstartpos $endpos in
2885-
let exp = Pexp_ident(array_function ~loc "Array" "set") in
2886-
mkexp(Pexp_apply(mkexp ~ghost:true ~loc exp,
2887-
[Nolabel,$1; Nolabel,$3; Nolabel,$6]))
2884+
{ let {pexp_attributes; pexp_desc } = $3 in
2885+
match pexp_desc with
2886+
| Pexp_constant(Pconst_string (label,_)) ->
2887+
let loc = mklocation $startpos($3) $endpos($3) in
2888+
let label_exp = mkexp ~attrs:pexp_attributes (Pexp_ident (mkloc (Lident label) loc)) ~loc in
2889+
mkinfixop
2890+
(mkinfixop $1 (mkoperator (ghloc "##")) label_exp)
2891+
(mkoperator (ghloc "#="))
2892+
$6
2893+
| _ ->
2894+
let loc = mklocation $symbolstartpos $endpos in
2895+
let exp = Pexp_ident(array_function ~loc "Array" "set") in
2896+
mkexp(Pexp_apply(mkexp ~ghost:true ~loc exp,
2897+
[Nolabel,$1; Nolabel,$3; Nolabel,$6]))
28882898
}
28892899
| simple_expr DOT LBRACKET expr RBRACKET EQUAL expr
28902900
{ let loc = mklocation $symbolstartpos $endpos in
@@ -3022,9 +3032,16 @@ parenthesized_expr:
30223032
mkexp(Pexp_object(Cstr.mk pat []))))
30233033
}
30243034
| E LBRACKET expr RBRACKET
3025-
{ let loc = mklocation $symbolstartpos $endpos in
3026-
let exp = Pexp_ident(array_function ~loc "Array" "get") in
3027-
mkexp(Pexp_apply(mkexp ~ghost:true ~loc exp, [Nolabel,$1; Nolabel,$3]))
3035+
{ let {pexp_attributes; pexp_desc } = $3 in
3036+
match pexp_desc with
3037+
| Pexp_constant(Pconst_string (label,_)) ->
3038+
let loc = mklocation $startpos($3) $endpos($3) in
3039+
let label_exp = mkexp ~attrs:pexp_attributes (Pexp_ident (mkloc (Lident label) loc)) ~loc in
3040+
mkinfixop $1 (mkoperator (ghloc "##")) label_exp
3041+
| _ ->
3042+
let loc = mklocation $symbolstartpos $endpos in
3043+
let exp = Pexp_ident(array_function ~loc "Array" "get") in
3044+
mkexp(Pexp_apply(mkexp ~ghost:true ~loc exp, [Nolabel,$1; Nolabel,$3]))
30283045
}
30293046
| E as_loc(LBRACKET) expr as_loc(error)
30303047
{ unclosed_exp (with_txt $2 "(") (with_txt $4 ")") }

0 commit comments

Comments
 (0)