From a723c6ce5b3f7c4c92353fbcd0ed5ed72a6d399d Mon Sep 17 00:00:00 2001 From: ValentinVignal Date: Wed, 4 May 2022 22:33:19 +0800 Subject: [PATCH 1/8] :sparkles: format-comment | ignored-patterns --- .../rules_list/format_comment/format_comment_rule.dart | 10 ++++++++-- .../rules/rules_list/format_comment/visitor.dart | 9 ++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) 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..eabd091602 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 @@ -24,15 +24,21 @@ class FormatCommentRule extends CommonRule { static const _warning = 'Prefer formatting comments like sentences.'; FormatCommentRule([Map config = const {}]) - : super( + : _ignoredPatterns = List.from( + config['ignored-patterns'] as List? ?? const [], + ).map((stringPattern) => RegExp(stringPattern)), + super( id: ruleId, severity: readSeverity(config, Severity.style), excludes: readExcludes(config), ); + /// The patterns to ignore. The patterns are used as [RegExp]s. + final Iterable _ignoredPatterns; + @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..e449a7fff8 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,9 @@ const _ignoreExp = 'ignore:'; const _ignoreForFileExp = 'ignore_for_file:'; class _Visitor extends RecursiveAstVisitor { + _Visitor(this._ignoredPatterns); + + final Iterable _ignoredPatterns; final _comments = <_CommentInfo>[]; Iterable<_CommentInfo> get comments => _comments; @@ -54,8 +57,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(); From 6083d3d1f89cc1ed27932181c83f18800597f160 Mon Sep 17 00:00:00 2001 From: ValentinVignal Date: Wed, 4 May 2022 22:33:44 +0800 Subject: [PATCH 2/8] :white_check_mark: Test format-comment | ignored-patterns --- .../format_comment/format_comment_test.dart | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) 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.', + ], + ); + }); }); } From 3ecf0c51a7f0a5c71e72d4b851c307d976007c2f Mon Sep 17 00:00:00 2001 From: ValentinVignal Date: Wed, 4 May 2022 22:41:22 +0800 Subject: [PATCH 3/8] :memo: Update documentation --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cfe91a418..ce51f12330 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +* feat: add `ignored-patterns` option to [`format-comment`](https://dartcodemetrics.dev/docs/rules/common/format-comment). The given regular expression will be used to ignore comments that match them. + ## 4.15.0 * fix: [`format-comment`](https://dartcodemetrics.dev/docs/rules/common/format-comment) is listing the macros from dart doc. From 651c2e4674ef93882756cadb1c3097ce8bf8c21e Mon Sep 17 00:00:00 2001 From: ValentinVignal Date: Wed, 4 May 2022 22:46:24 +0800 Subject: [PATCH 4/8] :pencil2: Fix typos in add a better comment --- CHANGELOG.md | 2 +- .../rules/rules_list/format_comment/format_comment_rule.dart | 3 ++- .../lint_analyzer/rules/rules_list/format_comment/visitor.dart | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce51f12330..eddd6bfad1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Unreleased -* feat: add `ignored-patterns` option to [`format-comment`](https://dartcodemetrics.dev/docs/rules/common/format-comment). The given regular expression will be used to ignore comments that match them. +* 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. ## 4.15.0 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 eabd091602..9cbcd63957 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 @@ -33,7 +33,8 @@ class FormatCommentRule extends CommonRule { excludes: readExcludes(config), ); - /// The patterns to ignore. The patterns are used as [RegExp]s. + /// The patterns to ignore. They are used to ignore and not lint comments that + /// match at least one of them. final Iterable _ignoredPatterns; @override 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 e449a7fff8..63c6bcd656 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 @@ -14,6 +14,7 @@ class _Visitor extends RecursiveAstVisitor { _Visitor(this._ignoredPatterns); final Iterable _ignoredPatterns; + final _comments = <_CommentInfo>[]; Iterable<_CommentInfo> get comments => _comments; From 06b475e10834de19ad9fed37bbafc8ee677e0d31 Mon Sep 17 00:00:00 2001 From: ValentinVignal Date: Thu, 5 May 2022 11:58:24 +0800 Subject: [PATCH 5/8] :recycle: Create a config parser --- .../rules_list/format_comment/config_parser.dart | 12 ++++++++++++ .../format_comment/format_comment_rule.dart | 7 ++----- 2 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 lib/src/analyzers/lint_analyzer/rules/rules_list/format_comment/config_parser.dart 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..5c5a8a02bb --- /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? ?? const [], + ).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 9cbcd63957..1f9a6713e1 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 { @@ -24,9 +23,7 @@ class FormatCommentRule extends CommonRule { static const _warning = 'Prefer formatting comments like sentences.'; FormatCommentRule([Map config = const {}]) - : _ignoredPatterns = List.from( - config['ignored-patterns'] as List? ?? const [], - ).map((stringPattern) => RegExp(stringPattern)), + : _ignoredPatterns = _ConfigParser.getIgnoredPatterns(config), super( id: ruleId, severity: readSeverity(config, Severity.style), From 5ef888526b251c5dbacaf2e7c2689ab933ed06ad Mon Sep 17 00:00:00 2001 From: ValentinVignal Date: Thu, 5 May 2022 11:58:47 +0800 Subject: [PATCH 6/8] :art: Move the class fields before the constructors --- .../rules_list/format_comment/format_comment_rule.dart | 8 ++++---- .../rules/rules_list/format_comment/visitor.dart | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) 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 1f9a6713e1..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 @@ -22,6 +22,10 @@ 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 {}]) : _ignoredPatterns = _ConfigParser.getIgnoredPatterns(config), super( @@ -30,10 +34,6 @@ class FormatCommentRule extends CommonRule { excludes: readExcludes(config), ); - /// The patterns to ignore. They are used to ignore and not lint comments that - /// match at least one of them. - final Iterable _ignoredPatterns; - @override Iterable check(InternalResolvedUnitResult source) { final visitor = _Visitor(_ignoredPatterns)..checkComments(source.unit.root); 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 63c6bcd656..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,10 +11,10 @@ const _ignoreExp = 'ignore:'; const _ignoreForFileExp = 'ignore_for_file:'; class _Visitor extends RecursiveAstVisitor { - _Visitor(this._ignoredPatterns); - final Iterable _ignoredPatterns; + _Visitor(this._ignoredPatterns); + final _comments = <_CommentInfo>[]; Iterable<_CommentInfo> get comments => _comments; From 6288e6f806130d13ecb4a67685a68742185b8aeb Mon Sep 17 00:00:00 2001 From: ValentinVignal Date: Thu, 5 May 2022 11:59:01 +0800 Subject: [PATCH 7/8] :memo: Update the documentation --- website/docs/rules/common/format-comment.md | 18 +++++++++++++++++- website/docs/rules/overview.md | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) 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. From 7624995be5dbd318f4260f5c3dbface0f8409421 Mon Sep 17 00:00:00 2001 From: ValentinVignal Date: Thu, 5 May 2022 12:00:48 +0800 Subject: [PATCH 8/8] :zap: Cast as non nullable as we checked before --- .../rules/rules_list/format_comment/config_parser.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 5c5a8a02bb..b4dee488f0 100644 --- 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 @@ -6,7 +6,7 @@ class _ConfigParser { static Iterable getIgnoredPatterns(Map config) => config[_ignoredPatternsConfig] is Iterable ? List.from( - config[_ignoredPatternsConfig] as Iterable? ?? const [], + config[_ignoredPatternsConfig] as Iterable, ).map((stringPattern) => RegExp(stringPattern)) : const []; }