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.
modifiedJS typeof as an object
let x: unknown = %raw(`"abc"`)let len = String.length(typeof(x) :> string)let up = (typeof(x) :> string)->String.toUpperCase
js
modifiedJS function as an object or an operand
@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
js
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 oftypeof,!orawait. Arguments, initializers, the right side of=and returned values take none. - Why not in the branches of
?:?Condprints them at level 3 only to put a nested conditional in parentheses. A function needs none there and is printed at level 1, soc ? x => x + 1 | 0 : x => x + 2 | 0stays as it was. - Why does
pp_functionadd them? It prints a function that only calls a name as that name, which needs none:arity(x => u(x))staysu.length. TheCall,Tagged_templateandTypeofcases no longer put a function in parentheses themselves. - Which checked-in outputs change? None.