This repository was archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 271
feat: add avoid-creating-vector-in-update #1166
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8075573
feat: add new rule correct-game-instantiating
incendial 7b7d520
feat: add static code diagnostic avoid-initializing-in-on-mount
incendial ae4620b
feat: add avoid-creating-vector-in-update
incendial f5a1ab3
Merge remote-tracking branch 'origin/master' into avoid-creating-vect…
incendial 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 hidden or 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 hidden or 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
46 changes: 46 additions & 0 deletions
46
...ules/rules_list/avoid_creating_vector_in_update/avoid_creating_vector_in_update_rule.dart
This file contains hidden or 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,46 @@ | ||
// ignore_for_file: public_member_api_docs | ||
|
||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/dart/ast/visitor.dart'; | ||
// ignore: implementation_imports | ||
import 'package:collection/collection.dart'; | ||
|
||
import '../../../../../utils/flame_type_utils.dart'; | ||
import '../../../../../utils/node_utils.dart'; | ||
import '../../../lint_utils.dart'; | ||
import '../../../models/internal_resolved_unit_result.dart'; | ||
import '../../../models/issue.dart'; | ||
import '../../../models/severity.dart'; | ||
import '../../models/flame_rule.dart'; | ||
import '../../rule_utils.dart'; | ||
|
||
part 'visitor.dart'; | ||
|
||
class AvoidCreatingVectorInUpdateRule extends FlameRule { | ||
static const String ruleId = 'avoid-creating-vector-in-update'; | ||
|
||
static const _warningMessage = "Avoid creating Vector2 in 'update' method."; | ||
|
||
AvoidCreatingVectorInUpdateRule([Map<String, Object> config = const {}]) | ||
: super( | ||
id: ruleId, | ||
severity: readSeverity(config, Severity.warning), | ||
excludes: readExcludes(config), | ||
includes: readIncludes(config), | ||
); | ||
|
||
@override | ||
Iterable<Issue> check(InternalResolvedUnitResult source) { | ||
final visitor = _Visitor(); | ||
|
||
source.unit.visitChildren(visitor); | ||
|
||
return visitor.expressions | ||
.map((expression) => createIssue( | ||
rule: this, | ||
location: nodeLocation(node: expression, source: source), | ||
message: _warningMessage, | ||
)) | ||
.toList(growable: false); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...src/analyzers/lint_analyzer/rules/rules_list/avoid_creating_vector_in_update/visitor.dart
This file contains hidden or 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,52 @@ | ||
part of 'avoid_creating_vector_in_update_rule.dart'; | ||
|
||
class _Visitor extends SimpleAstVisitor<void> { | ||
final _expressions = <Expression>[]; | ||
|
||
Iterable<Expression> get expressions => _expressions; | ||
|
||
@override | ||
void visitClassDeclaration(ClassDeclaration node) { | ||
super.visitClassDeclaration(node); | ||
|
||
final type = node.extendsClause?.superclass.type; | ||
if (type == null || !isComponentOrSubclass(type)) { | ||
return; | ||
} | ||
|
||
final updateMethod = node.members.firstWhereOrNull((member) => | ||
member is MethodDeclaration && | ||
member.name.lexeme == 'update' && | ||
isOverride(member.metadata)); | ||
|
||
if (updateMethod is MethodDeclaration) { | ||
final visitor = _VectorVisitor(); | ||
updateMethod.visitChildren(visitor); | ||
|
||
_expressions.addAll(visitor.wrongExpressions); | ||
} | ||
} | ||
} | ||
|
||
class _VectorVisitor extends RecursiveAstVisitor<void> { | ||
final wrongExpressions = <Expression>{}; | ||
|
||
@override | ||
void visitInstanceCreationExpression(InstanceCreationExpression node) { | ||
super.visitInstanceCreationExpression(node); | ||
|
||
if (isVector(node.staticType)) { | ||
wrongExpressions.add(node); | ||
} | ||
} | ||
|
||
@override | ||
void visitBinaryExpression(BinaryExpression node) { | ||
super.visitBinaryExpression(node); | ||
|
||
if (isVector(node.leftOperand.staticType) && | ||
isVector(node.rightOperand.staticType)) { | ||
wrongExpressions.add(node); | ||
} | ||
} | ||
} |
This file contains hidden or 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
43 changes: 43 additions & 0 deletions
43
...rules_list/avoid_creating_vector_in_update/avoid_creating_vector_in_update_rule_test.dart
This file contains hidden or 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,43 @@ | ||
import 'package:dart_code_metrics/src/analyzers/lint_analyzer/models/severity.dart'; | ||
import 'package:dart_code_metrics/src/analyzers/lint_analyzer/rules/rules_list/avoid_creating_vector_in_update/avoid_creating_vector_in_update_rule.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../../../../../helpers/rule_test_helper.dart'; | ||
|
||
const _examplePath = 'avoid_creating_vector_in_update/examples/example.dart'; | ||
|
||
void main() { | ||
group('AvoidCreatingVectorInUpdateRule', () { | ||
test('initialization', () async { | ||
final unit = await RuleTestHelper.resolveFromFile(_examplePath); | ||
final issues = AvoidCreatingVectorInUpdateRule().check(unit); | ||
|
||
RuleTestHelper.verifyInitialization( | ||
issues: issues, | ||
ruleId: 'avoid-creating-vector-in-update', | ||
severity: Severity.warning, | ||
); | ||
}); | ||
|
||
test('reports about found issues', () async { | ||
final unit = await RuleTestHelper.resolveFromFile(_examplePath); | ||
final issues = AvoidCreatingVectorInUpdateRule().check(unit); | ||
|
||
RuleTestHelper.verifyIssues( | ||
issues: issues, | ||
startLines: [4, 23, 24], | ||
startColumns: [23, 23, 23], | ||
locationTexts: [ | ||
'Vector2(10, 10)', | ||
'vector1 + vector2', | ||
'vector1 - vector2', | ||
], | ||
messages: [ | ||
"Avoid creating Vector2 in 'update' method.", | ||
"Avoid creating Vector2 in 'update' method.", | ||
"Avoid creating Vector2 in 'update' method.", | ||
], | ||
); | ||
}); | ||
}); | ||
} |
58 changes: 58 additions & 0 deletions
58
...zers/lint_analyzer/rules/rules_list/avoid_creating_vector_in_update/examples/example.dart
This file contains hidden or 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,58 @@ | ||
class MyComponent extends Component { | ||
@override | ||
void update(double dt) { | ||
final newVector = Vector2(10, 10); // LINT | ||
} | ||
} | ||
|
||
class MyComponent extends Component { | ||
final _temporaryVector = Vector2.zero(); | ||
|
||
@override | ||
void update(double dt) { | ||
_temporaryVector.setValues(10, 10); | ||
} | ||
} | ||
|
||
class MyComponent extends Component { | ||
final vector1 = Vector2(10, 10); | ||
final vector2 = Vector2(20, 20); | ||
|
||
@override | ||
void update(double dt) { | ||
final addVector = vector1 + vector2; // LINT | ||
final subVector = vector1 - vector2; // LINT | ||
} | ||
} | ||
|
||
class Component { | ||
void onMount() {} | ||
} | ||
|
||
class Vector2 { | ||
final double x; | ||
final double y; | ||
|
||
const Vector2(this.x, this.y); | ||
|
||
const Vector2.zero() | ||
: x = 0, | ||
y = 0; | ||
|
||
void setValues(double x, double y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
|
||
@override | ||
Vector2 operator /(double scale) => this; | ||
|
||
@override | ||
Vector2 operator +(double scale) => this; | ||
|
||
@override | ||
Vector2 operator -(double scale) => this; | ||
|
||
@override | ||
Vector2 operator *(double scale) => this; | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.