Repository navigation
Conversation
Changelog🐛 Bug fix
Semantic Diff3 commits · Full page ✏️ Put typeof and functions in parentheses where their precedence needs themfe36a70 · mununki · Nov 22, 2026, 04:48 UTC · edited
JS modified (typeof as an object) let x: unknown = %raw(`"abc"`)
let len = String.length(typeof(x) :> string)
let up = (typeof(x) :> string)->String.toUpperCase
-let len = typeof x.length;
+let len = (typeof x).length;
-let up = typeof x.toUpperCase();
+let up = (typeof x).toUpperCase();JS modified (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
-let n = x => x + 1 | 0.length;
+let n = (x => x + 1 | 0).length;
-let same = x => x + 1 | 0 === y => y + 1 | 0;
+let same = (x => x + 1 | 0) === (y => y + 1 | 0);
-function (x) {
+(function (x) {
return x + 1 | 0;
-}.call(null, 1);
+}).call(null, 1);Review
Test a function as a branch of ?: in the parentheses test7ef1db2 · mununki · Nov 22, 2026, 04:57 UTC A branch of ✨ Print a function in Some as a branch of ?: without parentheses8150388 · mununki · Nov 28, 2026, 12:55 UTC · new
JS modified (function in Some as a branch of ?:) @val external use: option<int => int> => unit = "use"
let q = (c, o) => use(c ? Some(n => n + 1) : o)
function q(c, o) {
- use(c ? (n => n + 1 | 0) : o);
+ use(c ? n => n + 1 | 0 : o);
}Tests: |
✅ Compiler benchmarksAllocated wordsIn total: 655,718,201 → 655,564,121 (-0.02%). Generated projects: 629,292,681 → 629,285,284 (±0.00%). None of the 265 benchmarks changed by 0.1% or more. InstructionsIn total: 9,224,546,672 → 9,224,341,578 (±0.00%). None of the 33 benchmarks changed by 0.1% or more. Output sizeIn total: 26,662,166 → 26,662,309 (±0.00%). None of the 13 benchmarks changed by 0.1% or more. Pull request 8150388 compared with base 83cf1de in this run. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #9375 +/- ##
=======================================
Coverage 82.34% 82.34%
=======================================
Files 462 462
Lines 65802 65802
=======================================
Hits 54183 54183
Misses 11619 11619
🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-9375 |
f6e1a86 to
a270105
Compare
|
Thanks for the fix and the thorough tests! From a review by Claude (Claude Code): A function inside @val external use: option<int => int> => unit = "use"
let q = (c, o) => use(c ? Some(n => n + 1) : o)Master prints The output is still valid JS. The branch reaches let rec branch_level (e : J.expression) =
match e.expression_desc with
| Fun _ -> 1
| Optional_block (e, true) -> branch_level e
| _ -> 3No checked-in test hits this case, which is why no output changed. A line in Apart from this, the change looks correct to me. |
…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>
A branch of `?:` prints a function at level 1, so it takes no parentheses there. Codecov found that no test had a function in a branch, so that case of `Cond` was not run. Signed-off-by: mununki <woonki.moon@gmail.com>
Why:
A branch of `?:` is printed at level 3, so that a nested conditional
takes parentheses, and at level 1 when it is a function, which needs
none. A function in `Some`, as in `use(c ? Some(n => n + 1) : o)` with
an `option<int => int>` argument, reaches `Cond` as
`Optional_block (Fun _, true)`, which is printed as the function itself.
`branch_level` did not match it, so the previous commits gave it
parentheses that master does not print. It now looks through such an
option, as the review of #9375 suggested.
JS modified (function in Some as a branch of ?:):
Example:
@Val external use: option<int => int> => unit = "use"
let q = (c, o) => use(c ? Some(n => n + 1) : o)
Before (js):
function q(c, o) {
use(c ? (n => n + 1 | 0) : o);
}
After (js):
function q(c, o) {
use(c ? n => n + 1 | 0 : o);
}
Tests: `typeof_and_function_paren_test.res` passes a function in `Some` as a
branch of `?:`. Compiled with the previous commit, its output has the
parentheses. No other checked-in output changes.
Signed-off-by: mununki <woonki.moon@gmail.com>
a270105 to
8150388
Compare
|
Thanks for the review. Done in 8150388: The branch also needed a rebase onto master. There, #9420 made the tag of a tagged template take parentheses itself when it is a function, as |
|
@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. Hooray! 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". |
Fixes: #9353
Js_dumpprintedtypeof ewithout parentheses wherever it stood, and a function without them except as a callee or the operand oftypeof. As the object of a member access or a method call,typeof x.lengthread the length ofx, soString.length((typeof(x) :> string))returned"number"instead of6. An arrow function took what followed it into its body, a syntax error:x => x + 1 | 0.lengthas the object of a member access, andx => x + 1 | 0 === y => y + 1 | 0as an operand of===. Afunctionexpression, as a@thisfunction is printed, read as a declaration as the object of a method call at the start of a statement, also a syntax error.typeofnow takes parentheses above level 13, as!does, and a function above level 2, as 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.?:take a function at level 1, also one inSome, as inuse(c ? Some(n => n + 1) : o), which is printed as the function itself.Condprints them at level 3 only to put a nested conditional in parentheses, soc ? x => x + 1 | 0 : x => x + 2 | 0stays as it was.pp_functionadds the parentheses, since 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, so a function as the tag of a tagged template still takes one pair.Tests:
typeof_and_function_paren_test.rescheckstypeofas the object of a member access and of a method call, an arrow function and an async one as the object of a member access, an arrow function as the object of a method call, a@thisfunction as the object of a member access and of a method call in a statement, an arrow function as an operand of===, and arrow functions as the branches of?:, also inSome, which take no parentheses. Compiled with master, the module fails to load with aSyntaxError, and without the cases that give one, thetypeofchecks return"number"and"string".Checklist
changes/README.md), or no release notes are needed and a maintainer should applychangelog:skip.CONTRIBUTING.md).git commit -s).