Parenthesise the in operator in a for loop's header
Why
JavaScript doesn't allow a bare in operator in the initializer of a for header, where the printer puts the start of a ReScript for loop and an end that is neither a number nor a variable. A start or end using Dict.has was therefore printed as a syntax error. While Js_dump prints the initializer, Ext_pp now records that in is forbidden until the next brackets, and In is printed in parentheses there.
modifiedJS for loop whose end uses in
let f = (d, g) => { for i in 0 to d->Dict.has("a") ? 5 : 4 { g(i) }}
js
Before a syntax error
After
for (let i = 0, i_finish = "a" in d ? 5 : 4; i <= i_finish; ++i) {for (let i = 0, i_finish = ("a" in d) ? 5 : 4; i <= i_finish; ++i) { g(i); g(i);}}
modifiedJS for loop whose start uses in
let f = (d, g) => { for i in d->Dict.has("a") ? 1 : 2 to 3 { g(i) }}
js
Before a syntax error
After
for (let i = "a" in d ? 1 : 2; i <= 3; ++i) {for (let i = ("a" in d) ? 1 : 2; i <= 3; ++i) { g(i); g(i);}}
Review
- Does an
inalready inside brackets, such as a call's argument, get parentheses? No: every bracket the printer opens throughExt_ppallowsinagain. Only brackets it writes as plain strings, such as a template's${…}, don't, so aninthere gets redundant but valid parentheses. - Which checked-in outputs change? None, besides the new test's.