Skip to content
Open
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ Add an `analysis_options.yaml` file under the `test/` directory, and include the

```yaml
include: package:solid_lints/analysis_options_test.yaml

analyzer:
plugins:
- custom_lint
```

Then you can see suggestions in your IDE or you can run checks manually:
Expand Down
4 changes: 4 additions & 0 deletions lib/analysis_options_test.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
include: package:solid_lints/analysis_options.yaml

analyzer:
plugins:
- custom_lint
Comment on lines +3 to +5

Choose a reason for hiding this comment

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

medium

This analyzer section appears to be redundant, as it is already defined in package:solid_lints/analysis_options.yaml, which is included in this file. The analyzer's configuration merging should handle this inheritance.

If this duplication is intentional—perhaps as a safeguard against issues with nested include directives in some tooling—it would be beneficial to add a comment explaining why it's necessary. This would improve maintainability by preventing future developers from removing it as dead code.


custom_lint:
rules:
# Tests usually organized in one large main() function making this rule not applicable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ your `debugPrint` call in a `!kReleaseMode` check.""",
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class AvoidFinalWithGetterRule extends SolidLintRule {
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class AvoidGlobalStateRule extends SolidLintRule {
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class AvoidLateKeywordRule extends SolidLintRule<AvoidLateKeywordParameters> {
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class AvoidNonNullAssertionRule extends SolidLintRule {
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class AvoidReturningWidgetsRule
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class AvoidUnnecessarySetStateRule extends SolidLintRule {
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class AvoidUnnecessaryTypeAssertions extends SolidLintRule {
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class AvoidUnnecessaryTypeCastsRule extends SolidLintRule {
}

@override
void run(
void runRule(
CustomLintResolver resolver,
error_listener.ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AvoidUnrelatedTypeAssertionsRule extends SolidLintRule {
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class AvoidUnusedParametersRule extends SolidLintRule<AvoidUnusedParameters> {
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/lints/avoid_using_api/avoid_using_api_rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class AvoidUsingApiRule extends SolidLintRule<AvoidUsingApiParameters> {
}

@override
Future<void> run(
Future<void> runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CyclomaticComplexityRule
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class DoubleLiteralFormatRule extends SolidLintRule {
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class FunctionLinesOfCodeRule
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/lints/member_ordering/member_ordering_rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class MemberOrderingRule extends SolidLintRule<MemberOrderingParameters> {
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class NamedParametersOrderingRule
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class NewlineBeforeReturnRule extends SolidLintRule {
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/lints/no_empty_block/no_empty_block_rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class NoEmptyBlockRule extends SolidLintRule<NoEmptyBlockParameters> {
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class NoEqualThenElseRule extends SolidLintRule {
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/lints/no_magic_number/no_magic_number_rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class NoMagicNumberRule extends SolidLintRule<NoMagicNumberParameters> {
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class NumberOfParametersRule
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class PreferConditionalExpressionsRule
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class PreferEarlyReturnRule extends SolidLintRule {
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/lints/prefer_first/prefer_first_rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class PreferFirstRule extends SolidLintRule {
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/lints/prefer_last/prefer_last_rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class PreferLastRule extends SolidLintRule {
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class PreferMatchFileNameRule
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class ProperSuperCallsRule extends SolidLintRule {
}

@override
void run(
void runRule(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
Expand Down
75 changes: 74 additions & 1 deletion lib/src/models/solid_lint_rule.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import 'dart:io';

import 'package:analyzer/error/listener.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:solid_lints/src/models/rule_config.dart';
import 'package:solid_lints/src/utils/path_utils.dart';
import 'package:yaml/yaml.dart';

/// A base class for emitting information about
/// issues with user's `.dart` files.
Expand All @@ -11,6 +16,74 @@ abstract class SolidLintRule<T extends Object?> extends DartLintRule {
/// defined custom parameters.
final RuleConfig<T> config;

/// A flag which indicates whether this rule was enabled by the user.
/// A flag which indicates whether this rule was enabled by the user (globally).
bool get enabled => config.enabled;

@override
void run(CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,) {
if (!isEnabledForFile(resolver.path)) return;
runRule(resolver, reporter, context);
}

/// Override this method instead of run() in your rule implementations
void runRule(CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,);

/// Determines if the rule should run for the given file.
bool isEnabledForFile(String filePath) {
if (!enabled) return false;
if (_isTestFile(filePath) && _isRuleDisabledInLocalConfig(filePath)) {
return false;
}
return true;
}

/// Checking if the file is a test file.
bool _isTestFile(String filePath) {
return filePath.contains('/test/') ||
filePath.contains(r'\test\') ||
filePath.endsWith('_test.dart');
}

/// Check if rule is disabled in local analysis_options.yaml config.
bool _isRuleDisabledInLocalConfig(String filePath) {
final configPath = findAnalysisOptionsForFile(filePath);
if (configPath == null) return false;

try {
final file = File(configPath);
if (!file.existsSync()) return false;

final content = file.readAsStringSync();
final yaml = loadYaml(content) as Map<String, dynamic>?;

if (yaml == null) return false;

final customLint = yaml['custom_lint'] as Map<String, dynamic>?;
if (customLint == null) return false;

final rules = customLint['rules'] as List?;
if (rules == null) return false;

/// Searching rule in rules list
for (final rule in rules) {
if (rule is String && rule == config.name) {
return false;
} else if (rule is Map) {
final key = rule.keys.first as String;
if (key == config.name) {
final value = rule.values.first;
return value == false;
}
}
}

return false;
} catch (e) {
return false;
}
}
}
Loading