Skip to content

Commit cfe1610

Browse files
authored
Parse doc comments in type parameters. (#6161)
* Parse doc comments in type parameters. * Added tests for interface files and externals.
1 parent cc00e83 commit cfe1610

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Fix issue where spreading record types with optional labels would not have their labels preserved as optional. https://github.com/rescript-lang/rescript-compiler/pull/6154
1919
- Fix error location to be the type with the spreads when spreading record types with duplicate labels. https://github.com/rescript-lang/rescript-compiler/pull/6157
2020
- Disable warning on `@inline` attibute on uncurried functions. https://github.com/rescript-lang/rescript-compiler/pull/6152
21+
- Support doc comments on arguments of function types. https://github.com/rescript-lang/rescript-compiler/pull/6161
2122

2223
# 11.0.0-alpha.3
2324

res_syntax/src/res_core.ml

+18-8
Original file line numberDiff line numberDiff line change
@@ -4125,14 +4125,21 @@ and parseTypeAlias p typ =
41254125
* | . type_parameter
41264126
*)
41274127
and parseTypeParameter p =
4128+
let docAttr : Parsetree.attributes =
4129+
match p.Parser.token with
4130+
| DocComment (loc, s) ->
4131+
Parser.next p;
4132+
[docCommentToAttribute loc s]
4133+
| _ -> []
4134+
in
41284135
if
41294136
p.Parser.token = Token.Tilde
41304137
|| p.token = Dot
41314138
|| Grammar.isTypExprStart p.token
41324139
then
41334140
let startPos = p.Parser.startPos in
41344141
let dotted = Parser.optional p Dot in
4135-
let attrs = parseAttributes p in
4142+
let attrs = docAttr @ parseAttributes p in
41364143
match p.Parser.token with
41374144
| Tilde -> (
41384145
Parser.next p;
@@ -4236,6 +4243,7 @@ and parseEs6ArrowType ~attrs p =
42364243
let returnType = parseTypExpr ~alias:false p in
42374244
let loc = mkLoc startPos p.prevEndPos in
42384245
Ast_helper.Typ.arrow ~loc ~attrs arg typ returnType
4246+
| DocComment _ -> assert false
42394247
| _ ->
42404248
let parameters = parseTypeParameters p in
42414249
Parser.expect EqualGreater p;
@@ -6399,15 +6407,17 @@ and parseAttribute p =
63996407
Some (attrId, payload)
64006408
| DocComment (loc, s) ->
64016409
Parser.next p;
6402-
Some
6403-
( {txt = "res.doc"; loc},
6404-
PStr
6405-
[
6406-
Ast_helper.Str.eval ~loc
6407-
(Ast_helper.Exp.constant ~loc (Pconst_string (s, None)));
6408-
] )
6410+
Some (docCommentToAttribute loc s)
64096411
| _ -> None
64106412

6413+
and docCommentToAttribute loc s : Parsetree.attribute =
6414+
( {txt = "res.doc"; loc},
6415+
PStr
6416+
[
6417+
Ast_helper.Str.eval ~loc
6418+
(Ast_helper.Exp.constant ~loc (Pconst_string (s, None)));
6419+
] )
6420+
64116421
and parseAttributes p =
64126422
parseRegion p ~grammar:Grammar.Attribute ~f:parseAttribute
64136423

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
let doc1: (/** ddd */ ~x: int) => int = (~x) => x + 1
2+
let doc2: @res.doc(" ddd ") int => int = x => x + 1
3+
let doc3: /** ddd */ int => int = x => x + 1
4+
let doc4: (/** ddd */ ~x: int, /** eee */ n) => int = (~x) => x + 1
5+
6+
module M : {
7+
let foo : (/** xxdoc */ ~x:int, /** yydoc */ ~y:int) => int
8+
} = {
9+
let foo= (~x, ~y) => x+y
10+
}
11+
12+
@val
13+
external ex : (/** ddd */ ~x: int, /** eee */ n) => int = "ex"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
let doc1: (/** ddd */ ~x: int) => int = (~x) => x + 1
2+
let doc2: /** ddd */ int => int = x => x + 1
3+
let doc3: /** ddd */ int => int = x => x + 1
4+
let doc4: (/** ddd */ ~x: int, /** eee */ n) => int = (~x) => x + 1
5+
6+
module M: {
7+
let foo: (/** xxdoc */ ~x: int, /** yydoc */ ~y: int) => int
8+
} = {
9+
let foo = (~x, ~y) => x + y
10+
}
11+
12+
@val
13+
external ex: (/** ddd */ ~x: int, /** eee */ n) => int = "ex"

0 commit comments

Comments
 (0)