Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit 07a2bfd

Browse files
authored
fix: format-comment is listing the macros from dart doc. (#823)
* fix: `format-comment` is listing the macros from dart doc. * fix: review changes * fix: review changes
1 parent ded9d0e commit 07a2bfd

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
* fix: [`format-comment`](https://dartcodemetrics.dev/docs/rules/common/format-comment) is listing the macros from dart doc.
56
* feat: add static code diagnostic [`avoid-non-ascii-symbols`](https://dartcodemetrics.dev/docs/rules/common/avoid-non-ascii-symbols).
67
* feat: remove declaration in [`prefer-immediate-return`](https://dartcodemetrics.dev/docs/rules/common/prefer-immediate-return).
78
* fix: correctly handle disabling rules with false.

lib/src/analyzers/lint_analyzer/rules/rules_list/format_comment/visitor.dart

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ const commentsOperator = {
55
_CommentType.documentation: '///',
66
};
77

8+
final _regMacrosExp = RegExp('{@(template|macro) .+}');
9+
const _macrosEndExp = '{@endtemplate}';
10+
const _ignoreExp = 'ignore:';
11+
const _ignoreForFileExp = 'ignore_for_file:';
12+
813
class _Visitor extends RecursiveAstVisitor<void> {
914
final _comments = <_CommentInfo>[];
1015

@@ -44,20 +49,26 @@ class _Visitor extends RecursiveAstVisitor<void> {
4449

4550
var text = commentText.trim();
4651

47-
if (text.isEmpty ||
48-
text.startsWith('ignore:') ||
49-
text.startsWith('ignore_for_file:')) {
50-
return;
51-
} else {
52-
text = text.trim();
53-
final upperCase = text[0] == text[0].toUpperCase();
54-
final lastSymbol = _punctuation.contains(text[text.length - 1]);
55-
final hasEmptySpace = commentText[0] == ' ';
56-
final incorrectFormat = !upperCase || !hasEmptySpace || !lastSymbol;
57-
final single = commentToken.previous == null && commentToken.next == null;
52+
final isIgnoreComment =
53+
text.startsWith(_ignoreExp) || text.startsWith(_ignoreForFileExp);
54+
55+
final isMacros = _regMacrosExp.hasMatch(text) || text == _macrosEndExp;
56+
57+
{
58+
if (text.isEmpty || isIgnoreComment || isMacros) {
59+
return;
60+
} else {
61+
text = text.trim();
62+
final upperCase = text[0] == text[0].toUpperCase();
63+
final lastSymbol = _punctuation.contains(text[text.length - 1]);
64+
final hasEmptySpace = commentText[0] == ' ';
65+
final incorrectFormat = !upperCase || !hasEmptySpace || !lastSymbol;
66+
final single =
67+
commentToken.previous == null && commentToken.next == null;
5868

59-
if (incorrectFormat && single) {
60-
_comments.add(_CommentInfo(type, commentToken));
69+
if (incorrectFormat && single) {
70+
_comments.add(_CommentInfo(type, commentToken));
71+
}
6172
}
6273
}
6374
}

test/src/analyzers/lint_analyzer/rules/rules_list/format_comment/examples/example_documentation.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,15 @@ void greet(String name) {
1616

1717
/// deletes the file at [path] from the file system.
1818
void delete(String path) {}
19+
20+
/// {@template template_name}
21+
void f1() {}
22+
23+
/// {@endtemplate}
24+
void f2() {}
25+
26+
/// {@macro template_name}
27+
void f3() {}
28+
29+
/// {@template my_project.my_class.my_method}
30+
void f4() {}

0 commit comments

Comments
 (0)