Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse doc comments in type parameters. #6161

Merged
merged 2 commits into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- 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
- 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
- Disable warning on `@inline` attibute on uncurried functions. https://github.com/rescript-lang/rescript-compiler/pull/6152
- Support doc comments on arguments of function types. https://github.com/rescript-lang/rescript-compiler/pull/6161

# 11.0.0-alpha.3

Expand Down
26 changes: 18 additions & 8 deletions res_syntax/src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4125,14 +4125,21 @@ and parseTypeAlias p typ =
* | . type_parameter
*)
and parseTypeParameter p =
let docAttr : Parsetree.attributes =
match p.Parser.token with
| DocComment (loc, s) ->
Parser.next p;
[docCommentToAttribute loc s]
| _ -> []
in
if
p.Parser.token = Token.Tilde
|| p.token = Dot
|| Grammar.isTypExprStart p.token
then
let startPos = p.Parser.startPos in
let dotted = Parser.optional p Dot in
let attrs = parseAttributes p in
let attrs = docAttr @ parseAttributes p in
match p.Parser.token with
| Tilde -> (
Parser.next p;
Expand Down Expand Up @@ -4236,6 +4243,7 @@ and parseEs6ArrowType ~attrs p =
let returnType = parseTypExpr ~alias:false p in
let loc = mkLoc startPos p.prevEndPos in
Ast_helper.Typ.arrow ~loc ~attrs arg typ returnType
| DocComment _ -> assert false
| _ ->
let parameters = parseTypeParameters p in
Parser.expect EqualGreater p;
Expand Down Expand Up @@ -6399,15 +6407,17 @@ and parseAttribute p =
Some (attrId, payload)
| DocComment (loc, s) ->
Parser.next p;
Some
( {txt = "res.doc"; loc},
PStr
[
Ast_helper.Str.eval ~loc
(Ast_helper.Exp.constant ~loc (Pconst_string (s, None)));
] )
Some (docCommentToAttribute loc s)
| _ -> None

and docCommentToAttribute loc s : Parsetree.attribute =
( {txt = "res.doc"; loc},
PStr
[
Ast_helper.Str.eval ~loc
(Ast_helper.Exp.constant ~loc (Pconst_string (s, None)));
] )

and parseAttributes p =
parseRegion p ~grammar:Grammar.Attribute ~f:parseAttribute

Expand Down
13 changes: 13 additions & 0 deletions res_syntax/tests/printer/expr/DocComments.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let doc1: (/** ddd */ ~x: int) => int = (~x) => x + 1
let doc2: @res.doc(" ddd ") int => int = x => x + 1
let doc3: /** ddd */ int => int = x => x + 1
let doc4: (/** ddd */ ~x: int, /** eee */ n) => int = (~x) => x + 1

module M : {
let foo : (/** xxdoc */ ~x:int, /** yydoc */ ~y:int) => int
} = {
let foo= (~x, ~y) => x+y
}

@val
external ex : (/** ddd */ ~x: int, /** eee */ n) => int = "ex"
13 changes: 13 additions & 0 deletions res_syntax/tests/printer/expr/expected/DocComments.res.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let doc1: (/** ddd */ ~x: int) => int = (~x) => x + 1
let doc2: /** ddd */ int => int = x => x + 1
let doc3: /** ddd */ int => int = x => x + 1
let doc4: (/** ddd */ ~x: int, /** eee */ n) => int = (~x) => x + 1

module M: {
let foo: (/** xxdoc */ ~x: int, /** yydoc */ ~y: int) => int
} = {
let foo = (~x, ~y) => x + y
}

@val
external ex: (/** ddd */ ~x: int, /** eee */ n) => int = "ex"