Skip to content

Commit fe36a70

Browse files
committed
Put typeof and functions in parentheses where their precedence needs them
Why: `Js_dump` printed `typeof e` without parentheses wherever it stood, and a function without them except as a callee or the operand of `typeof`. As the object of a member access or a method call, `typeof x.length` read the length of `x`, an arrow function took the access into its body, as in `x => x + 1 | 0.length`, a syntax error, and a `function` expression at the start of a statement read as a declaration. An arrow function as an operand of `===` gave a syntax error as well. `typeof` now takes parentheses above level 13, as `!` does, and a function above level 2, as a conditional does. JS modified (typeof as an object): Example: let x: unknown = %raw(`"abc"`) let len = String.length(typeof(x) :> string) let up = (typeof(x) :> string)->String.toUpperCase Before (js): let len = typeof x.length; let up = typeof x.toUpperCase(); After (js): let len = (typeof x).length; let up = (typeof x).toUpperCase(); JS modified (function as an object or an operand): Example: @get external arity: ('a => 'b) => int = "length" @send external callThis: (@this ('self, int) => int, Nullable.t<'s>, int) => int = "call" let n = arity(x => x + 1) let same = (x => x + 1) === (y => y + 1) callThis(@this (_, x) => x + 1, Nullable.null, 1)->ignore Before (js): let n = x => x + 1 | 0.length; let same = x => x + 1 | 0 === y => y + 1 | 0; function (x) { return x + 1 | 0; }.call(null, 1); After (js): let n = (x => x + 1 | 0).length; let same = (x => x + 1 | 0) === (y => y + 1 | 0); (function (x) { return x + 1 | 0; }).call(null, 1); Review: Where does a function take parentheses now? Where a conditional does: as an operand, the test of `?:`, the object of a call or member access, and the operand of `typeof`, `!` or `await`. Arguments, initializers, the right side of `=` and returned values take none. Review: Why not in the branches of `?:`? `Cond` prints them at level 3 only to put a nested conditional in parentheses. A function needs none there and is printed at level 1, so `c ? x => x + 1 | 0 : x => x + 2 | 0` stays as it was. Review: Why does `pp_function` add them? It prints a function that only calls a name as that name, which needs none: `arity(x => u(x))` stays `u.length`. The `Call`, `Tagged_template` and `Typeof` cases no longer put a function in parentheses themselves. Review: Which checked-in outputs change? None. Signed-off-by: mununki <woonki.moon@gmail.com>
1 parent 83cf1de commit fe36a70

4 files changed

Lines changed: 132 additions & 30 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix missing parentheses around `typeof` and functions used as the object of a member access or a call, or as an operand.

‎compiler/backend/js_dump.ml‎

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,13 @@ let default_fn_exp_state = No_name {single_arg = false}
399399

400400
(* TODO: refactoring
401401
Note that {!pp_function} could print both statement and expression when [No_name] is given
402+
403+
[paren] puts a function printed for [No_name] in parentheses, but not the
404+
name it is printed as when it only calls that name.
402405
*)
403-
let rec pp_function ~return_unit ~async ~is_method ?directive cxt (f : P.t)
404-
~fn_state (l : Ident.t list) (b : J.block) (env : Js_fun_env.t) : cxt =
406+
let rec pp_function ~return_unit ~async ~is_method ?directive ?(paren = false)
407+
cxt (f : P.t) ~fn_state (l : Ident.t list) (b : J.block)
408+
(env : Js_fun_env.t) : cxt =
405409
match b with
406410
| [
407411
{
@@ -511,8 +515,9 @@ let rec pp_function ~return_unit ~async ~is_method ?directive cxt (f : P.t)
511515
P.string f (L.function_ ~async ~arrow);
512516
param_body ()
513517
| No_name _ ->
514-
P.string f (L.function_ ~async ~arrow);
515-
param_body ()
518+
P.cond_paren_group f paren (fun _ ->
519+
P.string f (L.function_ ~async ~arrow);
520+
param_body ())
516521
| Name_non_top x ->
517522
ignore (pp_var_assign inner_cxt f x : cxt);
518523
P.string f (L.function_ ~async ~arrow);
@@ -660,7 +665,11 @@ and expression_desc cxt ~(level : int) f x : cxt =
660665
expression ~level:0 cxt f e2)
661666
| Fun {is_method; params; body; env; return_unit; async; directive} ->
662667
(* TODO: dump for comments *)
663-
pp_function ?directive ~is_method ~return_unit ~async
668+
(* An arrow function takes parentheses where a conditional does: as an
669+
operand and as the object of a call or member access. A [function]
670+
expression takes them there too, as it would otherwise start a
671+
declaration at the start of a statement. *)
672+
pp_function ?directive ~is_method ~return_unit ~async ~paren:(level > 2)
664673
~fn_state:default_fn_exp_state cxt f params body env
665674
| Jsx {callee; tag; spread; props; children; key; multi; fragment} ->
666675
if jsx_printable ~tag ~spread ~props ~children ~key ~multi ~fragment then
@@ -689,13 +698,7 @@ and expression_desc cxt ~(level : int) f x : cxt =
689698
| Call (e, el, _info) ->
690699
P.cond_paren_group f (level > 15) (fun _ ->
691700
P.group f 0 (fun _ ->
692-
let cxt =
693-
P.cond_paren_group f
694-
(match e.expression_desc with
695-
| Fun _ -> true
696-
| _ -> false)
697-
(fun () -> expression ~level:15 cxt f e)
698-
in
701+
let cxt = expression ~level:15 cxt f e in
699702
P.paren_group f 0 (fun _ ->
700703
match el with
701704
| [
@@ -726,13 +729,7 @@ and expression_desc cxt ~(level : int) f x : cxt =
726729
in
727730
arguments cxt f el)))
728731
| Tagged_template (call_expr, string_args, value_args) ->
729-
let cxt =
730-
P.cond_paren_group f
731-
(match call_expr.expression_desc with
732-
| Fun _ -> true
733-
| _ -> false)
734-
(fun () -> expression ~level:15 cxt f call_expr)
735-
in
732+
let cxt = expression ~level:15 cxt f call_expr in
736733
P.string f "`";
737734
let rec aux cxt xs ys =
738735
match (xs, ys) with
@@ -849,14 +846,10 @@ and expression_desc cxt ~(level : int) f x : cxt =
849846
P.string f " in ";
850847
expression ~level:rght cxt f obj)
851848
| Typeof e ->
852-
let is_fun =
853-
match e.expression_desc with
854-
| Fun _ -> true
855-
| _ -> false
856-
in
857-
P.string f "typeof";
858-
P.space f;
859-
P.cond_paren_group f is_fun (fun _ -> unary_operand cxt f e)
849+
P.cond_paren_group f (level > 13) (fun _ ->
850+
P.string f "typeof";
851+
P.space f;
852+
unary_operand cxt f e)
860853
| Bin (Minus, {expression_desc = Number (Float {f = "-0."})}, e) ->
861854
(* [-0 - x] is [-x] for every [x], while [0 - x] is [0] and not [-0] when
862855
[x] is [0] *)
@@ -1058,14 +1051,22 @@ and expression_desc cxt ~(level : int) f x : cxt =
10581051
P.space f;
10591052
(*
10601053
[level 1] is correct, however
1061-
to make nice indentation , force nested conditional to be parenthesized
1054+
to make nice indentation , force nested conditional to be parenthesized.
1055+
A function takes no parentheses here either.
10621056
*)
1063-
let cxt = P.group f 1 (fun _ -> expression ~level:3 cxt f e1) in
1057+
let branch_level (e : J.expression) =
1058+
match e.expression_desc with
1059+
| Fun _ -> 1
1060+
| _ -> 3
1061+
in
1062+
let cxt =
1063+
P.group f 1 (fun _ -> expression ~level:(branch_level e1) cxt f e1)
1064+
in
10641065

10651066
P.space f;
10661067
P.string f L.colon_space;
10671068
(* idem *)
1068-
P.group f 1 (fun _ -> expression ~level:3 cxt f e2)
1069+
P.group f 1 (fun _ -> expression ~level:(branch_level e2) cxt f e2)
10691070
in
10701071
if level > 2 then P.paren_vgroup f 1 action else action ()
10711072
| Object entries ->
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Mocha from "mocha";
4+
import * as Test_utils from "./test_utils.mjs";
5+
6+
let x = "abc";
7+
8+
function typeofLength() {
9+
return (typeof x).length;
10+
}
11+
12+
function typeofUpper() {
13+
return (typeof x).toUpperCase();
14+
}
15+
16+
let called = {
17+
contents: 0
18+
};
19+
20+
function callThisStatement() {
21+
(function (n) {
22+
called.contents = n;
23+
return n;
24+
}).call(null, 3);
25+
return called.contents;
26+
}
27+
28+
Mocha.describe("Typeof_and_function_paren_test", () => {
29+
Mocha.test("typeof as the object", () => {
30+
Test_utils.eq("File \"typeof_and_function_paren_test.res\", line 30, characters 7-14", (typeof x).length, 6);
31+
Test_utils.eq("File \"typeof_and_function_paren_test.res\", line 31, characters 7-14", (typeof x).toUpperCase(), "STRING");
32+
});
33+
Mocha.test("arrow function as the object", () => {
34+
Test_utils.eq("File \"typeof_and_function_paren_test.res\", line 35, characters 7-14", (n => n + 1 | 0).length, 1);
35+
Test_utils.eq("File \"typeof_and_function_paren_test.res\", line 36, characters 7-14", (async n => n + 1 | 0).length, 1);
36+
Test_utils.eq("File \"typeof_and_function_paren_test.res\", line 37, characters 7-14", (n => (n << 1)).call(null, 21), 42);
37+
});
38+
Mocha.test("function expression as the object", () => {
39+
Test_utils.eq("File \"typeof_and_function_paren_test.res\", line 41, characters 7-14", (function (n) {
40+
return n + 1 | 0;
41+
}).length, 1);
42+
Test_utils.eq("File \"typeof_and_function_paren_test.res\", line 42, characters 7-14", callThisStatement(), 3);
43+
});
44+
Mocha.test("arrow function as an operand", () => Test_utils.eq("File \"typeof_and_function_paren_test.res\", line 45, characters 48-55", (n => n + 1 | 0) === (n => n + 1 | 0), false));
45+
});
46+
47+
export {
48+
x,
49+
typeofLength,
50+
typeofUpper,
51+
called,
52+
callThisStatement,
53+
}
54+
/* Not a pure module */
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
open Mocha
2+
open Test_utils
3+
4+
// `typeof` and a function take parentheses as the object of a member access
5+
// or a call, and a function as an operand.
6+
7+
@get external arity: ('a => 'b) => int = "length"
8+
@send external call: ('a => 'b, Nullable.t<'s>, 'a) => 'b = "call"
9+
@get external arityThis: (@this ('self, int) => int) => int = "length"
10+
@send external callThis: (@this ('self, int) => int, Nullable.t<'s>, int) => int = "call"
11+
12+
let x: unknown = %raw(`"abc"`)
13+
14+
let typeofLength = () => String.length(typeof(x) :> string)
15+
let typeofUpper = () => (typeof(x) :> string)->String.toUpperCase
16+
17+
let called = ref(0)
18+
19+
// The call is a statement, which a function expression must not start
20+
let callThisStatement = () => {
21+
callThis(@this (_, n) => {
22+
called := n
23+
n
24+
}, Nullable.null, 3)->ignore
25+
called.contents
26+
}
27+
28+
describe(__MODULE__, () => {
29+
test("typeof as the object", () => {
30+
eq(__LOC__, typeofLength(), 6)
31+
eq(__LOC__, typeofUpper(), "STRING")
32+
})
33+
34+
test("arrow function as the object", () => {
35+
eq(__LOC__, arity(n => n + 1), 1)
36+
eq(__LOC__, arity(async n => n + 1), 1)
37+
eq(__LOC__, call(n => n * 2, Nullable.null, 21), 42)
38+
})
39+
40+
test("function expression as the object", () => {
41+
eq(__LOC__, arityThis(@this (_, n) => n + 1), 1)
42+
eq(__LOC__, callThisStatement(), 3)
43+
})
44+
45+
test("arrow function as an operand", () => eq(__LOC__, (n => n + 1) === (n => n + 1), false))
46+
})

0 commit comments

Comments
 (0)