Skip to content

Commit a4fa447

Browse files
committed
Fix issue with invalid code when emitting typeof with a function.
Fixes #7641
1 parent 4c5ad57 commit a4fa447

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929

3030
- Add experimental command to `rescript-tools` for extracting all ReScript code blocks from markdown, either a md-file directly, or inside of docstrings in ReScript code. https://github.com/rescript-lang/rescript/pull/7623
3131

32+
#### :bug: Bug fix
33+
34+
- Fix `typeof` parens on functions. https://github.com/rescript-lang/rescript/pull/7643
35+
36+
3237
# 12.0.0-beta.1
3338

3439
#### :rocket: New Feature

compiler/core/js_dump.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,9 +798,14 @@ and expression_desc cxt ~(level : int) f x : cxt =
798798
P.string f " in ";
799799
expression ~level:0 cxt f obj)
800800
| Typeof e ->
801+
let is_fun =
802+
match e.expression_desc with
803+
| Fun _ -> true
804+
| _ -> false
805+
in
801806
P.string f "typeof";
802807
P.space f;
803-
expression ~level:13 cxt f e
808+
P.cond_paren_group f is_fun (fun _ -> expression ~level:13 cxt f e)
804809
| Bin
805810
( Minus,
806811
{

tests/tests/src/core/Test.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@ function run(loc, left, comparator, right) {
4545
console.log(obj.stack.replace(/\n /g, "\n ").replace(/^Error\n/, "").replace(/^.+\n/, "").replace(/\n at .+\(node:internal.+\n?/g, ""));
4646
}
4747

48+
console.log(typeof (prim => require(prim)));
49+
50+
let TypeofParensOnFun = {};
51+
4852
export {
4953
dirname,
5054
print,
5155
run,
56+
TypeofParensOnFun,
5257
}
5358
/* dirname Not a pure module */

tests/tests/src/core/Test.res

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,9 @@ ${codeFrame}
4848
->Console.log
4949
}
5050
}
51+
52+
module TypeofParensOnFun = {
53+
@val external require: string => {..} = "require"
54+
55+
Console.log(Type.typeof(require))
56+
}

0 commit comments

Comments
 (0)