From 266bcdb6cb81751385a32a89a3cfe2995266d6e1 Mon Sep 17 00:00:00 2001 From: Ryunosuke Muramatsu Date: Mon, 23 Dec 2024 12:04:40 +0900 Subject: [PATCH 1/2] feat: disable avoid_hardcoded_color lint if it's a test file --- .../lib/src/lints/avoid_hardcoded_color.dart | 5 +++ .../src/lints/avoid_hardcoded_japanese.dart | 7 ++-- .../lib/src/utils/files_utils.dart | 7 ++++ packages/altive_lints/pubspec.yaml | 3 ++ .../test/src/utils/files_utils_test.dart | 35 +++++++++++++++++++ 5 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 packages/altive_lints/lib/src/utils/files_utils.dart create mode 100644 packages/altive_lints/test/src/utils/files_utils_test.dart 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 8d42bc7..f1ba37d 100644 --- a/packages/altive_lints/lib/src/lints/avoid_hardcoded_color.dart +++ b/packages/altive_lints/lib/src/lints/avoid_hardcoded_color.dart @@ -4,6 +4,8 @@ import 'package:analyzer/dart/element/type.dart'; import 'package:analyzer/error/listener.dart'; import 'package:custom_lint_builder/custom_lint_builder.dart'; +import '../utils/files_utils.dart'; + /// An `avoid_hardcoded_color` rule that discourages the use of /// hardcoded colors directly in the code, promoting the use of `ColorScheme`, /// `ThemeExtension`, or other Theme-based systems for defining colors. @@ -46,6 +48,9 @@ class AvoidHardcodedColor extends DartLintRule { ErrorReporter reporter, CustomLintContext context, ) { + if (isTestFile(resolver.source)) { + return; + } context.registry.addInstanceCreationExpression((node) { if (_isInsideColorScheme(node)) { return; diff --git a/packages/altive_lints/lib/src/lints/avoid_hardcoded_japanese.dart b/packages/altive_lints/lib/src/lints/avoid_hardcoded_japanese.dart index 508bb73..acaa11a 100644 --- a/packages/altive_lints/lib/src/lints/avoid_hardcoded_japanese.dart +++ b/packages/altive_lints/lib/src/lints/avoid_hardcoded_japanese.dart @@ -1,6 +1,8 @@ import 'package:analyzer/error/listener.dart'; import 'package:custom_lint_builder/custom_lint_builder.dart'; +import '../utils/files_utils.dart'; + /// An `avoid_hardcoded_japanese` rule which detects /// and reports hardcoded Japanese text strings within the code. /// @@ -40,10 +42,7 @@ class AvoidHardcodedJapanese extends DartLintRule { ErrorReporter reporter, CustomLintContext context, ) { - if (resolver.source.uri.pathSegments.contains('test')) { - return; - } - if (resolver.source.shortName.endsWith('_test.dart')) { + if (isTestFile(resolver.source)) { return; } context.registry.addSimpleStringLiteral((node) { diff --git a/packages/altive_lints/lib/src/utils/files_utils.dart b/packages/altive_lints/lib/src/utils/files_utils.dart new file mode 100644 index 0000000..b0f82cf --- /dev/null +++ b/packages/altive_lints/lib/src/utils/files_utils.dart @@ -0,0 +1,7 @@ +import 'package:analyzer/source/source.dart'; + +/// Whether the given [source] is a test file. +bool isTestFile(Source source) { + return source.uri.pathSegments.contains('test') || + source.shortName.endsWith('_test.dart'); +} diff --git a/packages/altive_lints/pubspec.yaml b/packages/altive_lints/pubspec.yaml index 6a8b1a9..9adcdf2 100644 --- a/packages/altive_lints/pubspec.yaml +++ b/packages/altive_lints/pubspec.yaml @@ -18,3 +18,6 @@ dependencies: analyzer: ^6.7.0 collection: ^1.18.0 custom_lint_builder: ^0.7.0 + +dev_dependencies: + test: ^1.25.14 diff --git a/packages/altive_lints/test/src/utils/files_utils_test.dart b/packages/altive_lints/test/src/utils/files_utils_test.dart new file mode 100644 index 0000000..2341fba --- /dev/null +++ b/packages/altive_lints/test/src/utils/files_utils_test.dart @@ -0,0 +1,35 @@ +import 'package:altive_lints/src/utils/files_utils.dart'; +import 'package:analyzer/file_system/physical_file_system.dart'; +import 'package:analyzer/source/file_source.dart'; +import 'package:test/test.dart'; + +void main() { + group('isTestFile', () { + test('False if it is a not test file', () { + final resourceProvider = PhysicalResourceProvider.INSTANCE; + + const filePath = '/project/lib/src/implementation.dart'; + final file = resourceProvider.getFile(filePath); + final source = FileSource(file); + expect(isTestFile(source), isFalse); + }); + + test('True if it is a test file', () { + final resourceProvider = PhysicalResourceProvider.INSTANCE; + + const filePath = '/project/lib/src/implementation_test.dart'; + final file = resourceProvider.getFile(filePath); + final source = FileSource(file); + expect(isTestFile(source), isTrue); + }); + + test('True if it is a test directory', () { + final resourceProvider = PhysicalResourceProvider.INSTANCE; + + const filePath = '/project/test/src/util.dart'; + final file = resourceProvider.getFile(filePath); + final source = FileSource(file); + expect(isTestFile(source), isTrue); + }); + }); +} From ed160c9b45354e52ecf6ad2d65259b2f64e354d5 Mon Sep 17 00:00:00 2001 From: Ryunosuke Muramatsu Date: Wed, 25 Dec 2024 05:38:36 +0900 Subject: [PATCH 2/2] test: add file for tests --- .../lints/avoid_hardcoded_color_test.dart | 28 +++++++++++++++++++ .../example/test/avoid_hardcoded_color.dart | 27 ++++++++++++++++++ ...est.dart => avoid_hardcoded_japanese.dart} | 0 3 files changed, 55 insertions(+) create mode 100644 packages/altive_lints/example/lints/avoid_hardcoded_color_test.dart create mode 100644 packages/altive_lints/example/test/avoid_hardcoded_color.dart rename packages/altive_lints/example/test/{avoid_hardcoded_japanese_test.dart => avoid_hardcoded_japanese.dart} (100%) diff --git a/packages/altive_lints/example/lints/avoid_hardcoded_color_test.dart b/packages/altive_lints/example/lints/avoid_hardcoded_color_test.dart new file mode 100644 index 0000000..66c98ea --- /dev/null +++ b/packages/altive_lints/example/lints/avoid_hardcoded_color_test.dart @@ -0,0 +1,28 @@ +// Check the `avoid_hardcoded_color` rule. +// +// This file ends with the name `_test.dart`, +// so it should be exempt from the warning. + +import 'package:flutter/material.dart'; + +class MyWidget extends StatelessWidget { + const MyWidget({super.key}); + + @override + Widget build(BuildContext context) { + return Column( + children: [ + const ColoredBox(color: Color(0xFF00FF00)), + const ColoredBox(color: Color.fromRGBO(0, 255, 0, 1)), + 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), + ); diff --git a/packages/altive_lints/example/test/avoid_hardcoded_color.dart b/packages/altive_lints/example/test/avoid_hardcoded_color.dart new file mode 100644 index 0000000..9afecd8 --- /dev/null +++ b/packages/altive_lints/example/test/avoid_hardcoded_color.dart @@ -0,0 +1,27 @@ +// Check the `avoid_hardcoded_color` rule. +// +// It should exclude warnings for the entire `test` directory. + +import 'package:flutter/material.dart'; + +class MyWidget extends StatelessWidget { + const MyWidget({super.key}); + + @override + Widget build(BuildContext context) { + return Column( + children: [ + const ColoredBox(color: Color(0xFF00FF00)), + const ColoredBox(color: Color.fromRGBO(0, 255, 0, 1)), + 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), + ); diff --git a/packages/altive_lints/example/test/avoid_hardcoded_japanese_test.dart b/packages/altive_lints/example/test/avoid_hardcoded_japanese.dart similarity index 100% rename from packages/altive_lints/example/test/avoid_hardcoded_japanese_test.dart rename to packages/altive_lints/example/test/avoid_hardcoded_japanese.dart