Skip to content

Commit b87edbb

Browse files
committed
converting cases f["123"] into f[123]
we use a round trip to avoid number-like literals ("0123")
1 parent 1142c37 commit b87edbb

File tree

5 files changed

+71
-8
lines changed

5 files changed

+71
-8
lines changed

jscomp/core/js_dump_property.ml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,18 @@ let property_access f s =
8383
end
8484
else
8585
begin
86-
P.bracket_group f 1 @@ fun _ ->
87-
Js_dump_string.pp_string f s
86+
P.bracket_group f 1 (fun _ ->
87+
(* avoid cases like
88+
"0123", "123_456"
89+
*)
90+
match string_of_int (int_of_string s ) with
91+
| s0 when s0 = s ->
92+
P.string f s
93+
| _ ->
94+
Js_dump_string.pp_string f s
95+
| exception _ ->
96+
Js_dump_string.pp_string f s
97+
)
8898
end
8999

90100
let property_key f (s : J.property_name) =

jscomp/test/build.ninja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ build test/gpr_1817_test.cmi test/gpr_1817_test.cmj : cc test/gpr_1817_test.ml |
265265
build test/gpr_1822_test.cmi test/gpr_1822_test.cmj : cc test/gpr_1822_test.ml | test/mt.cmj $stdlib
266266
build test/gpr_1891_test.cmi test/gpr_1891_test.cmj : cc test/gpr_1891_test.ml | $stdlib
267267
build test/gpr_1943_test.cmi test/gpr_1943_test.cmj : cc test/gpr_1943_test.ml | test/mt.cmj $stdlib
268-
build test/gpr_1946_test.cmi test/gpr_1946_test.cmj : cc test/gpr_1946_test.ml | $stdlib
268+
build test/gpr_1946_test.cmi test/gpr_1946_test.cmj : cc test/gpr_1946_test.ml | test/mt.cmj $stdlib
269269
build test/gpr_2250_test.cmi test/gpr_2250_test.cmj : cc test/gpr_2250_test.ml | test/mt.cmj $stdlib
270270
build test/gpr_2316_test.cmi test/gpr_2316_test.cmj : cc test/gpr_2316_test.ml | test/mt.cmj $stdlib
271271
build test/gpr_2352_test.cmi test/gpr_2352_test.cmj : cc test/gpr_2352_test.ml | $stdlib

jscomp/test/gpr_1943_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function eq(loc, x, y) {
3131
function f(x) {
3232
return [
3333
x["003"],
34-
x["50"],
34+
x[50],
3535
x["50x"],
3636
x.__50,
3737
x.__50x,

jscomp/test/gpr_1946_test.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
'use strict';
22

3+
var Mt = require("./mt.js");
4+
5+
var suites = {
6+
contents: /* [] */0
7+
};
8+
9+
var test_id = {
10+
contents: 0
11+
};
12+
13+
function eq(loc, x, y) {
14+
return Mt.eq_suites(test_id, suites, loc, x, y);
15+
}
316

417
var x = ({
518
x: 3,
@@ -13,16 +26,43 @@ var x = ({
1326

1427
var zz = ({
1528
"5": 3
16-
})["5"];
29+
})[5];
1730

18-
({
19-
"5": 3
20-
})["5"];
31+
var h = {
32+
"0123": 2,
33+
"123_456": 3
34+
};
35+
36+
function f(id) {
37+
while(false) {
38+
39+
};
40+
return id;
41+
}
42+
43+
eq("File \"gpr_1946_test.ml\", line 29, characters 6-13", ({
44+
"5": 3
45+
})[5], 3);
46+
47+
eq("File \"gpr_1946_test.ml\", line 30, characters 6-13", [
48+
2,
49+
3
50+
], [
51+
f(h)["0123"],
52+
f(h)["123_456"]
53+
]);
2154

2255
console.log(({
2356
"5": 3
2457
}).TAG | 0);
2558

59+
Mt.from_pair_suites("File \"gpr_1946_test.ml\", line 33, characters 23-30", suites.contents);
60+
61+
exports.suites = suites;
62+
exports.test_id = test_id;
63+
exports.eq = eq;
2664
exports.x = x;
2765
exports.zz = zz;
66+
exports.h = h;
67+
exports.f = f;
2868
/* x Not a pure module */

jscomp/test/gpr_1946_test.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,20 @@ let zz = [%obj{_5 = 3 }]##_5
1414

1515

1616

17+
18+
type t = {
19+
a : int ; [@bs.as "0123"]
20+
b : int ; [@bs.as "123_456"]
21+
}
22+
23+
let h = {a = 2; b = 3 }
24+
let f id =
25+
while false do () done;
26+
id
27+
28+
1729
;; eq __LOC__ [%obj{_5 = 3 }]##_5 3
30+
;; eq __LOC__ (2,3) ((f h).a,(f h).b)
1831
;; Js.log @@ Obj.tag (Obj.repr [%obj{_5 = 3 }])
1932

2033
;; Mt.from_pair_suites __LOC__ !suites

0 commit comments

Comments
 (0)