Skip to content

Commit 9c4af76

Browse files
shulhiShulhi Saplicknitt
authored
Fix @this attribute dropping parens (rescript-lang#7025)
* WIP * Check for existence of attr and arity Check for existence of attr at Ptyp_constr (function$) as the @this attribute is attached to the Ptyp_constr, not the Ptyp_arrow. * Another way to do it * Commit changes in test * Cleanup * Fix format * Update CHANGELOG --------- Co-authored-by: Shulhi Sapli <shulhi@mba-13.local> Co-authored-by: Christoph Knittel <ck@cca.io>
1 parent 4ab9a5f commit 9c4af76

File tree

6 files changed

+37
-29
lines changed

6 files changed

+37
-29
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#### :bug: Bug fix
2121

2222
- Fix tuple coercion. https://github.com/rescript-lang/rescript-compiler/pull/7024
23+
- Fix attribute printing. https://github.com/rescript-lang/rescript-compiler/pull/7025
2324

2425
#### :nail_care: Polish
2526

jscomp/syntax/src/res_parsetree_viewer.ml

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
open Parsetree
22

3-
let arrow_type ?(arity = max_int) ct =
3+
let arrow_type ?(arity = max_int) ?(attrs = []) ct =
44
let has_as_attr attrs =
55
Ext_list.exists attrs (fun (x, _) -> x.Asttypes.txt = "as")
66
in
@@ -45,10 +45,11 @@ let arrow_type ?(arity = max_int) ct =
4545
| typ -> (attrs_before, List.rev acc, typ)
4646
in
4747
match ct with
48-
| {ptyp_desc = Ptyp_arrow (Nolabel, _typ1, _typ2); ptyp_attributes = attrs} as
49-
typ ->
48+
| {ptyp_desc = Ptyp_arrow (Nolabel, _typ1, _typ2); ptyp_attributes = attrs1}
49+
as typ ->
50+
let attrs = attrs @ attrs1 in
5051
process attrs [] {typ with ptyp_attributes = []} arity
51-
| typ -> process [] [] typ arity
52+
| typ -> process attrs [] typ arity
5253

5354
let functor_type modtype =
5455
let rec process acc modtype =

jscomp/syntax/src/res_parsetree_viewer.mli

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* we restructure the tree into (a, b, c) and its returnType d *)
44
val arrow_type :
55
?arity:int ->
6+
?attrs:Parsetree.attributes ->
67
Parsetree.core_type ->
78
Parsetree.attributes
89
* (Parsetree.attributes * Asttypes.arg_label * Parsetree.core_type) list

jscomp/syntax/src/res_printer.ml

+7-1
Original file line numberDiff line numberDiff line change
@@ -1591,9 +1591,13 @@ and print_label_declaration ~state (ld : Parsetree.label_declaration) cmt_tbl =
15911591
])
15921592

15931593
and print_typ_expr ~(state : State.t) (typ_expr : Parsetree.core_type) cmt_tbl =
1594+
let parent_attrs =
1595+
let attrs = ParsetreeViewer.filter_parsing_attrs typ_expr.ptyp_attributes in
1596+
if Ast_uncurried.core_type_is_uncurried_fun typ_expr then attrs else []
1597+
in
15941598
let print_arrow ?(arity = max_int) typ_expr =
15951599
let attrs_before, args, return_type =
1596-
ParsetreeViewer.arrow_type ~arity typ_expr
1600+
ParsetreeViewer.arrow_type ~arity ~attrs:parent_attrs typ_expr
15971601
in
15981602
let return_type_needs_parens =
15991603
match return_type.ptyp_desc with
@@ -1841,6 +1845,8 @@ and print_typ_expr ~(state : State.t) (typ_expr : Parsetree.core_type) cmt_tbl =
18411845
in
18421846
let should_print_its_own_attributes =
18431847
match typ_expr.ptyp_desc with
1848+
| Ptyp_constr _ when Ast_uncurried.core_type_is_uncurried_fun typ_expr ->
1849+
true
18441850
| Ptyp_arrow _ (* es6 arrow types print their own attributes *) -> true
18451851
| _ -> false
18461852
in

jscomp/syntax/tests/printer/typexpr/expected/arrow.res.txt

+22-22
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ type t = (
125125
type t = (@attr string, @attr float) => unit
126126
type t = (@attr @attr2 string, @attr @attr2 float, @attr3 int) => unit
127127

128-
type t = @attr string => unit
128+
type t = @attr (string => unit)
129129
type t = @attr (foo, bar, baz) => unit
130130
type t = @attr (foo, @attr2 ~f: bar, @attr3 ~f: baz) => unit
131131

132-
type t = @attr string => @attr int => unit
132+
type t = @attr (string => @attr (int => unit))
133133
type t = @attr (string, int) => @attr (int, float) => unit
134-
type t = @attr int => @attr (int, float) => @attr unit => unit => unit
134+
type t = @attr (int => @attr (int, float) => @attr (unit => unit => unit))
135135
type t = @attr (@attr2 ~f: int, @attr3 ~g: float) => unit
136136

137137
type f = (
@@ -144,50 +144,50 @@ type f = (
144144
@attr3 ~h: ccccrazysldkfjslkdjflksdjkf=?,
145145
) => unit
146146

147-
type t = @attr
148-
stringWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong => @attr2
149-
floatWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong => @attr3
150-
intWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong => unitWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong
147+
type t = @attr (
148+
stringWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong => @attr2 (
149+
floatWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong => @attr3 (
150+
intWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong => unitWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong
151+
)
152+
)
153+
)
151154

152-
type t = @attr
153-
(
155+
type t = @attr (
154156
fooWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong,
155157
barWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong,
156158
bazWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong,
157-
) => @attr2
158-
(
159+
) => @attr2 (
159160
stringWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong,
160161
floatWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong,
161162
) => unit
162163

163164
type t = @attr
164165
@attrWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong
165-
@attrWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong
166-
(
166+
@attrWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong (
167167
fooWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong,
168168
barWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong,
169169
bazWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong,
170170
) => @attr2
171171
@attrWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong
172-
@attrWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong
173-
(
172+
@attrWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong (
174173
stringWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong,
175174
floatWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong,
176175
) => unit
177176

178177
external debounce: (int, @meth unit) => unit = "debounce"
179178

180-
external debounce: int => @meth unit => unit = "debounce"
179+
external debounce: int => @meth (unit => unit) = "debounce"
181180

182-
external debounce: (int, @meth unit => unit) => @meth unit => unit = "debounce"
181+
external debounce: (int, @meth (unit => unit)) => @meth (unit => unit) = "debounce"
183182

184-
external debounce: (int, @meth unit => unit, @meth unit => unit) => @meth unit => unit = "debounce"
183+
external debounce: (int, @meth (unit => unit), @meth (unit => unit)) => @meth (unit => unit) =
184+
"debounce"
185185

186186
external debounce: (
187187
int,
188-
@meth unit => unit,
189-
@meth unit => @meth unit => unit,
190-
) => @meth unit => unit = "debounce"
188+
@meth (unit => unit),
189+
@meth (unit => @meth (unit => unit)),
190+
) => @meth (unit => unit) = "debounce"
191191

192192
type returnTyp = (int, int) => @magic float
193193
type returnTyp = (
@@ -214,7 +214,7 @@ type t = (@attrOnInt int, @attrOnInt int, @attrOnInt int, @attrOnInt int) => int
214214
type t = (@attr ~x: int, ~y: int, @attr ~z: int, @attr ~omega: int) => unit
215215

216216
@val external requestAnimationFrame: (float => unit) => unit = "requestAnimationFrame"
217-
@val external requestAnimationFrame: @attr (float => unit) => unit = "requestAnimationFrame"
217+
@val external requestAnimationFrame: @attr ((float => unit) => unit) = "requestAnimationFrame"
218218

219219
type arrows = (int, (float => unit) => unit, float) => unit
220220

jscomp/test/reasonReact.res

+1-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ and oldNewSelf<'state, 'retainedProps, 'action> = {
117117
type rec jsComponentThis<'state, 'props, 'retainedProps, 'action> = {
118118
"state": totalState<'state, 'retainedProps, 'action>,
119119
"props": {"reasonProps": 'props},
120-
"setState": @meth
121-
(
120+
"setState": @meth (
122121
(
123122
totalState<'state, 'retainedProps, 'action>,
124123
'props,

0 commit comments

Comments
 (0)