Skip to content

Commit

Permalink
add avoid_multiple_declarations_per_line (#2502)
Browse files Browse the repository at this point in the history
* add avoid_multiple_declarations_per_line

* only highlight second variable in declaration list

* highlight only variable name
  • Loading branch information
lukepighetti authored Mar 11, 2021
1 parent 5b0a9c3 commit dff708b
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions example/all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ linter:
- avoid_implementing_value_types
- avoid_init_to_null
- avoid_js_rounded_ints
- avoid_multiple_declarations_per_line
- avoid_null_checks_in_equality_operators
- avoid_positional_boolean_parameters
- avoid_print
Expand Down
2 changes: 2 additions & 0 deletions lib/src/rules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import 'rules/avoid_function_literals_in_foreach_calls.dart';
import 'rules/avoid_implementing_value_types.dart';
import 'rules/avoid_init_to_null.dart';
import 'rules/avoid_js_rounded_ints.dart';
import 'rules/avoid_multiple_declarations_per_line.dart';
import 'rules/avoid_null_checks_in_equality_operators.dart';
import 'rules/avoid_positional_boolean_parameters.dart';
import 'rules/avoid_print.dart';
Expand Down Expand Up @@ -213,6 +214,7 @@ void registerLintRules() {
..register(AvoidImplementingValueTypes())
..register(AvoidInitToNull())
..register(AvoidJsRoundedInts())
..register(AvoidMultipleDeclarationsPerLine())
..register(AvoidNullChecksInEqualityOperators())
..register(AvoidOperatorEqualsOnMutableClasses())
..register(AvoidPositionalBooleanParameters())
Expand Down
61 changes: 61 additions & 0 deletions lib/src/rules/avoid_multiple_declarations_per_line.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';

import '../analyzer.dart';

const _desc = r"Don't declare multiple variables on a single line.";

const _details = r'''
**DON'T** declare multiple variables on a single line.
**BAD:**
```
String? foo, bar, baz;
```
**GOOD:**
```
String? foo;
String? bar;
String? baz;
```
''';

class AvoidMultipleDeclarationsPerLine extends LintRule
implements NodeLintRule {
AvoidMultipleDeclarationsPerLine()
: super(
name: 'avoid_multiple_declarations_per_line',
description: _desc,
details: _details,
group: Group.style);

@override
void registerNodeProcessors(
NodeLintRegistry registry, LinterContext context) {
final visitor = _Visitor(this);
registry.addVariableDeclarationList(this, visitor);
}
}

class _Visitor extends SimpleAstVisitor {
final LintRule rule;

_Visitor(this.rule);

@override
void visitVariableDeclarationList(VariableDeclarationList node) {
final variables = node.variables;

if (variables.length > 1) {
final secondVariable = variables[1];
rule.reportLint(secondVariable.name);
}
}
}
53 changes: 53 additions & 0 deletions test/rules/avoid_multiple_declarations_per_line.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// test w/ `pub run test -N avoid_multiple_declarations_per_line`

// ignore_for_file: unused_local_variable

String? badFoo, badBar, badBaz; // LINT

String? goodFoo;
String? goodBar;
String? goodBaz;

methodContainingBadDeclaration() {
String? badFoo, badBar, badBaz; // LINT
}

methodContainingGoodDeclaration() {
String? goodFoo;
String? goodBar;
String? goodBaz;
}

class BadClass {
String? foo, bar, baz; // LINT

methodContainingBadDeclaration() {
String? badFoo, badBar, badBaz; // LINT
}
}

class GoodClass {
String? foo;
String? bar;
String? baz;

methodContainingGoodDeclaration() {
String? goodFoo;
String? goodBar;
String? goodBaz;
}
}

extension BadExtension on Object {
static String? badFoo, badBar, badBaz; // LINT
}

extension GoodExtension on Object {
static String? foo;
static String? bar;
static String? baz;
}

0 comments on commit dff708b

Please sign in to comment.