Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: disable avoid_hardcoded_color lint if it's a test file #83

Merged
merged 2 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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),
);
27 changes: 27 additions & 0 deletions packages/altive_lints/example/test/avoid_hardcoded_color.dart
Original file line number Diff line number Diff line change
@@ -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),
);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed _test from the filename because it does not confirm the test directory if _test is added to the filename!

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
///
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions packages/altive_lints/lib/src/utils/files_utils.dart
Original file line number Diff line number Diff line change
@@ -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');
}
3 changes: 3 additions & 0 deletions packages/altive_lints/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
35 changes: 35 additions & 0 deletions packages/altive_lints/test/src/utils/files_utils_test.dart
Original file line number Diff line number Diff line change
@@ -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);
});
});
}
Loading