diff --git a/CHANGELOG.md b/CHANGELOG.md index 85f9ac6c87..b3424feb48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* 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. * fix: [`avoid-border-all`](https://dartcodemetrics.dev/docs/rules/flutter/avoid-border-all) is triggered even when it is not a const. * fix: remove duplicated and ignore void function calls for [`prefer-moving-to-variable`](https://dartcodemetrics.dev/docs/rules/common/prefer-moving-to-variable). diff --git a/lib/src/analyzers/lint_analyzer/rules/rules_list/format_comment/config_parser.dart b/lib/src/analyzers/lint_analyzer/rules/rules_list/format_comment/config_parser.dart new file mode 100644 index 0000000000..b4dee488f0 --- /dev/null +++ b/lib/src/analyzers/lint_analyzer/rules/rules_list/format_comment/config_parser.dart @@ -0,0 +1,12 @@ +part of 'format_comment_rule.dart'; + +class _ConfigParser { + static const _ignoredPatternsConfig = 'ignored-patterns'; + + static Iterable getIgnoredPatterns(Map config) => + config[_ignoredPatternsConfig] is Iterable + ? List.from( + config[_ignoredPatternsConfig] as Iterable, + ).map((stringPattern) => RegExp(stringPattern)) + : const []; +} diff --git a/lib/src/analyzers/lint_analyzer/rules/rules_list/format_comment/format_comment_rule.dart b/lib/src/analyzers/lint_analyzer/rules/rules_list/format_comment/format_comment_rule.dart index b3c2cf6ec4..cc5250190b 100644 --- a/lib/src/analyzers/lint_analyzer/rules/rules_list/format_comment/format_comment_rule.dart +++ b/lib/src/analyzers/lint_analyzer/rules/rules_list/format_comment/format_comment_rule.dart @@ -12,10 +12,9 @@ import '../../../models/severity.dart'; import '../../models/common_rule.dart'; import '../../rule_utils.dart'; +part 'config_parser.dart'; part 'models/comment_info.dart'; - part 'models/comment_type.dart'; - part 'visitor.dart'; class FormatCommentRule extends CommonRule { @@ -23,8 +22,13 @@ class FormatCommentRule extends CommonRule { static const _warning = 'Prefer formatting comments like sentences.'; + /// The patterns to ignore. They are used to ignore and not lint comments that + /// match at least one of them. + final Iterable _ignoredPatterns; + FormatCommentRule([Map config = const {}]) - : super( + : _ignoredPatterns = _ConfigParser.getIgnoredPatterns(config), + super( id: ruleId, severity: readSeverity(config, Severity.style), excludes: readExcludes(config), @@ -32,7 +36,7 @@ class FormatCommentRule extends CommonRule { @override Iterable check(InternalResolvedUnitResult source) { - final visitor = _Visitor()..checkComments(source.unit.root); + final visitor = _Visitor(_ignoredPatterns)..checkComments(source.unit.root); return [ for (final comment in visitor.comments) diff --git a/lib/src/analyzers/lint_analyzer/rules/rules_list/format_comment/visitor.dart b/lib/src/analyzers/lint_analyzer/rules/rules_list/format_comment/visitor.dart index 3c83a2db08..7f2a78e261 100644 --- a/lib/src/analyzers/lint_analyzer/rules/rules_list/format_comment/visitor.dart +++ b/lib/src/analyzers/lint_analyzer/rules/rules_list/format_comment/visitor.dart @@ -11,6 +11,10 @@ const _ignoreExp = 'ignore:'; const _ignoreForFileExp = 'ignore_for_file:'; class _Visitor extends RecursiveAstVisitor { + final Iterable _ignoredPatterns; + + _Visitor(this._ignoredPatterns); + final _comments = <_CommentInfo>[]; Iterable<_CommentInfo> get comments => _comments; @@ -54,8 +58,12 @@ class _Visitor extends RecursiveAstVisitor { final isMacros = _regMacrosExp.hasMatch(text) || text == _macrosEndExp; + final isAnIgnoredPattern = _ignoredPatterns.any( + (regExp) => regExp.hasMatch(text), + ); + { - if (text.isEmpty || isIgnoreComment || isMacros) { + if (text.isEmpty || isIgnoreComment || isMacros || isAnIgnoredPattern) { return; } else { text = text.trim(); diff --git a/test/src/analyzers/lint_analyzer/rules/rules_list/format_comment/format_comment_test.dart b/test/src/analyzers/lint_analyzer/rules/rules_list/format_comment/format_comment_test.dart index 2ab4aed653..d36827c396 100644 --- a/test/src/analyzers/lint_analyzer/rules/rules_list/format_comment/format_comment_test.dart +++ b/test/src/analyzers/lint_analyzer/rules/rules_list/format_comment/format_comment_test.dart @@ -89,5 +89,39 @@ void main() { final unit = await RuleTestHelper.resolveFromFile(_withoutIssuePath); RuleTestHelper.verifyNoIssues(FormatCommentRule().check(unit)); }); + + test('ignores the given patterns', () async { + final unit = await RuleTestHelper.resolveFromFile(_examplePath); + final issues = FormatCommentRule(const { + 'ignored-patterns': [ + // Ignores all the comments that start with 'Without'. + r'^Without.*$', + ], + }).check(unit); + + RuleTestHelper.verifyIssues( + issues: issues, + startLines: [1, 5, 8, 10], + startColumns: [1, 5, 3, 5], + locationTexts: [ + '// With start space without dot', + '// with start space with dot.', + '/// With start space without dot', + '/// with start space with dot.', + ], + messages: [ + 'Prefer formatting comments like sentences.', + 'Prefer formatting comments like sentences.', + 'Prefer formatting comments like sentences.', + 'Prefer formatting comments like sentences.', + ], + replacements: [ + '// With start space without dot.', + '// With start space with dot.', + '/// With start space without dot.', + '/// With start space with dot.', + ], + ); + }); }); } diff --git a/website/docs/rules/common/format-comment.md b/website/docs/rules/common/format-comment.md index 0cd6c174cf..0c01a2ebc1 100644 --- a/website/docs/rules/common/format-comment.md +++ b/website/docs/rules/common/format-comment.md @@ -1,5 +1,7 @@ # Format comments +![Configurable](https://img.shields.io/badge/-configurable-informational) + ## Rule id {#rule-id} format-comment @@ -12,6 +14,20 @@ Style Prefer format comments like sentences. +Use `ignored-patterns` configuration, if you want to ignore comments that match the given regular expressions. + +### Config example {#config-example} + +```yaml +dart_code_metrics: + ... + rules: + ... + - format-comment: + ignored-patterns: + - ^ cSpell.* # Ignores all the comments that start with 'cSpell' (for example: '// cSpell:disable-next-line'). +``` + ### Example {#example} Bad: @@ -50,4 +66,4 @@ class Test { } /* Prefer format comments like sentences. */ -``` \ No newline at end of file +``` diff --git a/website/docs/rules/overview.md b/website/docs/rules/overview.md index 35580e8897..a1aac31206 100644 --- a/website/docs/rules/overview.md +++ b/website/docs/rules/overview.md @@ -79,7 +79,7 @@ Rules configuration is [described here](../getting-started/configuration#configu Checks that double literals should begin with `0.` instead of just `.`, and should not end with a trailing `0`. -- [format-comment](./common/format-comment.md)   ![Has auto-fix](https://img.shields.io/badge/-has%20auto--fix-success) +- [format-comment](./common/format-comment.md)   [![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) Prefer format comments like sentences.