Skip to content

Commit

Permalink
feat: disable avoid_hardcoded_color lint if it's a test file (#83)
Browse files Browse the repository at this point in the history
* feat: disable avoid_hardcoded_color lint if it's a test file

* test: add file for tests
  • Loading branch information
riscait authored Dec 24, 2024
1 parent 8126ddc commit 6003c83
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 4 deletions.
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),
);
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);
});
});
}

0 comments on commit 6003c83

Please sign in to comment.