Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add avoid_multiple_declarations_per_line #2502

Merged
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
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;
}