Skip to content

Commit b6ac281

Browse files
kallentuCommit Queue
authored andcommitted
[linter] Update missing_code_block_language_in_doc_comment to avoid linting indented code blocks.
After trying to add the lint to flutter (flutter/flutter#145354), I noticed that we lint on indented code blocks, which we should not be doing. This CL uses the new code block type in https://dart-review.googlesource.com/c/sdk/+/358326 to make sure we only lint on fenced code blocks. Change-Id: Ic13596dd3c95ce4ff64deeffe02da0fdb7a298e0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/359981 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent b6137f7 commit b6ac281

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

pkg/linter/lib/src/rules/missing_code_block_language_in_doc_comment.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/ast/ast.dart';
6+
import 'package:analyzer/dart/ast/doc_comment.dart';
67
import 'package:analyzer/dart/ast/visitor.dart';
78

89
import '../analyzer.dart';
@@ -69,13 +70,14 @@ class _Visitor extends SimpleAstVisitor<void> {
6970
@override
7071
void visitComment(Comment node) {
7172
for (var codeBlock in node.codeBlocks) {
72-
if (codeBlock.infoString == null) {
73-
var openingCodeBlockFence = codeBlock.lines.first;
74-
rule.reportLintForOffset(
75-
openingCodeBlockFence.offset,
76-
openingCodeBlockFence.length,
77-
);
78-
}
73+
if (codeBlock.infoString != null) continue;
74+
if (codeBlock.type != CodeBlockType.fenced) continue;
75+
76+
var openingCodeBlockFence = codeBlock.lines.first;
77+
rule.reportLintForOffset(
78+
openingCodeBlockFence.offset,
79+
openingCodeBlockFence.length,
80+
);
7981
}
8082
}
8183
}

pkg/linter/test/rules/missing_code_block_language_in_doc_comment_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class MissingCodeBlockLanguageInDocCommentTest extends LintRuleTest {
2222
/// ```dart
2323
/// test
2424
/// ```
25+
class A {}
2526
''');
2627
}
2728

@@ -43,6 +44,17 @@ class A {}
4344
''');
4445
}
4546

47+
test_indentedCodeBlock() async {
48+
await assertNoDiagnostics(r'''
49+
/// Example:
50+
///
51+
/// var printer = Printer();
52+
/// printer.printToStdout();
53+
///
54+
class A {}
55+
''');
56+
}
57+
4658
test_missingLanguage() async {
4759
await assertDiagnostics(r'''
4860
/// ```

0 commit comments

Comments
 (0)