refactor: Doc comments from Parameter AstNode. #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is tracking a refactor to allow doc comments to be inferred from the
ASTNode
tree.Problem
The Dart analyzer's Element model allows for accessing doc comments on an Element, but ParameterElements only ever contain doc comments if they are a Formal Parameter that relates to a field.
Only in the second case are we able to get doc comments from the Parameter element. What this means is that there is no idiomatic Dart way of inferring
--help
comments from a Parameter, at least from the element model.Potential Solutions
Access doc comments via ASTNode
We're easily able to get the doc comment via the ASTNode of the parameter. Users would simply document the parameter of a method/function (as shown above) which would generate the help text.
However, for the upcoming macros feature, it's unclear whether or not the API will allow access to the ASTs. If it is an option (or if we can e.g. read the raw source code and parse it ourselves), then this would likely be the go-to solution. If the AST tree is not available to macros, though, then this would be a case where the user experience is different between
build_runner
andmacros
.relevant issue
Doc Comments via Annotations
We already ship an
@Option
annotation, which allows doc comments and other explicit values to reach the element model via constant evaluation (i.e. theDartObject
interface).This isn't the most elegant solution, but it gets the job done. In the case of documenting every single parameter of a method, if users create complex methods with many args, the amount of annotation use will surely be a negative experience.
Arg
classesAnother solution is to recommend that users create separate Arg classes for each function, which would allow users to generate help text from doc comments on the
Field
s of the Arg class.This solution makes intuitive sense, and we could recommend this approach for all argument generation, so that it becomes the default way of doing things (rather than individual parameters on a method/function). We lose a little flexibility and gain some verbosity, but in return gain a good amount too. Its possible that
macros
would help trim the verbosity a good amount as well.This solution needs a little more investigation to see how effective it is in practice.