Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 7cc70c9

Browse files
author
Iwan
committed
Fix printing of comments on Ptyp_arrow with labels.
Fixes #200
1 parent 587b640 commit 7cc70c9

File tree

11 files changed

+120
-42
lines changed

11 files changed

+120
-42
lines changed

src/res_comments_table.ml

+9-4
Original file line numberDiff line numberDiff line change
@@ -1279,13 +1279,12 @@ let rec walkStructure s t comments =
12791279
attach t.trailing callExpr.pexp_loc afterExpr;
12801280
walkList
12811281
~getLoc:(fun (_argLabel, expr) ->
1282-
let loc = match expr.Parsetree.pexp_attributes with
1282+
match expr.Parsetree.pexp_attributes with
12831283
| ({Location.txt = "ns.namedArgLoc"; loc}, _)::_attrs ->
12841284
{loc with loc_end = expr.pexp_loc.loc_end}
12851285
| _ ->
12861286
expr.pexp_loc
1287-
in
1288-
loc)
1287+
)
12891288
~walkNode:walkExprArgument
12901289
arguments
12911290
t
@@ -1840,7 +1839,13 @@ and walkExprArgument (_argLabel, expr) t comments =
18401839

18411840
and walkTypeParameters typeParameters t comments =
18421841
visitListButContinueWithRemainingComments
1843-
~getLoc:(fun (_, _, typexpr) -> typexpr.Parsetree.ptyp_loc)
1842+
~getLoc:(fun (_, _, typexpr) ->
1843+
match typexpr.Parsetree.ptyp_attributes with
1844+
| ({Location.txt = "ns.namedArgLoc"; loc}, _)::_attrs ->
1845+
{loc with loc_end = typexpr.ptyp_loc.loc_end}
1846+
| _ ->
1847+
typexpr.ptyp_loc
1848+
)
18441849
~walkNode:walkTypeParameter
18451850
~newlineDelimited:false
18461851
typeParameters

src/res_core.ml

+14-5
Original file line numberDiff line numberDiff line change
@@ -3828,9 +3828,13 @@ and parseTypeParameter p =
38283828
match p.Parser.token with
38293829
| Tilde ->
38303830
Parser.next p;
3831-
let (name, _loc) = parseLident p in
3831+
let (name, loc) = parseLident p in
3832+
let lblLocAttr = (Location.mkloc "ns.namedArgLoc" loc, Parsetree.PStr []) in
38323833
Parser.expect ~grammar:Grammar.TypeExpression Colon p;
3833-
let typ = parseTypExpr p in
3834+
let typ =
3835+
let typ = parseTypExpr p in
3836+
{typ with ptyp_attributes = lblLocAttr::typ.ptyp_attributes}
3837+
in
38343838
begin match p.Parser.token with
38353839
| Equal ->
38363840
Parser.next p;
@@ -3899,9 +3903,13 @@ and parseEs6ArrowType ~attrs p =
38993903
match p.Parser.token with
39003904
| Tilde ->
39013905
Parser.next p;
3902-
let (name, _loc) = parseLident p in
3906+
let (name, loc) = parseLident p in
3907+
let lblLocAttr = (Location.mkloc "ns.namedArgLoc" loc, Parsetree.PStr []) in
39033908
Parser.expect ~grammar:Grammar.TypeExpression Colon p;
3904-
let typ = parseTypExpr ~alias:false ~es6Arrow:false p in
3909+
let typ =
3910+
let typ = parseTypExpr ~alias:false ~es6Arrow:false p in
3911+
{typ with ptyp_attributes = lblLocAttr::typ.ptyp_attributes}
3912+
in
39053913
let arg = match p.Parser.token with
39063914
| Equal ->
39073915
Parser.next p;
@@ -3912,7 +3920,8 @@ and parseEs6ArrowType ~attrs p =
39123920
in
39133921
Parser.expect EqualGreater p;
39143922
let returnType = parseTypExpr ~alias:false p in
3915-
Ast_helper.Typ.arrow ~attrs arg typ returnType
3923+
let loc = mkLoc startPos p.prevEndPos in
3924+
Ast_helper.Typ.arrow ~loc ~attrs arg typ returnType
39163925
| _ ->
39173926
let parameters = parseTypeParameters p in
39183927
Parser.expect EqualGreater p;

src/res_printer.ml

+9-2
Original file line numberDiff line numberDiff line change
@@ -1612,7 +1612,9 @@ and printTypExpr (typExpr : Parsetree.core_type) cmtTbl =
16121612
Doc.softLine;
16131613
if isUncurried then Doc.concat [Doc.dot; Doc.space] else Doc.nil;
16141614
Doc.join ~sep:(Doc.concat [Doc.comma; Doc.line]) (
1615-
List.map (fun tp -> printTypeParameter tp cmtTbl) args
1615+
List.map (fun tp ->
1616+
printTypeParameter tp cmtTbl
1617+
) args
16161618
)
16171619
]
16181620
);
@@ -1812,6 +1814,11 @@ and printTypeParameter (attrs, lbl, typ) cmtTbl =
18121814
| Labelled _ -> Doc.nil
18131815
| Optional _lbl -> Doc.text "=?"
18141816
in
1817+
let (loc, typ) = match typ.ptyp_attributes with
1818+
| ({Location.txt = "ns.namedArgLoc"; loc}, _)::attrs ->
1819+
({loc with loc_end = typ.ptyp_loc.loc_end}, {typ with ptyp_attributes = attrs})
1820+
| _ -> (typ.ptyp_loc, typ)
1821+
in
18151822
let doc = Doc.group (
18161823
Doc.concat [
18171824
uncurried;
@@ -1821,7 +1828,7 @@ and printTypeParameter (attrs, lbl, typ) cmtTbl =
18211828
optionalIndicator;
18221829
]
18231830
) in
1824-
printComments doc cmtTbl typ.ptyp_loc
1831+
printComments doc cmtTbl loc
18251832

18261833
and printValueBinding ~recFlag vb cmtTbl i =
18271834
let (hasGenType, attrs) = ParsetreeViewer.splitGenTypeAttr vb.pvb_attributes in

tests/parsing/errors/other/__snapshots__/parse.spec.js.snap

+2-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ exports[`patternMatching.js 1`] = `
9595
exports[`regionMissingComma.res 1`] = `
9696
"=====Parsetree==========================================
9797
external make :
98-
?style:ReactDOMRe.Style.t -> ((?image:bool -> React.element)[@bs ]) =
98+
?style:((ReactDOMRe.Style.t)[@ns.namedArgLoc ]) ->
99+
((?image:((bool)[@ns.namedArgLoc ]) -> React.element)[@bs ]) =
99100
\\"ModalContent\\"
100101
type nonrec 'extraInfo student =
101102
{

tests/parsing/errors/typexpr/__snapshots__/parse.spec.js.snap

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ module Error2 =
88
type nonrec observation =
99
{
1010
observed: int ;
11-
onStep: currentValue:unit -> [%rescript.typehole ] }
11+
onStep:
12+
currentValue:((unit)[@ns.namedArgLoc ]) -> [%rescript.typehole ] }
1213
end
1314
module Error3 =
1415
struct
1516
type nonrec observation =
1617
{
1718
observed: int ;
18-
onStep: currentValue:unit -> [%rescript.typehole ] }
19+
onStep:
20+
currentValue:((unit)[@ns.namedArgLoc ]) -> [%rescript.typehole ] }
1921
end
2022
=====Errors=============================================
2123
@@ -206,8 +208,8 @@ type nonrec websocket = < id: [%rescript.typehole ] > Js.t
206208
207209
exports[`garbage.res 1`] = `
208210
"=====Parsetree==========================================
209-
external printName : name:unit -> unit = \\"printName\\"[@@bs.module
210-
\\"moduleName\\"]
211+
external printName : name:((unit)[@ns.namedArgLoc ]) -> unit = \\"printName\\"
212+
[@@bs.module \\"moduleName\\"]
211213
=====Errors=============================================
212214
213215
Syntax error!

tests/parsing/grammar/signature/__snapshots__/parse.spec.js.snap

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ exports[`external.js 1`] = `
2222
"module type Signature =
2323
sig
2424
type nonrec t
25-
external linkProgram : t -> program:webGlProgram -> unit = \\"linkProgram\\"
25+
external linkProgram :
26+
t -> program:((webGlProgram)[@ns.namedArgLoc ]) -> unit = \\"linkProgram\\"
2627
[@@bs.send ]
2728
external add_nat :
2829
nat -> int -> int -> int = \\"add_nat_bytecode\\" \\"add_nat_native\\"

