Skip to content

[12.x] Print the operands of in and loop headers with the levels JS needs - #9455

Open
illusionalsagacity wants to merge 1 commit into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-printer-operand-levels-12.x
Open

illusionalsagacity wants to merge 1 commit into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-printer-operand-levels-12.x

Conversation

@illusionalsagacity

Copy link
Copy Markdown
Contributor

Backport of #9428 to 12.x.

Problem

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. The start of a for loop whose end is a number or a variable was printed at level 0 as well, so a sequence in it was a syntax error, or, in a start ending in a variable, a second loop variable.

let property = (d, b) => d->Dict.has(b ? "x" : "y")
let obj = (b, d, e) => (b ? d : e)->Dict.has("a")
let lessThan = (d, e) => d->Dict.has("a") < e->Dict.has("a")
let start = (f, g) =>
  for i in {
    f()
    g()
  } to 5 {
    f()
  }

Before:

function property(d, b) {
  return b ? "x" : "y" in d;
}

function obj(b, d, e) {
  return "a" in b ? d : e;
}

function lessThan(d, e) {
  return "a" in d < "a" in e;
}

function start(f, g) {
  for (let i = f(), g(); i <= 5; ++i) {
    f();
  }
}

After:

function property(d, b) {
  return (
    b ? "x" : "y"
  ) in d;
}

function obj(b, d, e) {
  return "a" in (
    b ? d : e
  );
}

function lessThan(d, e) {
  return "a" in d < ("a" in e);
}

function start(f, g) {
  for (let i = (f(), g()); i <= 5; ++i) {
    f();
  }
}

Fix

As on master:

  • In 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 is printed at level 1.

Differences from master:

  • 12.x has no for...of or for await...of, so the master change to their iterables, and the tests of them, don't apply.

Tests

tests/tests/src/printer_operand_levels_test.res is master's without the for...of and for await...of cases. It covers conditional, sequence and operator operands of in, a dict literal as its object, in as an operand of <, === and ++, and a sequence as the start of a for loop. Its .mjs is the same as on master for those cases. Built with 12.x before this change, the module is a syntax error. No other module in tests/tests changes output.

Investigated by Claude.

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 was
printed at level 0 as well. JavaScript reads a comma in the start as the
next declaration, so a sequence was a syntax error, or, in a start ending
in a variable, a second loop variable. It is now printed at level 1.

12.x has no `for...of` or `for await...of`, so the master change to
their iterables and the tests of them don't apply here.

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
(cherry picked from commit ecb21d0)
@pkg-pr-new

pkg-pr-new Bot commented Nov 27, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9455

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9455

@rescript/darwin-x64

npm i https://pkg.pr.new/@rescript/darwin-x64@9455

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9455

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9455

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9455

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9455

commit: a05603a

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant