From ad73a6607f82b4f726091f5aca1f263e5e8d97a6 Mon Sep 17 00:00:00 2001 From: vlkonoshenko Date: Sat, 30 Apr 2022 14:23:05 +0300 Subject: [PATCH 1/3] fix: `format-comment` is listing the macros from dart doc. --- CHANGELOG.md | 1 + .../rules_list/format_comment/visitor.dart | 37 ++++++++++++------- .../examples/example_documentation.dart | 9 +++++ 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8bac6246f..4b4768531c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +* fix: [`format-comment`](https://dartcodemetrics.dev/docs/rules/common/format-comment) is listing the macros from dart doc. * fix: dart-code-metrics crash saying `Bad state: No element` when running command. ## 4.14.0 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 69485f21ce..457b390a0c 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 @@ -44,20 +44,31 @@ class _Visitor extends RecursiveAstVisitor { var text = commentText.trim(); - if (text.isEmpty || - text.startsWith('ignore:') || - text.startsWith('ignore_for_file:')) { - return; - } else { - text = text.trim(); - final upperCase = text[0] == text[0].toUpperCase(); - final lastSymbol = _punctuation.contains(text[text.length - 1]); - final hasEmptySpace = commentText[0] == ' '; - final incorrectFormat = !upperCase || !hasEmptySpace || !lastSymbol; - final single = commentToken.previous == null && commentToken.next == null; + final isIgnoreComment = + text.startsWith('ignore:') || text.startsWith('ignore_for_file:'); - if (incorrectFormat && single) { - _comments.add(_CommentInfo(type, commentToken)); + final regTemplateExp = RegExp(r"{@template [\w'-]+}"); + final regMacroExp = RegExp(r"{@macro [\w'-]+}"); + + final isMacros = regTemplateExp.hasMatch(text) || + regMacroExp.hasMatch(text) || + text == '{@endtemplate}'; + + { + if (text.isEmpty || isIgnoreComment || isMacros) { + return; + } else { + text = text.trim(); + final upperCase = text[0] == text[0].toUpperCase(); + final lastSymbol = _punctuation.contains(text[text.length - 1]); + final hasEmptySpace = commentText[0] == ' '; + final incorrectFormat = !upperCase || !hasEmptySpace || !lastSymbol; + final single = + commentToken.previous == null && commentToken.next == null; + + if (incorrectFormat && single) { + _comments.add(_CommentInfo(type, commentToken)); + } } } } diff --git a/test/src/analyzers/lint_analyzer/rules/rules_list/format_comment/examples/example_documentation.dart b/test/src/analyzers/lint_analyzer/rules/rules_list/format_comment/examples/example_documentation.dart index d266b454c9..0b3917f10e 100644 --- a/test/src/analyzers/lint_analyzer/rules/rules_list/format_comment/examples/example_documentation.dart +++ b/test/src/analyzers/lint_analyzer/rules/rules_list/format_comment/examples/example_documentation.dart @@ -16,3 +16,12 @@ void greet(String name) { /// deletes the file at [path] from the file system. void delete(String path) {} + +/// {@template template_name} +void f1() {} + +/// {@endtemplate} +void f2() {} + +/// {@macro template_name} +void f3() {} From 8a63ba7dd34381f5b85c22a68c5a8786f8fe4c3d Mon Sep 17 00:00:00 2001 From: vlkonoshenko Date: Sun, 1 May 2022 07:37:22 +0300 Subject: [PATCH 2/3] fix: review changes --- .../rules/rules_list/format_comment/visitor.dart | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 457b390a0c..efc1b94b41 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 @@ -5,6 +5,9 @@ const commentsOperator = { _CommentType.documentation: '///', }; +final _regTemplateExp = RegExp(r"{@template [\w'-]+}"); +final _regMacroExp = RegExp(r"{@macro [\w'-]+}"); + class _Visitor extends RecursiveAstVisitor { final _comments = <_CommentInfo>[]; @@ -47,11 +50,8 @@ class _Visitor extends RecursiveAstVisitor { final isIgnoreComment = text.startsWith('ignore:') || text.startsWith('ignore_for_file:'); - final regTemplateExp = RegExp(r"{@template [\w'-]+}"); - final regMacroExp = RegExp(r"{@macro [\w'-]+}"); - - final isMacros = regTemplateExp.hasMatch(text) || - regMacroExp.hasMatch(text) || + final isMacros = _regTemplateExp.hasMatch(text) || + _regMacroExp.hasMatch(text) || text == '{@endtemplate}'; { From 367e601536f59bb3999200eca0b8b9c336c5121a Mon Sep 17 00:00:00 2001 From: vlkonoshenko Date: Tue, 3 May 2022 19:20:30 +0300 Subject: [PATCH 3/3] fix: review changes --- .../rules/rules_list/format_comment/visitor.dart | 12 ++++++------ .../examples/example_documentation.dart | 3 +++ 2 files changed, 9 insertions(+), 6 deletions(-) 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 efc1b94b41..3c83a2db08 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 @@ -5,8 +5,10 @@ const commentsOperator = { _CommentType.documentation: '///', }; -final _regTemplateExp = RegExp(r"{@template [\w'-]+}"); -final _regMacroExp = RegExp(r"{@macro [\w'-]+}"); +final _regMacrosExp = RegExp('{@(template|macro) .+}'); +const _macrosEndExp = '{@endtemplate}'; +const _ignoreExp = 'ignore:'; +const _ignoreForFileExp = 'ignore_for_file:'; class _Visitor extends RecursiveAstVisitor { final _comments = <_CommentInfo>[]; @@ -48,11 +50,9 @@ class _Visitor extends RecursiveAstVisitor { var text = commentText.trim(); final isIgnoreComment = - text.startsWith('ignore:') || text.startsWith('ignore_for_file:'); + text.startsWith(_ignoreExp) || text.startsWith(_ignoreForFileExp); - final isMacros = _regTemplateExp.hasMatch(text) || - _regMacroExp.hasMatch(text) || - text == '{@endtemplate}'; + final isMacros = _regMacrosExp.hasMatch(text) || text == _macrosEndExp; { if (text.isEmpty || isIgnoreComment || isMacros) { diff --git a/test/src/analyzers/lint_analyzer/rules/rules_list/format_comment/examples/example_documentation.dart b/test/src/analyzers/lint_analyzer/rules/rules_list/format_comment/examples/example_documentation.dart index 0b3917f10e..c670c8e891 100644 --- a/test/src/analyzers/lint_analyzer/rules/rules_list/format_comment/examples/example_documentation.dart +++ b/test/src/analyzers/lint_analyzer/rules/rules_list/format_comment/examples/example_documentation.dart @@ -25,3 +25,6 @@ void f2() {} /// {@macro template_name} void f3() {} + +/// {@template my_project.my_class.my_method} +void f4() {}