Skip to content

Commit

Permalink
fix: Resolved warnings false detection in ColorScheme definition (#73)
Browse files Browse the repository at this point in the history
* fix: warnings-false-detection-in-ColorScheme-definition

* fix: selfreview

* fix: Define in colorscheme only the colors to be used.

* Update packages/altive_lints/lib/src/lints/avoid_hardcoded_color.dart

Co-authored-by: nkmr <[email protected]>

* chore: revision

* fix: revision

---------

Co-authored-by: nkmr <[email protected]>
  • Loading branch information
boywithdv and k-nkmr authored Nov 5, 2024
1 parent 02d1848 commit 4dc4b4e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ class MyWidget extends StatelessWidget {
// expect_lint: avoid_hardcoded_color
const ColoredBox(color: Colors.green),
ColoredBox(color: Theme.of(context).colorScheme.primary),

ColoredBox(color: _colorScheme.primary),
const ColoredBox(color: Colors.transparent),
],
);
}
}
ColorScheme get _colorScheme => const ColorScheme.dark(
primary:Color.fromRGBO(0, 255, 0, 1),
);
16 changes: 16 additions & 0 deletions packages/altive_lints/lib/src/lints/avoid_hardcoded_color.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/error/listener.dart';
Expand Down Expand Up @@ -46,6 +47,9 @@ class AvoidHardcodedColor extends DartLintRule {
CustomLintContext context,
) {
context.registry.addInstanceCreationExpression((node) {
if (_isInsideColorScheme(node)) {
return;
}
final typeName = node.staticType?.getDisplayString();

if (typeName == 'Color') {
Expand Down Expand Up @@ -78,4 +82,16 @@ class AvoidHardcodedColor extends DartLintRule {
type.getDisplayString() == 'MaterialColor' ||
type.getDisplayString() == 'MaterialAccentColor');
}

bool _isInsideColorScheme(AstNode node) {
var parent = node.parent;
while (parent != null) {
if (parent is InstanceCreationExpression &&
parent.staticType?.getDisplayString() == 'ColorScheme') {
return true;
}
parent = parent.parent;
}
return false;
}
}

0 comments on commit 4dc4b4e

Please sign in to comment.