-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add avoid_multiple_declarations_per_line (#2502)
* add avoid_multiple_declarations_per_line * only highlight second variable in declaration list * highlight only variable name
- Loading branch information
1 parent
5b0a9c3
commit dff708b
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |