From 4dc4b4e6c264d018bd5bdbb353af0ec02a8cd8da Mon Sep 17 00:00:00 2001 From: tsukuda jukiya <67436434+boywithdv@users.noreply.github.com> Date: Tue, 5 Nov 2024 14:33:57 +0900 Subject: [PATCH] fix: Resolved warnings false detection in ColorScheme definition (#73) * 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 <75433035+k-nkmr@users.noreply.github.com> * chore: revision * fix: revision --------- Co-authored-by: nkmr <75433035+k-nkmr@users.noreply.github.com> --- .../example/lints/avoid_hardcoded_color.dart | 5 ++++- .../lib/src/lints/avoid_hardcoded_color.dart | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/altive_lints/example/lints/avoid_hardcoded_color.dart b/packages/altive_lints/example/lints/avoid_hardcoded_color.dart index fb27ea4..0be2bdf 100644 --- a/packages/altive_lints/example/lints/avoid_hardcoded_color.dart +++ b/packages/altive_lints/example/lints/avoid_hardcoded_color.dart @@ -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), + ); \ No newline at end of file diff --git a/packages/altive_lints/lib/src/lints/avoid_hardcoded_color.dart b/packages/altive_lints/lib/src/lints/avoid_hardcoded_color.dart index f904911..8d42bc7 100644 --- a/packages/altive_lints/lib/src/lints/avoid_hardcoded_color.dart +++ b/packages/altive_lints/lib/src/lints/avoid_hardcoded_color.dart @@ -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'; @@ -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') { @@ -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; + } }