-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Introduce a syntax for referencing out-of-scope elements from a doc comment. #49073
Comments
Only comment: wondering if we should "lint" relative paths that point from public APIs to non-public APIs. Like a link in But a link within the the |
Do you have any data on how common the current strategies for linking to out-of-scope elements mentioned in the "MIGRATION PLAN" section are in a larger codebase like, say, Flutter? I am wondering how much work it would be to make Flutter's docs warning-free again after this proposal has been implemented... |
To pile on: it would be GREAT to have tooling to flag imports that only exist to support doc comment references. To pile on more: add a quick-fix to replace these w/ the new syntax 🤯 |
@kevmoo those ideas are in the migration plan. Great minds... 😄 @goderbauer I've found in flutter stats for "what if we didn't count a doc comment reference as a usage? How many imports would become "unused"? There are about ~50 imports like this in flutter/flutter, and ~10 in flutter/plugins. I haven't yet figured out how many cases there are of |
From design review:
|
You mentioned a documentation plan is out of scope of this design, but it may be worth include adding some information to Dartdoc comment references once implemented. If dartdoc is committing to this reference syntax, it might be time to more formally specify the entire syntax on a dedicated page on dart.dev as well :) |
As for how it will render in HTML, it would be nice to only include the final element (after the IDEs may be able to implement similar logic to compress the longer references visually (then expand on click or something) and perhaps other tools but this wouldn't really handle the long case on GitHub. |
Thanks @goderbauer and @parlough! More notes:
|
Another alternative, a little more low-tech, would be to have doc-only imports. Say a comment like: /// @dartdoc import "package:flutter/widget.dart" would make the contents of that library available for DartDoc referencing, as if it had been imported normally (probably taking precedence in case of a conflict, since it's intended for documenting. It means |
FYI, I've transcribed this proposal into a design doc that is shared publicly for easier commenting. |
I've written an alternative proposal for an import syntax, at #50702, based on @lrhn 's comment above. |
Introduce a syntax for referencing out-of-scope elements from a doc comment.
WHAT PROBLEM IS THIS SOLVING?
Users can reference elements in doc comments (
///
-style) with square brackets. For example,/// Returns a [Gadget]
and/// Must be called after [Gadget.initialize]
. These elements can then be cross-linked in the IDE, code search, and doc sites.However, such elements must be in-scope; they must be imported into, or declared within, the library with the doc comment. This design introduces a syntax for referencing out-of-scope elements.
BACKGROUND
Audience
Anyone writing documented Dart code.
Glossary
///
), and which is placed just before a library element (class, top-level function, top-level constant, etc.) or a class/mixin/enum/extension element (method, field, etc.). A doc comment is parsed (by various tools) as Markdown text.[List]
or[Future.wait]
.show
andhide
combinators).OVERVIEW
The basic design is to allow out-of-scope elements to be referenced in a doc comment by referencing the element’s library, via the same support URIs, and name, delimited by a hash mark (
#
). Quick examples:That last example indicates that a reference can also be made to a Dart library.
Non-goals
There are several asks to enhance the support of what can go into a doc comment. For example indicating a callable element with parentheses (e.g.
/// [Future.wait()]
) or multiple properties (e.g./// [myList.toSet().length]
) or even just snippets of code (e.g./// [await thisMethod()]
or/// [thisConst + thatConst]
). These also just show up as plain, unlinked text in the IDE and in doc sites. Enhancements like these are out-of-scope.USAGE EXAMPLES
Examples of legal usage of the new syntax:
Examples of non-working usage of the new syntax:
DETAILED DESIGN/DISCUSSION
When a tool which parses doc comment references sees a possible element reference between square brackets, it can look for a hash mark (
#
), and resolve the text to the left as a URI, and the right as an element (one identifier or two separated by a period). Such tools have all of the know-how to parse import URIs, and collect the namespace exported by a library at the resolved URI location. This means that an element can be located by the library in which it is declared, or by any library which exports the element.The import URI syntax is chosen for several simple reasons:
There is an interesting consequence of using the import URI syntax and the exact URI parsing mechanism: In order for the IDE to cross-link references with a package: schema, the indicated package must be "known" in the current package’s package configuration (e.g. pubspec). It is conceivable that cross-package links could be made in doc sites even if the linked package is not a known by the package’s configuration, but this behavior should not be expected or relied on.
Requiring a referenced package to be known in the referring package’s package configuration could be seen as detrimental. But in the common case (the "pub package" case), it is not expensive to add a referenced package to the dev_dependencies, and this solid link will allow static analysis tools to continue to report when a doc comment reference becomes "broken."
Using the export namespace of the library at the resolved URI location has the following consequences:
_Foo
or a field named_foo
) cannot be referred to, outside of the library in which it is declared. I consider this a feature, but if there is strong desire to be able to refer to such elements, we could change the mechanics from "a library’s export namespace" to something made up.ACCESSIBILITY
I am not aware of any accessibility considerations.
INTERNATIONALIZATION
I am not aware of any internationalization or localization considerations.
OPEN QUESTIONS
Is the hash mark a bad delimiter? I chose it because it looks like an HTML document fragment delimiter. I haven’t found any downsides to it, and haven’t seen a better delimiter to use.
TESTING PLAN
Support for this syntax will be tested in:
DOCUMENTATION PLAN
I am not aware of any documentation for how to write and format doc comments outside of Effective Dart. Effective Dart does have an entry encouraging using square brackets to reference in-scope identifiers, which will be updated. If there is a need for a guide, an article, or other documentation for doc comments, beyond what is provided in Effective Dart, that is out-of-scope for this design.
MIGRATION PLAN
If a developer has wanted to reference an out-of-scope element, there are a few strategies they may have ultimately chosen:
/// See `List`
. For this case, we can offer a lint and a quick fix, "this could be a doc comment reference." (This could have many false positives.)This addresses #44637.
The text was updated successfully, but these errors were encountered: