Repository navigation
Print the operands of in and loop headers with the levels JS needs - #9428
Conversation
Why:
`Js_dump` printed both operands of the `in` operator, which `Dict.has`
compiles to, at level 0, so that a conditional, a sequence or a comparison
got no parentheses and JavaScript took only part of it as the operand. It
also printed an `in` without parentheses at levels up to 12, so that an
`in` on the right of a comparison, or an operand of `+`, was split the same
way. `In` now takes its levels from `instanceof`, which is in the same class
of operators.
The start of a `for` loop whose end is a number or a variable, and what
`for...of` and `for await...of` iterate, were printed at level 0 as well.
JavaScript reads a comma in the start as the next declaration and doesn't
allow one in an iterable, so a sequence was a syntax error, or, in a start
ending in a variable, a second loop variable. They are now printed at level
1.
JS modified (operands of `in`):
Example:
let property = (d, b) => d->Dict.has(b ? "x" : "y")
let obj = (b, d, e) => (b ? d : e)->Dict.has("a")
Before (js):
function property(d, b) {
return b ? "x" : "y" in d;
}
function obj(b, d, e) {
return "a" in b ? d : e;
}
After (js):
function property(d, b) {
return (
b ? "x" : "y"
) in d;
}
function obj(b, d, e) {
return "a" in (
b ? d : e
);
}
JS modified (`in` as an operand):
Example:
let lessThan = (d, e) => d->Dict.has("a") < e->Dict.has("a")
Before (js):
function lessThan(d, e) {
return "a" in d < "a" in e;
}
After (js):
function lessThan(d, e) {
return "a" in d < ("a" in e);
}
JS modified (sequence in a loop header):
Example:
let start = (f, g) =>
for i in {
f()
g()
} to 5 {
f()
}
let iterable = (f, items: array<int>) =>
for _ of {
f()
items
} {
f()
}
Before (js): syntax errors
for (let i = f(), g(); i <= 5; ++i) {
…
for (let _for_of of f(), items) {
After (js):
for (let i = (f(), g()); i <= 5; ++i) {
…
for (let _for_of of (f(), items)) {
Review: Do other outputs change? A dict literal tested with `in` is now printed
as `key in ({a: 1})`, as an object literal is as the operand of any other
operator. No other checked-in output changes.
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Changelog🐛 Bug fix
Semantic Diff1 commit · Full page Print the operands of
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #9428 +/- ##
=======================================
Coverage 82.21% 82.22%
=======================================
Files 461 462 +1
Lines 65573 65579 +6
=======================================
+ Hits 53914 53921 +7
+ Misses 11659 11658 -1
🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
cknitt
left a comment
There was a problem hiding this comment.
Looks good to me. I built it, checked the fix against master with extra cases, and found no problems.
This review was done by Claude.
Part of #9398 (item 36 and part of 37). Found while reviewing the fixes for it.
Problem
The printer wrote both operands of
in, whichDict.hascompiles to, without the parentheses JS needs, andinitself without parentheses where it is an operand. The output was valid JS with another meaning.Before:
After:
Before:
After:
A sequence as the start of a
forloop, or as whatfor...ofiterates, was a syntax error:Before:
After:
Fix
Intakes its three levels frominstanceof, which is in the same class of operators. Theforstart and the iterables print at level 1. A dict literal tested withinnow prints askey in ({a: 1}), as it does as an operand of other operators.Not fixed here
A function expression gets no parentheses as an
inoperand ("length" in x => x + 1 | 0), which needs a cast to dict to reach.Tests
printer_operand_levels_test.res. No existing output changes.Merging
Conflicts in
js_dump.mlwithfix-for-loop-in-operatorandfix-for-loop-closure. I'll rebase whichever lands later.12.x
12.x has the
inbug too:d->Dict.has(b ? "x" : "y")compiles tob ? "x" : "y" in dthere. It doesn't havefor...of, and I haven't run theforloop start case there. I'll backport once this one is reviewed.The fix was written by Claude.