Skip to content

Commit

Permalink
[linter] Update missing_code_block_language_in_doc_comment to avoid l…
Browse files Browse the repository at this point in the history
…inting 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]>
  • Loading branch information
kallentu authored and Commit Queue committed Mar 28, 2024
1 parent b6137f7 commit b6ac281
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/doc_comment.dart';
import 'package:analyzer/dart/ast/visitor.dart';

import '../analyzer.dart';
Expand Down Expand Up @@ -69,13 +70,14 @@ class _Visitor extends SimpleAstVisitor<void> {
@override
void visitComment(Comment node) {
for (var codeBlock in node.codeBlocks) {
if (codeBlock.infoString == null) {
var openingCodeBlockFence = codeBlock.lines.first;
rule.reportLintForOffset(
openingCodeBlockFence.offset,
openingCodeBlockFence.length,
);
}
if (codeBlock.infoString != null) continue;
if (codeBlock.type != CodeBlockType.fenced) continue;

var openingCodeBlockFence = codeBlock.lines.first;
rule.reportLintForOffset(
openingCodeBlockFence.offset,
openingCodeBlockFence.length,
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MissingCodeBlockLanguageInDocCommentTest extends LintRuleTest {
/// ```dart
/// test
/// ```
class A {}
''');
}

Expand All @@ -43,6 +44,17 @@ class A {}
''');
}

test_indentedCodeBlock() async {
await assertNoDiagnostics(r'''
/// Example:
///
/// var printer = Printer();
/// printer.printToStdout();
///
class A {}
''');
}

test_missingLanguage() async {
await assertDiagnostics(r'''
/// ```
Expand Down

0 comments on commit b6ac281

Please sign in to comment.