-
Notifications
You must be signed in to change notification settings - Fork 469
Regression: comment on labeled argument formatted away #6094
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
Comments
Works on master. |
Confirmed, this is working again now. |
@cristianoc Actually the problem is still there, but it happens in uncurried mode only. |
So that position is something that does not correspond to anything in the parse tree. There's going to be some workaround to represent a comment in that position, which does not like the transformation to uncurried function definition. Something perhaps even more important, is that one cannot put doc comments in that position (syntax error). CC @zth who had been asking about this last thing. As a separate thought, I was wondering whether normal comments should be treated exactly like doc comments. That would limit the positions comments can be places, but would imply the comments are in the AST so the outcome would be much more predictable. |
Had a chat with Intelligentia A. about the topic. Turns out there's something one could do and has not been explored: ContextThis discussion revolves around the ReScript language, which uses a handwritten parser and is bound to the existing OCaml AST definitions for compatibility reasons. The challenge is to find a way to represent comments in the AST, particularly when it comes to pretty printing and doc comments, while preserving compatibility with other tools. Proposed Solution: Extended OCaml ASTA proposed solution is to create a pure extension of the OCaml AST, where nodes only become bigger to store additional information, such as comments. This extended AST would be written to disk, maintaining compatibility with other tools. Benefits
Potential Dangers
TestingThe extended AST has been tested to ensure that the OCaml serialization and deserialization mechanisms can handle the extended AST gracefully, and the tools can ignore the extra information. ConclusionGiven the successful test results, the extended OCaml AST approach seems to be a viable solution for representing comments in the ReScript language while preserving compatibility with other tools. Thorough testing with various ReScript programs and different tools and libraries is crucial to ensure the robustness and compatibility of this solution. Engaging with the ReScript and OCaml communities can provide additional insights and feedback to further refine the approach. |
This seems to be a parser issue only, because attributes attach fine to the arguments (using let someFunc = (
@ns.doc("Fine arg.") ~arg1: bool,
@ns.doc("Fine arg 2.") ~arg2: option<string>=?,
@ns.doc("Fine arg 3.") arg3: string,
) => {
ignore(arg1)
ignore(arg2)
ignore(arg3)
} Haven't checked where the attributes end up though, but I assume it must be on the function. And haven't checked whether they "make it" to the typed tree. But they should if they end up attached to the function. I'm very interested in supporting this use case. Both for the WIP doc extraction, and for leveraging it in the editor for showing documentation. |
[
structure_item
...
<def>
pattern
Ppat_var "someFunc"
expression
Pexp_fun
Labelled "arg1"
None
pattern
attribute "res.namedArgLoc"
[]
attribute "ns.doc"
[
...
expression
Pexp_constant PConst_string ("Fine arg.", Some "*j")
]
Ppat_constraint
pattern
Ppat_var "arg1"
core_type
Ptyp_constr "bool"
[]
expression
Pexp_fun
Optional "arg2"
None
pattern
attribute "res.namedArgLoc"
[]
attribute "ns.doc"
[
...
expression
Pexp_constant PConst_string ("Fine arg 2.", Some "*j")
]
Ppat_constraint
pattern
Ppat_var "arg2"
core_type
Ptyp_constr "option"
[
core_type
Ptyp_constr "string"
[]
]
expression
Pexp_fun
Nolabel
None
pattern
attribute "ns.doc"
[
...
expression
Pexp_constant PConst_string ("Fine arg 3.", Some "*j")
]
Ppat_constraint
pattern
Ppat_var "arg3"
core_type
Ptyp_constr "string"
[]
expression
...
] |
So yes ^ it's on the function. |
Can you see whether it ends up in the typed tree? |
I know it does. But I can double check. |
It is actually not true. I know it does for places where there's a natural place to store the attribute.
|
This is the parse tree:
This is the typed tree (printer modified to show the number of attributes):
So it looks like the attributes are indeed lost. |
So one needs to:
|
Perhaps this is a good opportunity to test the ast extension idea where: |
yes it goes nowhere |
Here's an example PR that extends the parse tree with attributes on function definitions: #6155 After doing that, it occurred to me that this was misguided. In this specific example, putting the attribute on the pattern is the correct thing to do. Kind of forgot that However, the PR still illustrates how to extend the parse tree, so I've out it up anyway. The real place where the attribute is missing, is in the function type declaration, not in the function declaration, and one can look into that separately. |
On further investigation, the typed tree for let foo = (@res.doc("ddd") ~x) => x+1 does not have any explicit types, so there's nothing to look at in the typed tree. let foo: (@res.doc("ddd") ~x: int) => int = (~x) => x + 1 This latter example has the doc comment in the typed tree: structure_item
Tstr_value Nonrec
[
<def>
pattern
Tpat_constraint
core_type
attribute "res.doc"
[
structure_item
Pstr_eval
expression
Pexp_constant PConst_string ("ddd",Some "*j")
]
Ttyp_arrow
Labelled "x"
... CC @zth what do you think? |
This essentially means that you'd need an explicit type definition for the function to be able to use docstrings for args. You have that in these cases:
The cases where it won't work is for ad hoc functions that you rely on inference for. But those shouldn't be the main case needing docstrings anyway, compared to the cases listed above. Sounds like a perfectly acceptable situation to me. @cristianoc fixing so that one can use the dedicated docstrings syntax should be easy, right? |
Fixing the parser should be easy. |
@cristianoc you do the parser, I do the extension? |
Parser: #6161 |
The original issue is fixed here: #6169 |
Uh oh!
There was an error while loading. Please reload this page.
The comment in
is preserved when formatting with 10.1.3, but gets removed when formatting with master:
The text was updated successfully, but these errors were encountered: