Skip to content

Commit cfd0a34

Browse files
shulhiShulhi Sapli
and
Shulhi Sapli
authored
Fix formatting of external with as attribute and _ placeholder (rescript-lang#6970)
* Handle different arity when arrow has as attribute during printing * Small cleanup * Add tests * Update CHANGELOG --------- Co-authored-by: Shulhi Sapli <shulhi@mba-13.local>
1 parent ea8a5f2 commit cfd0a34

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- Fix issue where long layout break added a trailing comma in partial application `...`. https://github.com/rescript-lang/rescript-compiler/pull/6949
3030
- Fix incorrect format of function under unary operator. https://github.com/rescript-lang/rescript-compiler/pull/6953
3131
- Fix incorrect incorrect printing of module binding with signature. https://github.com/rescript-lang/rescript-compiler/pull/6963
32+
- Fix incorrect printing of external with `@as` attribute and `_` placholder (fixed argument). https://github.com/rescript-lang/rescript-compiler/pull/6970
3233

3334
#### :house: Internal
3435

jscomp/syntax/src/res_parsetree_viewer.ml

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
open Parsetree
22

33
let arrow_type ?(arity = max_int) ct =
4+
let has_as_attr attrs =
5+
Ext_list.exists attrs (fun (x, _) -> x.Asttypes.txt = "as")
6+
in
47
let rec process attrs_before acc typ arity =
58
match typ with
6-
| typ when arity <= 0 -> (attrs_before, List.rev acc, typ)
9+
| typ when arity < 0 -> (attrs_before, List.rev acc, typ)
710
| {
811
ptyp_desc = Ptyp_arrow ((Nolabel as lbl), typ1, typ2);
912
ptyp_attributes = [];
@@ -25,8 +28,20 @@ let arrow_type ?(arity = max_int) ct =
2528
ptyp_desc = Ptyp_arrow (((Labelled _ | Optional _) as lbl), typ1, typ2);
2629
ptyp_attributes = attrs;
2730
} ->
31+
(* Res_core.parse_es6_arrow_type has a workaround that removed an extra arity for the function if the
32+
argument is a Ptyp_any with @as attribute i.e. ~x: @as(`{prop: value}`) _.
33+
34+
When this case is encountered we add that missing arity so the arrow is printed properly.
35+
*)
36+
let arity =
37+
match typ1 with
38+
| {ptyp_desc = Ptyp_any; ptyp_attributes = attrs1}
39+
when has_as_attr attrs1 ->
40+
arity
41+
| _ -> arity - 1
42+
in
2843
let arg = (attrs, lbl, typ1) in
29-
process attrs_before (arg :: acc) typ2 (arity - 1)
44+
process attrs_before (arg :: acc) typ2 arity
3045
| typ -> (attrs_before, List.rev acc, typ)
3146
in
3247
match ct with

jscomp/syntax/tests/printer/other/attributes.res

+27
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,30 @@ let x = 1
2424
)
2525
let x = 1
2626

27+
@obj external ff: (~x: int, ~g: int, ~h: int) => _ = ""
28+
29+
@obj external ff: (~x: int, ~g: int, ~h: @as(3) _) => _ = ""
30+
31+
@obj external ff: (~x: int) => (~h: @as(3) _) => _ = ""
32+
33+
@obj
34+
external ff: (
35+
~lo: @as(3) _,
36+
~mid: @as(3) _,
37+
~hi: int,
38+
) => _ = ""
39+
40+
41+
@obj
42+
external ff: (
43+
~hi: int,
44+
~lo: @as(3) _
45+
) => _ = ""
46+
47+
@obj
48+
external ff: (
49+
~hi: int,
50+
~mid: @as(3) _,
51+
~lo: @as(3) _
52+
) => _ = ""
53+

jscomp/syntax/tests/printer/other/expected/attributes.res.txt

+15
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,18 @@ let x = 1
2222

2323
@inlinePrivate(@module("./logo.svg") external logo: string = "default")
2424
let x = 1
25+
26+
@obj external ff: (~x: int, ~g: int, ~h: int) => _ = ""
27+
28+
@obj external ff: (~x: int, ~g: int, ~h: @as(3) _) => _ = ""
29+
30+
@obj external ff: (~x: int) => (~h: @as(3) _) => _ = ""
31+
32+
@obj
33+
external ff: (~lo: @as(3) _, ~mid: @as(3) _, ~hi: int) => _ = ""
34+
35+
@obj
36+
external ff: (~hi: int, ~lo: @as(3) _) => _ = ""
37+
38+
@obj
39+
external ff: (~hi: int, ~mid: @as(3) _, ~lo: @as(3) _) => _ = ""

0 commit comments

Comments
 (0)