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

Commit 8ec23cb

Browse files
authored
feat: support ignoring regular comments for format-comment (#1110)
1 parent b8f190c commit 8ec23cb

File tree

6 files changed

+55
-7
lines changed

6 files changed

+55
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* feat: support ignoring regular comments for [`format-comment`](https://dartcodemetrics.dev/docs/rules/common/format-comment).
6+
37
## 5.2.1
48

59
* fix: avoid null check exception in the analyzer.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ part of 'format_comment_rule.dart';
22

33
class _ConfigParser {
44
static const _ignoredPatternsConfig = 'ignored-patterns';
5+
static const _onlyDocComments = 'only-doc-comments';
56

6-
static Iterable<RegExp> getIgnoredPatterns(Map<String, Object> config) =>
7+
static Iterable<RegExp> parseIgnoredPatterns(Map<String, Object> config) =>
78
config[_ignoredPatternsConfig] is Iterable
89
? List<String>.from(
910
config[_ignoredPatternsConfig] as Iterable,
1011
).map(RegExp.new)
1112
: const [];
13+
14+
static bool parseOnlyDocComments(Map<String, Object> config) =>
15+
config[_onlyDocComments] == true;
1216
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ class FormatCommentRule extends CommonRule {
2626
/// match at least one of them.
2727
final Iterable<RegExp> _ignoredPatterns;
2828

29+
final bool _onlyDocComments;
30+
2931
FormatCommentRule([Map<String, Object> config = const {}])
30-
: _ignoredPatterns = _ConfigParser.getIgnoredPatterns(config),
32+
: _ignoredPatterns = _ConfigParser.parseIgnoredPatterns(config),
33+
_onlyDocComments = _ConfigParser.parseOnlyDocComments(config),
3134
super(
3235
id: ruleId,
3336
severity: readSeverity(config, Severity.style),
@@ -40,13 +43,17 @@ class FormatCommentRule extends CommonRule {
4043
final json = super.toJson();
4144
json[_ConfigParser._ignoredPatternsConfig] =
4245
_ignoredPatterns.map((exp) => exp.pattern).toList();
46+
json[_ConfigParser._onlyDocComments] = _onlyDocComments;
4347

4448
return json;
4549
}
4650

4751
@override
4852
Iterable<Issue> check(InternalResolvedUnitResult source) {
49-
final visitor = _Visitor(_ignoredPatterns)..checkComments(source.unit.root);
53+
final visitor = _Visitor(
54+
_ignoredPatterns,
55+
_onlyDocComments,
56+
)..checkComments(source.unit.root);
5057

5158
return [
5259
for (final comment in visitor.comments)

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
part of 'format_comment_rule.dart';
22

3-
const commentsOperator = {
3+
const _commentsOperator = {
44
_CommentType.base: '//',
55
_CommentType.documentation: '///',
66
};
@@ -12,8 +12,10 @@ const _ignoreForFileExp = 'ignore_for_file:';
1212

1313
class _Visitor extends RecursiveAstVisitor<void> {
1414
final Iterable<RegExp> _ignoredPatterns;
15+
final bool _onlyDocComments;
1516

16-
_Visitor(this._ignoredPatterns);
17+
// ignore: avoid_positional_boolean_parameters
18+
_Visitor(this._ignoredPatterns, this._onlyDocComments);
1719

1820
final _comments = <_CommentInfo>[];
1921

@@ -41,15 +43,15 @@ class _Visitor extends RecursiveAstVisitor<void> {
4143
final token = commentToken.toString();
4244
if (token.startsWith('///')) {
4345
_checkCommentByType(commentToken, _CommentType.documentation);
44-
} else if (token.startsWith('//')) {
46+
} else if (token.startsWith('//') && !_onlyDocComments) {
4547
_checkCommentByType(commentToken, _CommentType.base);
4648
}
4749
}
4850
}
4951

5052
void _checkCommentByType(Token commentToken, _CommentType type) {
5153
final commentText =
52-
commentToken.toString().substring(commentsOperator[type]!.length);
54+
commentToken.toString().substring(_commentsOperator[type]!.length);
5355

5456
var text = commentText.trim();
5557

test/src/analyzers/lint_analyzer/rules/rules_list/format_comment/format_comment_test.dart renamed to test/src/analyzers/lint_analyzer/rules/rules_list/format_comment/format_comment_rule_test.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,34 @@ void main() {
8585
);
8686
});
8787

88+
test('reports about found issues only for doc comments', () async {
89+
final unit = await RuleTestHelper.resolveFromFile(_multiline);
90+
final issues = FormatCommentRule(const {
91+
'only-doc-comments': true,
92+
}).check(unit);
93+
94+
RuleTestHelper.verifyIssues(
95+
issues: issues,
96+
startLines: [2, 5, 17],
97+
startColumns: [3, 3, 1],
98+
locationTexts: [
99+
'/// The value this wraps',
100+
'/// true if this box contains a value.',
101+
'/// deletes the file at [path] from the file system.',
102+
],
103+
messages: [
104+
'Prefer formatting comments like sentences.',
105+
'Prefer formatting comments like sentences.',
106+
'Prefer formatting comments like sentences.',
107+
],
108+
replacements: [
109+
'/// The value this wraps.',
110+
'/// True if this box contains a value.',
111+
'/// Deletes the file at [path] from the file system.',
112+
],
113+
);
114+
});
115+
88116
test('reports no issues', () async {
89117
final unit = await RuleTestHelper.resolveFromFile(_withoutIssuePath);
90118
RuleTestHelper.verifyNoIssues(FormatCommentRule().check(unit));

website/docs/rules/common/format-comment.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Prefer format comments like sentences.
66

77
Use `ignored-patterns` configuration, if you want to ignore comments that match the given regular expressions.
88

9+
Use `only-doc-comments` configuration, if you want to check only documentation comments (`///`).
10+
911
### ⚙️ Config example {#config-example}
1012

1113
```yaml
@@ -14,6 +16,7 @@ dart_code_metrics:
1416
rules:
1517
...
1618
- format-comment:
19+
only-doc-comments: true
1720
ignored-patterns:
1821
- ^ cSpell.* # Ignores all the comments that start with 'cSpell' (for example: '// cSpell:disable-next-line').
1922
```

0 commit comments

Comments
 (0)