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 267
feat: add new rule correct-game-instantiating #1163
Merged
Merged
Changes from all commits
Commits
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
16 changes: 16 additions & 0 deletions
16
lib/src/analyzers/lint_analyzer/rules/models/flame_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,16 @@ | ||
import 'rule.dart'; | ||
import 'rule_type.dart'; | ||
|
||
/// Represents a base class for Flutter-specific rules. | ||
abstract class FlameRule extends Rule { | ||
static const link = 'https://pub.dev/packages/flame'; | ||
|
||
const FlameRule({ | ||
required super.id, | ||
required super.severity, | ||
required super.excludes, | ||
required super.includes, | ||
}) : super( | ||
type: RuleType.flame, | ||
); | ||
} |
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
75 changes: 75 additions & 0 deletions
75
...analyzer/rules/rules_list/correct_game_instantiating/correct_game_instantiating_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,75 @@ | ||
// ignore_for_file: public_member_api_docs | ||
|
||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/dart/ast/visitor.dart'; | ||
import 'package:collection/collection.dart'; | ||
|
||
import '../../../../../utils/flutter_types_utils.dart'; | ||
import '../../../../../utils/node_utils.dart'; | ||
import '../../../lint_utils.dart'; | ||
import '../../../models/internal_resolved_unit_result.dart'; | ||
import '../../../models/issue.dart'; | ||
import '../../../models/replacement.dart'; | ||
import '../../../models/severity.dart'; | ||
import '../../models/flame_rule.dart'; | ||
import '../../rule_utils.dart'; | ||
|
||
part 'visitor.dart'; | ||
|
||
class CorrectGameInstantiatingRule extends FlameRule { | ||
static const String ruleId = 'correct-game-instantiating'; | ||
|
||
static const _warningMessage = | ||
'Incorrect game instantiation. The game will reset on each rebuild.'; | ||
static const _correctionMessage = "Replace with 'controlled'."; | ||
|
||
CorrectGameInstantiatingRule([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.info | ||
.map((info) => createIssue( | ||
rule: this, | ||
location: nodeLocation(node: info.node, source: source), | ||
message: _warningMessage, | ||
replacement: _createReplacement(info), | ||
)) | ||
.toList(growable: false); | ||
} | ||
|
||
Replacement? _createReplacement(_InstantiationInfo info) { | ||
if (info.isStateless) { | ||
final arguments = info.node.argumentList.arguments.map((arg) { | ||
if (arg is NamedExpression && arg.name.label.name == 'game') { | ||
final expression = arg.expression; | ||
if (expression is InstanceCreationExpression) { | ||
final name = | ||
expression.staticType?.getDisplayString(withNullability: false); | ||
if (name != null) { | ||
return 'gameFactory: $name.new,'; | ||
} | ||
} | ||
} | ||
|
||
return arg.toSource(); | ||
}); | ||
|
||
return Replacement( | ||
replacement: 'GameWidget.controlled$arguments;', | ||
comment: _correctionMessage, | ||
); | ||
} | ||
|
||
return null; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
lib/src/analyzers/lint_analyzer/rules/rules_list/correct_game_instantiating/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,69 @@ | ||
part of 'correct_game_instantiating_rule.dart'; | ||
|
||
class _Visitor extends RecursiveAstVisitor<void> { | ||
final _info = <_InstantiationInfo>[]; | ||
|
||
Iterable<_InstantiationInfo> get info => _info; | ||
|
||
@override | ||
void visitClassDeclaration(ClassDeclaration node) { | ||
super.visitClassDeclaration(node); | ||
|
||
final type = node.extendsClause?.superclass.type; | ||
if (type == null) { | ||
return; | ||
} | ||
|
||
final isWidget = isWidgetOrSubclass(type); | ||
final isWidgetState = isWidgetStateOrSubclass(type); | ||
if (!isWidget && !isWidgetState) { | ||
return; | ||
} | ||
|
||
final declaration = node.members.firstWhereOrNull((declaration) => | ||
declaration is MethodDeclaration && declaration.name.lexeme == 'build'); | ||
|
||
if (declaration is MethodDeclaration) { | ||
final visitor = _GameWidgetInstantiatingVisitor(); | ||
declaration.visitChildren(visitor); | ||
|
||
if (visitor.wrongInstantiation != null) { | ||
_info.add(_InstantiationInfo( | ||
visitor.wrongInstantiation!, | ||
isStateless: !isWidgetState, | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
class _GameWidgetInstantiatingVisitor extends RecursiveAstVisitor<void> { | ||
InstanceCreationExpression? wrongInstantiation; | ||
|
||
_GameWidgetInstantiatingVisitor(); | ||
|
||
@override | ||
void visitInstanceCreationExpression(InstanceCreationExpression node) { | ||
super.visitInstanceCreationExpression(node); | ||
|
||
if (isGameWidget(node.staticType) && | ||
node.constructorName.name?.name != 'controlled') { | ||
final gameArgument = node.argumentList.arguments.firstWhereOrNull( | ||
(argument) => | ||
argument is NamedExpression && argument.name.label.name == 'game', | ||
); | ||
|
||
if (gameArgument is NamedExpression && | ||
gameArgument.expression is InstanceCreationExpression) { | ||
wrongInstantiation = node; | ||
} | ||
} | ||
} | ||
} | ||
|
||
class _InstantiationInfo { | ||
final bool isStateless; | ||
final InstanceCreationExpression node; | ||
|
||
const _InstantiationInfo(this.node, {required this.isStateless}); | ||
} |
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
49 changes: 49 additions & 0 deletions
49
...zer/rules/rules_list/correct_game_instantiating/correct_game_instantiating_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,49 @@ | ||
import 'package:dart_code_metrics/src/analyzers/lint_analyzer/models/severity.dart'; | ||
import 'package:dart_code_metrics/src/analyzers/lint_analyzer/rules/rules_list/correct_game_instantiating/correct_game_instantiating_rule.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../../../../../helpers/rule_test_helper.dart'; | ||
|
||
const _examplePath = 'correct_game_instantiating/examples/example.dart'; | ||
|
||
void main() { | ||
group('CorrectGameInstantiatingRule', () { | ||
test('initialization', () async { | ||
final unit = await RuleTestHelper.resolveFromFile(_examplePath); | ||
final issues = CorrectGameInstantiatingRule().check(unit); | ||
|
||
RuleTestHelper.verifyInitialization( | ||
issues: issues, | ||
ruleId: 'correct-game-instantiating', | ||
severity: Severity.warning, | ||
); | ||
}); | ||
|
||
test('reports about found issues', () async { | ||
final unit = await RuleTestHelper.resolveFromFile(_examplePath); | ||
final issues = CorrectGameInstantiatingRule().check(unit); | ||
|
||
RuleTestHelper.verifyIssues( | ||
issues: issues, | ||
startLines: [4, 25], | ||
startColumns: [12, 12], | ||
locationTexts: [ | ||
'GameWidget(game: MyFlameGame())', | ||
'GameWidget(game: MyFlameGame())', | ||
], | ||
messages: [ | ||
'Incorrect game instantiation. The game will reset on each rebuild.', | ||
'Incorrect game instantiation. The game will reset on each rebuild.', | ||
], | ||
replacements: [ | ||
'GameWidget.controlled(gameFactory: MyFlameGame.new,);', | ||
null, | ||
], | ||
replacementComments: [ | ||
"Replace with 'controlled'.", | ||
null, | ||
], | ||
); | ||
}); | ||
}); | ||
} |
64 changes: 64 additions & 0 deletions
64
...analyzers/lint_analyzer/rules/rules_list/correct_game_instantiating/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,64 @@ | ||
class MyGamePage extends StatelessWidget { | ||
@override | ||
Widget build(BuildContext context) { | ||
return GameWidget(game: MyFlameGame()); // LINT | ||
} | ||
} | ||
|
||
class MyGamePage extends StatefulWidget { | ||
@override | ||
State<MyGamePage> createState() => _MyGamePageState(); | ||
} | ||
|
||
class _MyGamePageState extends State<MyGamePage> { | ||
late final _game = MyFlameGame(); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return GameWidget(game: _game); | ||
} | ||
} | ||
|
||
class _MyGamePageState extends State<MyGamePage> { | ||
@override | ||
Widget build(BuildContext context) { | ||
return GameWidget(game: MyFlameGame()); // LINT | ||
} | ||
} | ||
|
||
class MyGamePage extends StatelessWidget { | ||
@override | ||
Widget build(BuildContext context) { | ||
return GameWidget.controlled(gameFactory: MyFlameGame.new); | ||
} | ||
} | ||
|
||
class GameWidget extends Widget { | ||
final Widget game; | ||
|
||
GameWidget({required this.game}); | ||
|
||
GameWidget.controlled({required GameFactory<Widget> gameFactory}) { | ||
this.game = gameFactory(); | ||
} | ||
} | ||
|
||
class MyFlameGame extends Widget {} | ||
|
||
typedef GameFactory<T> = T Function(); | ||
|
||
class StatefulWidget extends Widget {} | ||
|
||
class StatelessWidget extends Widget {} | ||
|
||
class Widget { | ||
const Widget(); | ||
} | ||
|
||
class BuildContext {} | ||
|
||
abstract class State<T> { | ||
void initState(); | ||
|
||
void setState(VoidCallback callback) => callback(); | ||
} |
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
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.
@incendial maybe
for Flutter Flame Engine
?