tests/parsing/grammar/structure/__snapshots__/parse.spec.js.snap

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ exports[`externalDefinition.js 1`] = `
3535
"external clear : t -> int -> unit = \\"clear\\"
3636
external add_nat : nat -> int = \\"add_nat_bytecode\\" \\"add_nat_native\\"
3737
external attachShader :
38-
t -> program:webGlProgram -> shader:webGlShader -> unit = \\"attachShader\\"
38+
t ->
39+
program:((webGlProgram)[@ns.namedArgLoc ]) ->
40+
shader:((webGlShader)[@ns.namedArgLoc ]) -> unit = \\"attachShader\\"
3941
[@@bs.send ]
4042
external svg : unit -> React.element = \\"svg\\"
4143
external svg : unit -> React.element = \\"svg\\"

tests/parsing/grammar/typedefinition/__snapshots__/parse.spec.js.snap

+2-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ type nonrec t = private int
166166
type nonrec t = private int -> int
167167
type nonrec t = private int -> int
168168
type nonrec t = private int -> int -> int
169-
type nonrec t = private int -> x:string -> float -> unit
169+
type nonrec t = private
170+
int -> x:((string)[@ns.namedArgLoc ]) -> float -> unit
170171
type nonrec t = private string as 'x
171172
type nonrec t = private [%ext ]
172173
type nonrec t = private [%ext \\"console.log\\"]

tests/parsing/grammar/typexpr/__snapshots__/parse.spec.js.snap

+32-19
Original file line numberDiff line numberDiff line change
@@ -54,33 +54,46 @@ exports[`es6Arrow.js 1`] = `
5454
"type nonrec t = x -> unit
5555
type nonrec t = x -> unit
5656
type nonrec t = int -> string -> unit
57-
type nonrec t = a:int -> b:int -> int
58-
type nonrec t = ?a:int -> ?b:int -> int
57+
type nonrec t =
58+
a:((int)[@ns.namedArgLoc ]) -> b:((int)[@ns.namedArgLoc ]) -> int
59+
type nonrec t =
60+
?a:((int)[@ns.namedArgLoc ]) -> ?b:((int)[@ns.namedArgLoc ]) -> int
5961
type nonrec t = int -> int -> int -> int
60-
type nonrec t = a:int -> b:int -> c:int -> int
62+
type nonrec t =
63+
a:((int)[@ns.namedArgLoc ]) ->
64+
b:((int)[@ns.namedArgLoc ]) -> c:((int)[@ns.namedArgLoc ]) -> int
6165
let (f : x -> unit) = xf
6266
let (f : x -> unit) = xf
6367
let (f : int -> string -> unit) = xf
64-
let (t : a:int -> b:int -> int) = xf
65-
let (t : ?a:int -> ?b:int -> int) = xf
68+
let (t : a:((int)[@ns.namedArgLoc ]) -> b:((int)[@ns.namedArgLoc ]) -> int) =
69+
xf
70+
let (t : ?a:((int)[@ns.namedArgLoc ]) -> ?b:((int)[@ns.namedArgLoc ]) -> int)
71+
= xf
6672
let (t : int -> int -> int -> int) = xf
67-
let (t : a:int -> b:int -> c:int -> int) = xf
68-
type nonrec t = f:int -> string
69-
type nonrec t = ?f:int -> string
70-
let (f : f:int -> string) = fx
71-
let (f : ?f:int -> string) = fx
72-
type nonrec t = f:int -> string
73-
type nonrec t = f:int -> string
74-
type nonrec t = f:(int -> string) -> float
75-
type nonrec t = f:(int -> string) -> float
76-
type nonrec t = f:int -> string -> float
73+
let (t :
74+
a:((int)[@ns.namedArgLoc ]) ->
75+
b:((int)[@ns.namedArgLoc ]) -> c:((int)[@ns.namedArgLoc ]) -> int)
76+
= xf
77+
type nonrec t = f:((int)[@ns.namedArgLoc ]) -> string
78+
type nonrec t = ?f:((int)[@ns.namedArgLoc ]) -> string
79+
let (f : f:((int)[@ns.namedArgLoc ]) -> string) = fx
80+
let (f : ?f:((int)[@ns.namedArgLoc ]) -> string) = fx
81+
type nonrec t = f:((int)[@ns.namedArgLoc ]) -> string
82+
type nonrec t = f:((int)[@ns.namedArgLoc ]) -> string
83+
type nonrec t = f:((int -> string)[@ns.namedArgLoc ]) -> float
84+
type nonrec t = f:((int -> string)[@ns.namedArgLoc ]) -> float
85+
type nonrec t = f:((int)[@ns.namedArgLoc ]) -> string -> float
7786
type nonrec t =
78-
((a:int -> ((b:int -> ((float)[@attr ]) -> unit)[@attrBeforeLblB ]))
87+
((a:((int)[@ns.namedArgLoc ]) ->
88+
((b:((int)[@ns.namedArgLoc ]) -> ((float)[@attr ]) -> unit)[@attrBeforeLblB
89+
]))
7990
[@attrBeforeLblA ])
8091
type nonrec t =
81-
((a:int -> ((b:int -> ((float)[@attr ]) -> unit)[@attrBeforeLblB ]))
92+
((a:((int)[@ns.namedArgLoc ]) ->
93+
((b:((int)[@ns.namedArgLoc ]) -> ((float)[@attr ]) -> unit)[@attrBeforeLblB
94+
]))
8295
[@attrBeforeLblA ])
83-
type nonrec t = ((a:int -> unit)[@attr ])
96+
type nonrec t = ((a:((int)[@ns.namedArgLoc ]) -> unit)[@attr ])
8497
type nonrec 'a getInitialPropsFn =
8598
< query: string Js.Dict.t ;req: 'a Js.t Js.Nullable.t > Js.t ->
8699
'a Js.t Js.Promise.t"
@@ -106,7 +119,7 @@ type nonrec t = (module Console) ref
106119
let (devices : (string, (module DEVICE)) Hastbl.t) = Hashtbl.creat 17"
107120
`;
108121

109-
exports[`parenthesized.js 1`] = `"type nonrec t = ((a:int -> unit)[@attr ])"`;
122+
exports[`parenthesized.js 1`] = `"type nonrec t = ((a:((int)[@ns.namedArgLoc ]) -> unit)[@attr ])"`;
110123

111124
exports[`poly.js 1`] = `
112125
"external getLogger :

tests/printer/comments/__snapshots__/render.spec.js.snap

+24-4
Original file line numberDiff line numberDiff line change
@@ -1808,16 +1808,36 @@ type add = /* before */ (
18081808
/* c2 */ int /* c3 */,
18091809
) => /* before return */ int /* after */
18101810
type add = /* before */ (
1811-
~a: /* c0 */ int /* c1 */,
1812-
~b: /* c2 */ int /* c3 */,
1811+
/* c0 */ ~a: int /* c1 */,
1812+
/* c2 */ ~b: int /* c3 */,
18131813
) => /* before return */ int /* after */
18141814
type multiply = /* before */ (
1815-
~fn: /* c0 */ (
1815+
/* c0 */ ~fn: (
18161816
/* cinner0 */ int /* cinner1 */,
18171817
/* cinner2 */ int /* cinner3 */,
18181818
) => /* cx */ int /* c1 */,
1819-
~b: /* c2 */ int /* c3 */,
1819+
/* c2 */ ~b: int /* c3 */,
18201820
) => int /* after */
1821+
type make = (
1822+
~activity: ActivityFlow_Activity.t,
1823+
~showExpert: (string, string) => unit,
1824+
~showCognitiveAspect: string => unit,
1825+
~next: (string, string, array<string>) => unit,
1826+
~hapticFeedback: unit => unit,
1827+
~participants: Belt.Set.String.t,
1828+
// here
1829+
1830+
// above
1831+
/* c0 */ ~openPhotoFeed: /* c1 */ (
1832+
/* c2 */ ~feedId: /* c3 */ option<string> /* c4 */,
1833+
/* c5 */ unit /* c6 */,
1834+
) => /* c7 */ unit /* c8 */,
1835+
// only provide this when it shows a random activity
1836+
~getAnother: unit => unit=?,
1837+
) => React.element
1838+
1839+
type f = /* c0 */ (~a: /* c1 */ int /* c2 */) => /* c3 */ int /* c4 */
1840+
type f = (/* c0 */ ~a: /* c1 */ int /* c2 */) => /* c3 */ int /* c4 */
18211841
18221842
// Ptyp_object
18231843
type jsUser = /* before */ {

tests/printer/comments/typexpr.res

+17
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,23 @@ type fn = {
3232
type add = /* before */ ( /* c0 */ int /* c1 */, /* c2 */ int /* c3 */) => /* before return */ int /* after */
3333
type add = /* before */ ( /* c0 */ ~a:int /* c1 */, /* c2 */ ~b:int /* c3 */) => /* before return */ int /* after */
3434
type multiply = /* before */ ( /* c0 */ ~fn: (/* cinner0 */ int /* cinner1 */, /* cinner2 */int /*cinner3 */) => /*cx*/ int /* c1 */, /* c2 */ ~b:int /* c3 */) => int /* after */
35+
type make = (
36+
~activity: ActivityFlow_Activity.t,
37+
~showExpert: (string, string) => unit,
38+
~showCognitiveAspect: string => unit,
39+
~next: (string, string, array<string>) => unit,
40+
~hapticFeedback: unit => unit,
41+
~participants: Belt.Set.String.t,
42+
// here
43+
44+
// above
45+
/* c0 */ ~openPhotoFeed: /* c1 */ (/* c2 */~feedId: /* c3 */ option<string> /* c4 */, /* c5 */ unit /* c6 */) => /* c7 */ unit /* c8 */,
46+
// only provide this when it shows a random activity
47+
~getAnother: unit => unit=?,
48+
) => React.element
49+
50+
type f = /* c0 */ ~a: /* c1 */ int /* c2 */ => /* c3 */ int /* c4 */
51+
type f = (/* c0 */ ~a: /* c1 */ int /* c2 */) => /* c3 */ int /* c4 */
3552

3653
// Ptyp_object
3754
type jsUser = /* before */ {

0 commit comments

Comments
 (0)