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

Commit 29be54e

Browse files
feat: Add the option ignored-patterns to the rule format-comment (#828)
* ✨ format-comment | ignored-patterns * ✅ Test format-comment | ignored-patterns * 📝 Update documentation * ✏️ Fix typos in add a better comment * ♻️ Create a config parser * 🎨 Move the class fields before the constructors * 📝 Update the documentation * ⚡ Cast as non nullable as we checked before Co-authored-by: Dmitry Krutskikh <dmitry.krutskikh@gmail.com>
1 parent 13fb583 commit 29be54e

File tree

7 files changed

+82
-7
lines changed

7 files changed

+82
-7
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+
* feat: add the `ignored-patterns` option to [`format-comment`](https://dartcodemetrics.dev/docs/rules/common/format-comment). The given regular expressions will be used to ignore comments that match them.
56
* fix: [`avoid-border-all`](https://dartcodemetrics.dev/docs/rules/flutter/avoid-border-all) is triggered even when it is not a const.
67
* fix: remove duplicated and ignore void function calls for [`prefer-moving-to-variable`](https://dartcodemetrics.dev/docs/rules/common/prefer-moving-to-variable).
78
* fix: temporary remove enums support for [`prefer-trailing-comma`](https://dartcodemetrics.dev/docs/rules/common/prefer-trailing-comma).
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
part of 'format_comment_rule.dart';
2+
3+
class _ConfigParser {
4+
static const _ignoredPatternsConfig = 'ignored-patterns';
5+
6+
static Iterable<RegExp> getIgnoredPatterns(Map<String, Object> config) =>
7+
config[_ignoredPatternsConfig] is Iterable
8+
? List<String>.from(
9+
config[_ignoredPatternsConfig] as Iterable,
10+
).map((stringPattern) => RegExp(stringPattern))
11+
: const [];
12+
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,31 @@ import '../../../models/severity.dart';
1212
import '../../models/common_rule.dart';
1313
import '../../rule_utils.dart';
1414

15+
part 'config_parser.dart';
1516
part 'models/comment_info.dart';
16-
1717
part 'models/comment_type.dart';
18-
1918
part 'visitor.dart';
2019

2120
class FormatCommentRule extends CommonRule {
2221
static const String ruleId = 'format-comment';
2322

2423
static const _warning = 'Prefer formatting comments like sentences.';
2524

25+
/// The patterns to ignore. They are used to ignore and not lint comments that
26+
/// match at least one of them.
27+
final Iterable<RegExp> _ignoredPatterns;
28+
2629
FormatCommentRule([Map<String, Object> config = const {}])
27-
: super(
30+
: _ignoredPatterns = _ConfigParser.getIgnoredPatterns(config),
31+
super(
2832
id: ruleId,
2933
severity: readSeverity(config, Severity.style),
3034
excludes: readExcludes(config),
3135
);
3236

3337
@override
3438
Iterable<Issue> check(InternalResolvedUnitResult source) {
35-
final visitor = _Visitor()..checkComments(source.unit.root);
39+
final visitor = _Visitor(_ignoredPatterns)..checkComments(source.unit.root);
3640

3741
return [
3842
for (final comment in visitor.comments)

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ const _ignoreExp = 'ignore:';
1111
const _ignoreForFileExp = 'ignore_for_file:';
1212

1313
class _Visitor extends RecursiveAstVisitor<void> {
14+
final Iterable<RegExp> _ignoredPatterns;
15+
16+
_Visitor(this._ignoredPatterns);
17+
1418
final _comments = <_CommentInfo>[];
1519

1620
Iterable<_CommentInfo> get comments => _comments;
@@ -54,8 +58,12 @@ class _Visitor extends RecursiveAstVisitor<void> {
5458

5559
final isMacros = _regMacrosExp.hasMatch(text) || text == _macrosEndExp;
5660

61+
final isAnIgnoredPattern = _ignoredPatterns.any(
62+
(regExp) => regExp.hasMatch(text),
63+
);
64+
5765
{
58-
if (text.isEmpty || isIgnoreComment || isMacros) {
66+
if (text.isEmpty || isIgnoreComment || isMacros || isAnIgnoredPattern) {
5967
return;
6068
} else {
6169
text = text.trim();

test/src/analyzers/lint_analyzer/rules/rules_list/format_comment/format_comment_test.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,39 @@ void main() {
8989
final unit = await RuleTestHelper.resolveFromFile(_withoutIssuePath);
9090
RuleTestHelper.verifyNoIssues(FormatCommentRule().check(unit));
9191
});
92+
93+
test('ignores the given patterns', () async {
94+
final unit = await RuleTestHelper.resolveFromFile(_examplePath);
95+
final issues = FormatCommentRule(const {
96+
'ignored-patterns': [
97+
// Ignores all the comments that start with 'Without'.
98+
r'^Without.*$',
99+
],
100+
}).check(unit);
101+
102+
RuleTestHelper.verifyIssues(
103+
issues: issues,
104+
startLines: [1, 5, 8, 10],
105+
startColumns: [1, 5, 3, 5],
106+
locationTexts: [
107+
'// With start space without dot',
108+
'// with start space with dot.',
109+
'/// With start space without dot',
110+
'/// with start space with dot.',
111+
],
112+
messages: [
113+
'Prefer formatting comments like sentences.',
114+
'Prefer formatting comments like sentences.',
115+
'Prefer formatting comments like sentences.',
116+
'Prefer formatting comments like sentences.',
117+
],
118+
replacements: [
119+
'// With start space without dot.',
120+
'// With start space with dot.',
121+
'/// With start space without dot.',
122+
'/// With start space with dot.',
123+
],
124+
);
125+
});
92126
});
93127
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Format comments
22

3+
![Configurable](https://img.shields.io/badge/-configurable-informational)
4+
35
## Rule id {#rule-id}
46

57
format-comment
@@ -12,6 +14,20 @@ Style
1214

1315
Prefer format comments like sentences.
1416

17+
Use `ignored-patterns` configuration, if you want to ignore comments that match the given regular expressions.
18+
19+
### Config example {#config-example}
20+
21+
```yaml
22+
dart_code_metrics:
23+
...
24+
rules:
25+
...
26+
- format-comment:
27+
ignored-patterns:
28+
- ^ cSpell.* # Ignores all the comments that start with 'cSpell' (for example: '// cSpell:disable-next-line').
29+
```
30+
1531
### Example {#example}
1632
1733
Bad:
@@ -50,4 +66,4 @@ class Test {
5066
}
5167
/* Prefer format comments
5268
like sentences. */
53-
```
69+
```

website/docs/rules/overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Rules configuration is [described here](../getting-started/configuration#configu
7979

8080
Checks that double literals should begin with `0.` instead of just `.`, and should not end with a trailing `0`.
8181

82-
- [format-comment](./common/format-comment.md) &nbsp; ![Has auto-fix](https://img.shields.io/badge/-has%20auto--fix-success)
82+
- [format-comment](./common/format-comment.md) &nbsp; [![Configurable](https://img.shields.io/badge/-configurable-informational)](./common/format-comment.md#config-example) ![Has auto-fix](https://img.shields.io/badge/-has%20auto--fix-success)
8383

8484
Prefer format comments like sentences.
8585

0 commit comments

Comments
 (0)