Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/src/rules/no_adjacent_strings_in_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -56,6 +58,20 @@ class _Visitor extends SimpleAstVisitor<void> {

_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) {
Expand Down
16 changes: 13 additions & 3 deletions test_data/rules/no_adjacent_strings_in_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,31 @@
// test w/ `dart test -N no_adjacent_strings_in_list`

void bad() {
List<String> list = <String>[
var list = [
'a' // LINT
'b',
'c',
];

List<String> list2 = <String>[
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<String> list = <String>[
var list = [
'a' + // OK
'b',
'c',
Expand Down