-
Notifications
You must be signed in to change notification settings - Fork 170
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
Added rule avoid_final_parameters #3045
Merged
pq
merged 2 commits into
dart-lang:master
from
mat100payette:avoid_final_parameters_rule
Oct 28, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ Guillermo López-Anglada <[email protected]> | |
Parker Lougheed <[email protected]> | ||
David Martos <[email protected]> | ||
Juan Pablo Paulsen <[email protected]> | ||
Mathieu Payette <[email protected]> | ||
Luke Pighetti <[email protected]> | ||
Sem van Rij <[email protected]> | ||
Michael Joseph Rosenthal <[email protected]> | ||
|
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,103 @@ | ||
// 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'Avoid final for parameter declarations'; | ||
|
||
const _details = r''' | ||
|
||
**AVOID** declaring parameters as final. | ||
|
||
Declaring parameters as final can lead to unecessarily verbose code, especially | ||
when using the "parameter_assignments" rule. | ||
|
||
**GOOD:** | ||
```dart | ||
void badParameter(String label) { // OK | ||
print(label); | ||
} | ||
``` | ||
|
||
**BAD:** | ||
```dart | ||
void goodParameter(final String label) { // LINT | ||
print(label); | ||
} | ||
``` | ||
|
||
**GOOD:** | ||
```dart | ||
void badExpression(int value) => print(value); // OK | ||
``` | ||
|
||
**BAD:** | ||
```dart | ||
void goodExpression(final int value) => print(value); // LINT | ||
``` | ||
|
||
**GOOD:** | ||
```dart | ||
[1, 4, 6, 8].forEach((value) => print(value + 2)); // OK | ||
``` | ||
|
||
**BAD:** | ||
```dart | ||
[1, 4, 6, 8].forEach((final value) => print(value + 2)); // LINT | ||
``` | ||
|
||
'''; | ||
|
||
class AvoidFinalParameters extends LintRule { | ||
AvoidFinalParameters() | ||
: super( | ||
name: 'avoid_final_parameters', | ||
description: _desc, | ||
details: _details, | ||
group: Group.style); | ||
|
||
@override | ||
List<String> get incompatibleRules => const ['prefer_final_parameters']; | ||
|
||
@override | ||
void registerNodeProcessors( | ||
NodeLintRegistry registry, LinterContext context) { | ||
var visitor = _Visitor(this); | ||
registry.addConstructorDeclaration(this, visitor); | ||
registry.addFunctionExpression(this, visitor); | ||
registry.addMethodDeclaration(this, visitor); | ||
} | ||
} | ||
|
||
class _Visitor extends SimpleAstVisitor { | ||
final LintRule rule; | ||
|
||
_Visitor(this.rule); | ||
|
||
/// Report the lint for parameters in the [parameters] list that are final. | ||
void _reportApplicableParameters(FormalParameterList? parameters) { | ||
if (parameters != null) { | ||
for (var param in parameters.parameters) { | ||
if (param.isFinal) { | ||
rule.reportLint(param); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@override | ||
void visitConstructorDeclaration(ConstructorDeclaration node) => | ||
_reportApplicableParameters(node.parameters); | ||
|
||
@override | ||
void visitFunctionExpression(FunctionExpression node) => | ||
_reportApplicableParameters(node.parameters); | ||
|
||
@override | ||
void visitMethodDeclaration(MethodDeclaration node) => | ||
_reportApplicableParameters(node.parameters); | ||
} |
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,139 @@ | ||
// 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/ `dart test -N avoid_final_parameters` | ||
|
||
void badRequiredPositional(final String label) { // LINT | ||
print(label); | ||
} | ||
|
||
void goodRequiredPositional(String label) { // OK | ||
print(label); | ||
} | ||
|
||
void badOptionalPosition([final String? label]) { // LINT | ||
print(label); | ||
} | ||
|
||
void goodOptionalPosition([String? label]) { // OK | ||
print(label); | ||
} | ||
|
||
void badRequiredNamed({required final String label}) { // LINT | ||
print(label); | ||
} | ||
|
||
void goodRequiredNamed({required String label}) { // OK | ||
print(label); | ||
} | ||
|
||
void badOptionalNamed({final String? label}) { // LINT | ||
print(label); | ||
} | ||
|
||
void goodOptionalNamed({String? label}) { // OK | ||
print(label); | ||
} | ||
|
||
void badExpression(final int value) => print(value); // LINT | ||
|
||
void goodExpression(int value) => print(value); // OK | ||
|
||
bool? _testingVariable; | ||
|
||
void set badSet(final bool setting) => _testingVariable = setting; // LINT | ||
|
||
void set goodSet(bool setting) => _testingVariable = setting; // OK | ||
|
||
var badClosure = (final Object random) { // LINT | ||
print(random); | ||
}; | ||
|
||
var goodClosure = (Object random) { // OK | ||
print(random); | ||
}; | ||
|
||
var _testingList = [1, 7, 15, 20]; | ||
|
||
void useBadClosureArgument() { | ||
_testingList.forEach((final element) => print(element + 4)); // LINT | ||
} | ||
|
||
void useGoodClosureArgument() { | ||
_testingList.forEach((element) => print(element + 4)); // OK | ||
} | ||
|
||
void useGoodTypedClosureArgument() { | ||
_testingList.forEach((int element) => print(element + 4)); // OK | ||
} | ||
|
||
void badMixedLast(final String bad, String good) { // LINT | ||
print(bad); | ||
print(good); | ||
} | ||
|
||
void badMixedFirst(String goodFirst, final String badSecond) { // LINT | ||
print(goodFirst); | ||
print(badSecond); | ||
} | ||
|
||
// LINT [+1] | ||
void badMixedMiddle(final String badFirst, String goodSecond, final String badThird) { // LINT | ||
print(badFirst); | ||
print(goodSecond); | ||
print(badThird); | ||
} | ||
|
||
void goodMultiple(String bad, String good) { // OK | ||
print(bad); | ||
print(good); | ||
} | ||
|
||
class C { | ||
String value = ''; | ||
int _contents = 0; | ||
|
||
C(final String content) { // LINT | ||
_contents = content.length; | ||
} | ||
|
||
C.bad(final int contents) : _contents = contents; // LINT | ||
|
||
C.good(int contents) : _contents = contents; // OK | ||
|
||
C.badValue(final String value) : this.value = value; // LINT | ||
|
||
C.goodValue(this.value); // OK | ||
|
||
factory C.goodFactory(String value) { // OK | ||
return C(value); | ||
} | ||
|
||
factory C.badFactory(final String value) { // LINT | ||
return C(value); | ||
} | ||
|
||
void set badContents(final int contents) => _contents = contents; // LINT | ||
void set goodContents(int contents) => _contents = contents; // OK | ||
|
||
int get contentValue => _contents + 4; // OK | ||
|
||
void badMethod(final String bad) { // LINT | ||
print(bad); | ||
} | ||
|
||
void goodMethod(String good) { // OK | ||
print(good); | ||
} | ||
|
||
@override | ||
C operator +(final C other) { // LINT | ||
return C.good(contentValue + other.contentValue); | ||
} | ||
|
||
@override | ||
C operator -(C other) { // OK | ||
return C.good(contentValue + other.contentValue); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although there are a couple of places where
FormalParameterList
can appear in the AST that aren't covered here, I believe thatfinal
is invalid in those places. As a result, it might be easier today, and require less maintenance in the future, to register to visitFormalParameterList
directly than to register to visit the parents.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bwilkerson Just to be on the same page, can you provide 1 or 2 small examples of these other places ? To stay as consistent as possible, I made this rule reflect the opposite of
prefer_final_parameters
. That being said, I wouldn't mind doing slight changes to this PR if I understand them lol.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the course of replying I decided that it's better to leave the code the way you have it. Sorry for the noise.
Nodes of type
FormalParameterList
appear as a child of the following node types:ConstructorDeclaration
,FieldFormalParameter
,FunctionExpression
,FunctionTypeAlias
,FunctionTypedFormalParameter
,GenericFunctionType
, andMethodDeclaration
. The ones that are currently missing are in typedefs, both the old and new syntactic form, and in formal parameters whose type is a function. I believe that in all those locations any use offinal
is an error. Which means that if you did what I suggested we'd be double reporting in those places, which is something we strive to avoid.