Skip to content

Commit f7bd4c4

Browse files
Fix firstNonCommentToken in anticipation of analyzer fix. (#1503)
This commit fixes a subtle bug in the formatter which is currently covered up by an analyzer bug. The formatter bug is this: `AstNodeExtensions.firstNonCommentToken` assumes that if an AST node doesn't implement `AnnotatedNode`, then it can't possibly begin with a documentation comment, so it is safe to simply defer to `beginToken`. But that isn't true `DefaultFormalParameter` doesn't implement `AnnotatedNode`, but its first child is a `NormalFormalParameter`, which *can* begin with a documentation comment. Currently, analyzer bug dart-lang/sdk#56313 prevents `NormalFormalParameter.beginToken` from properly accounting for the presence of the documentation comment, so as a result, `AstNodeExtensions.firstNonCommentToken` works correctly on `DefaultFormalParameter`. In order to avoid breaking the formatter when I fix the analyzer bug, I need to first fix the formatter. The formatter fix is for `AstNodeExtensions.firstNonCommentToken` to recognize that `DefaultFormalParameter` is unusual, and to compute its first non-comment token directly (by looking at the first non-cmoment token of its first child, `parameter`). The formatter fix works regardless of whether the analyzer bug is fixed, so it is safe to land the formatter fix first.
1 parent 2112257 commit f7bd4c4

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

lib/src/ast_extensions.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ extension AstNodeExtensions on AstNode {
1818
return switch (this) {
1919
AnnotatedNode(metadata: [var annotation, ...]) => annotation.beginToken,
2020
AnnotatedNode(firstTokenAfterCommentAndMetadata: var token) => token,
21+
// DefaultFormalParameter is not an AnnotatedNode, but its first child
22+
// (parameter) *is* an AnnotatedNode, so we can't just use beginToken.
23+
DefaultFormalParameter(:var parameter) => parameter.firstNonCommentToken,
2124
_ => beginToken
2225
};
2326
}

0 commit comments

Comments
 (0)