comment_references
Only reference in-scope identifiers in doc comments.
This rule is available as of Dart 2.0.
This rule has a quick fix available.
Details
#DO reference only in-scope identifiers in doc comments.
If you surround identifiers like variable, method, or type names in square brackets, then tools like your IDE and dart doc
can link to them. For this to work, ensure that all identifiers in docs wrapped in brackets are in scope.
For example, assuming outOfScopeId
is out of scope:
BAD:
/// Returns whether [value] is larger than [outOfScopeId].
bool isOutOfRange(int value) { ... }
GOOD:
/// Returns the larger of [a] or [b].
int max_int(int a, int b) { ... }
Note that the square bracket comment format is designed to allow comments to refer to declarations using a fairly natural format but does not allow arbitrary expressions. In particular, code references within square brackets can consist of any of the following:
- A bare identifier which is in-scope for the comment (see the spec for what is "in-scope" in doc comments). Examples include
[print]
and[Future]
. - Two identifiers separated by a period (a "prefixed identifier"), such that the first identifier acts as a namespacing identifier, such as a class property name or method name prefixed by the containing class's name, or a top-level identifier prefixed by an import prefix. Examples include
[Future.new]
(an unnamed constructor),[Future.value]
(a constructor),[Future.wait]
(a static method),[Future.then]
(an instance method),[math.max]
(given that 'dart:async' is imported with amax
prefix). - A prefixed identifier followed by a pair of parentheses, used to disambiguate named constructors from instance members (whose names are allowed to collide). Examples include
[Future.value()]
. - Three identifiers separated by two periods, such that the first identifier is an import prefix name, the second identifier is a top-level element like a class or an extension, and the third identifier is a member of that top-level element. Examples include
[async.Future.then]
(given that 'dart:async' is imported with anasync
prefix).
Known limitations
The comment_references
lint rule aligns with the Dart analyzer's notion of comment references, which is occasionally distinct from Dartdoc's notion of comment references. The lint rule may report comment references which Dartdoc can resolve, even though the analyzer cannot. See linter#1142 for more information.
Usage
#To enable the comment_references
rule, add comment_references
under linter > rules in your analysis_options.yaml
file:
linter:
rules:
- comment_references
Unless stated otherwise, the documentation on this site reflects Dart 3.5.4. Page last updated on 2024-07-03. View source or report an issue.