|
| 1 | +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 6 | +import 'package:analyzer/dart/ast/token.dart'; |
| 7 | +import 'package:analyzer/dart/ast/visitor.dart'; |
| 8 | + |
| 9 | +import '../analyzer.dart'; |
| 10 | + |
| 11 | +const _desc = r'Attach library doc comments to library directives.'; |
| 12 | + |
| 13 | +const _details = r''' |
| 14 | +Attach library doc comments (with `///`) to library directives, rather than |
| 15 | +leaving them dangling near the top of a library. |
| 16 | +
|
| 17 | +**BAD:** |
| 18 | +```dart |
| 19 | +/// This is a great library. |
| 20 | +import 'package:math'; |
| 21 | +``` |
| 22 | +
|
| 23 | +```dart |
| 24 | +/// This is a great library. |
| 25 | +
|
| 26 | +class C {} |
| 27 | +``` |
| 28 | +
|
| 29 | +**GOOD:** |
| 30 | +```dart |
| 31 | +/// This is a great library. |
| 32 | +library; |
| 33 | +
|
| 34 | +import 'package:math'; |
| 35 | +
|
| 36 | +class C {} |
| 37 | +``` |
| 38 | +
|
| 39 | +**NOTE:** An unnamed library, like `library;` above, is only supported in Dart |
| 40 | +2.19 and later. Code which might run in earlier versions of Dart will need to |
| 41 | +provide a name in the `library` directive. |
| 42 | +'''; |
| 43 | + |
| 44 | +class DanglingLibraryDocComments extends LintRule { |
| 45 | + static const LintCode code = LintCode( |
| 46 | + 'dangling_library_doc_comments', 'Dangling library doc comment.', |
| 47 | + correctionMessage: |
| 48 | + "Add a 'library' directive after the library comment."); |
| 49 | + |
| 50 | + DanglingLibraryDocComments() |
| 51 | + : super( |
| 52 | + name: 'dangling_library_doc_comments', |
| 53 | + description: _desc, |
| 54 | + details: _details, |
| 55 | + group: Group.style); |
| 56 | + |
| 57 | + @override |
| 58 | + LintCode get lintCode => code; |
| 59 | + |
| 60 | + @override |
| 61 | + void registerNodeProcessors( |
| 62 | + NodeLintRegistry registry, LinterContext context) { |
| 63 | + var visitor = _Visitor(this); |
| 64 | + registry.addCompilationUnit(this, visitor); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +class _Visitor extends SimpleAstVisitor<void> { |
| 69 | + final DanglingLibraryDocComments rule; |
| 70 | + |
| 71 | + _Visitor(this.rule); |
| 72 | + |
| 73 | + @override |
| 74 | + void visitCompilationUnit(CompilationUnit node) { |
| 75 | + if (node.directives.isNotEmpty) { |
| 76 | + // Only consider a doc comment on the first directive. Doc comments on |
| 77 | + // other directives do not have the appearance of documenting the library. |
| 78 | + var firstDirective = node.directives.first; |
| 79 | + if (firstDirective is LibraryDirective) { |
| 80 | + // Given the presense of library directive, don't worry about later doc |
| 81 | + // comments in the library. |
| 82 | + return; |
| 83 | + } |
| 84 | + if (firstDirective is PartOfDirective) { |
| 85 | + // Don't worry about possible "library doc comments" in a part. |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + var docComment = firstDirective.documentationComment; |
| 90 | + if (docComment != null) { |
| 91 | + rule.reportLintForToken(docComment.beginToken); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + if (node.declarations.isEmpty) { |
| 99 | + // Without any declarations, we only need to check for a doc comment as |
| 100 | + // the last thing in a file. |
| 101 | + Token? endComment = node.endToken.precedingComments; |
| 102 | + while (endComment is CommentToken) { |
| 103 | + if (endComment is DocumentationCommentToken) { |
| 104 | + rule.reportLintForToken(endComment); |
| 105 | + } |
| 106 | + endComment = endComment.next; |
| 107 | + } |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + var firstDeclaration = node.declarations.first; |
| 112 | + var docComment = firstDeclaration.documentationComment; |
| 113 | + if (docComment == null) { |
| 114 | + return; |
| 115 | + } |
| 116 | + var lineInfo = node.lineInfo; |
| 117 | + |
| 118 | + if (docComment.tokens.length > 1) { |
| 119 | + for (var i = 0; i < docComment.tokens.length - 1; i++) { |
| 120 | + var commentToken = docComment.tokens[i]; |
| 121 | + var followingCommentToken = docComment.tokens[i + 1]; |
| 122 | + var commentEndLine = lineInfo.getLocation(commentToken.end).lineNumber; |
| 123 | + var followingCommentLine = |
| 124 | + lineInfo.getLocation(followingCommentToken.offset).lineNumber; |
| 125 | + if (followingCommentLine > commentEndLine + 1) { |
| 126 | + // There is a blank line within the declaration's doc comments. |
| 127 | + rule.reportLintForToken(commentToken); |
| 128 | + return; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // We must walk through the comments following the doc comment, tracking |
| 134 | + // pairs of consecutive comments so as to check whether any two are |
| 135 | + // separated by a blank line. |
| 136 | + var commentToken = docComment.endToken; |
| 137 | + var followingCommentToken = commentToken.next; |
| 138 | + while (followingCommentToken != null) { |
| 139 | + // Any blank line between the doc comment and following comments makes |
| 140 | + // the doc comment look dangling. |
| 141 | + var commentEndLine = lineInfo.getLocation(commentToken.end).lineNumber; |
| 142 | + var followingCommentLine = |
| 143 | + lineInfo.getLocation(followingCommentToken.offset).lineNumber; |
| 144 | + if (followingCommentLine > commentEndLine + 1) { |
| 145 | + // There is a blank line between the declaration's doc comment and the |
| 146 | + // declaration. |
| 147 | + rule.reportLint(docComment); |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + commentToken = followingCommentToken; |
| 152 | + followingCommentToken = followingCommentToken.next; |
| 153 | + } |
| 154 | + |
| 155 | + var commentEndLine = lineInfo.getLocation(commentToken.end).lineNumber; |
| 156 | + // The syntactic entity to which a comment is "attached" is the |
| 157 | + // [Comment]'s `parent`, not it's `endToken`'s `next` [Token]. |
| 158 | + var tokenAfterDocComment = |
| 159 | + (docComment.endToken as DocumentationCommentToken).parent; |
| 160 | + if (tokenAfterDocComment == null) { |
| 161 | + // We shouldn't get here as the doc comment is attached to |
| 162 | + // [firstDeclaration]. |
| 163 | + return; |
| 164 | + } |
| 165 | + var declarationStartLine = |
| 166 | + lineInfo.getLocation(tokenAfterDocComment.offset).lineNumber; |
| 167 | + if (declarationStartLine > commentEndLine + 1) { |
| 168 | + // There is a blank line between the declaration's doc comment and the |
| 169 | + // declaration. |
| 170 | + rule.reportLint(docComment); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments