From 020759472f2cde84f5c5bd7f7e7f0c21185ec654 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Fri, 18 Jun 2021 13:40:04 -0700 Subject: [PATCH] Report adjacent strings in if-elements and for-elements --- .../rules/no_adjacent_strings_in_list.dart | 20 +++++++++++++++++-- .../rules/no_adjacent_strings_in_list.dart | 16 ++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/src/rules/no_adjacent_strings_in_list.dart b/lib/src/rules/no_adjacent_strings_in_list.dart index eab3b26ab..b327f531a 100644 --- a/lib/src/rules/no_adjacent_strings_in_list.dart +++ b/lib/src/rules/no_adjacent_strings_in_list.dart @@ -11,9 +11,9 @@ const _desc = r"Don't use adjacent strings in list."; const _details = r''' -**DON'T** use adjacent strings in list. +**DON'T** use adjacent strings in a list. -This can be sign of forgotten comma. +This can indicate a forgotten comma. **GOOD:** ```dart @@ -47,6 +47,8 @@ class NoAdjacentStringsInList extends LintRule implements NodeLintRule { void registerNodeProcessors( NodeLintRegistry registry, LinterContext context) { var visitor = _Visitor(this); + registry.addForElement(this, visitor); + registry.addIfElement(this, visitor); registry.addListLiteral(this, visitor); } } @@ -56,6 +58,20 @@ class _Visitor extends SimpleAstVisitor { _Visitor(this.rule); + @override + void visitForElement(ForElement node) { + if (node.body is AdjacentStrings) { + rule.reportLint(node.body); + } + } + + @override + void visitIfElement(IfElement node) { + if (node.thenElement is AdjacentStrings) { + rule.reportLint(node.thenElement); + } + } + @override void visitListLiteral(ListLiteral node) { for (var e in node.elements) { diff --git a/test_data/rules/no_adjacent_strings_in_list.dart b/test_data/rules/no_adjacent_strings_in_list.dart index 2476879b1..a6f998e2c 100644 --- a/test_data/rules/no_adjacent_strings_in_list.dart +++ b/test_data/rules/no_adjacent_strings_in_list.dart @@ -5,21 +5,31 @@ // test w/ `dart test -N no_adjacent_strings_in_list` void bad() { - List list = [ + var list = [ 'a' // LINT 'b', 'c', ]; - List list2 = [ + var list2 = [ 'a' // LINT 'b' 'c' ]; + + var list3 = [ + if (1 == 2) 'a' // LINT + 'b' + ]; + + var list4 = [ + for (var v in []) 'a' // LINT + 'b' + ]; } void good() { - List list = [ + var list = [ 'a' + // OK 'b', 'c',