diff --git a/CHANGELOG.md b/CHANGELOG.md index 742d803c83..82f1b7d07b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/res_syntax/src/res_core.ml b/res_syntax/src/res_core.ml index f066f5527e..da16a57592 100644 --- a/res_syntax/src/res_core.ml +++ b/res_syntax/src/res_core.ml @@ -4125,6 +4125,13 @@ 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 @@ -4132,7 +4139,7 @@ and parseTypeParameter p = 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; @@ -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; @@ -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 diff --git a/res_syntax/tests/printer/expr/DocComments.res b/res_syntax/tests/printer/expr/DocComments.res new file mode 100644 index 0000000000..c2188c7b77 --- /dev/null +++ b/res_syntax/tests/printer/expr/DocComments.res @@ -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" diff --git a/res_syntax/tests/printer/expr/expected/DocComments.res.txt b/res_syntax/tests/printer/expr/expected/DocComments.res.txt new file mode 100644 index 0000000000..70c276d364 --- /dev/null +++ b/res_syntax/tests/printer/expr/expected/DocComments.res.txt @@ -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"