Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.4.0

- Added `allow_with_comments` parameter for `no_empty_block` lint.
- Added `exclude_entity` parameter for `prefer_match_feli_name` lint.
Comment thread
yurii-prykhodko-solid marked this conversation as resolved.
Resolved

## 0.3.0

Expand Down
3 changes: 2 additions & 1 deletion lib/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ custom_lint:

- prefer_first
- prefer_last
- prefer_match_file_name
- prefer_match_file_name:
Comment thread
yurii-prykhodko-solid marked this conversation as resolved.
Resolved
ignore_extensions: true
Comment thread
yurii-prykhodko-solid marked this conversation as resolved.
Resolved
Comment thread
yurii-prykhodko-solid marked this conversation as resolved.
Resolved
- proper_super_calls

linter:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// A data model class that represents the "prefer factmatch file name" input
/// parameters
class PreferMatchFileNameParameters {
/// A list of methods that should be excluded from the lint.
final Iterable<String> excludeEntity;

static const bool _defaultIgnoreExtensionsValue = false;

/// Constructor for [PreferMatchFileNameParameters] model
const PreferMatchFileNameParameters({
required this.excludeEntity,
});

/// Method for creating from json data
factory PreferMatchFileNameParameters.fromJson(Map<String, Object?> json) =>
PreferMatchFileNameParameters(
excludeEntity: (json['exclude_entity'] as Iterable?)
?.map((e) => e.toString())
.toList() ??
[],
);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:analyzer/error/listener.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:path/path.dart' as p;
import 'package:solid_lints/src/lints/prefer_match_file_name/models/prefer_match_file_name_parameters.dart';
import 'package:solid_lints/src/lints/prefer_match_file_name/visitors/prefer_match_file_name_visitor.dart';
import 'package:solid_lints/src/models/rule_config.dart';
import 'package:solid_lints/src/models/solid_lint_rule.dart';
Expand Down Expand Up @@ -48,7 +49,8 @@ import 'package:solid_lints/src/utils/node_utils.dart';
/// class SomethingPublic {} // OK
/// ```
///
class PreferMatchFileNameRule extends SolidLintRule {
class PreferMatchFileNameRule
extends SolidLintRule<PreferMatchFileNameParameters> {
/// This lint rule represents the error if iterable
/// access can be simplified.
static const String lintName = 'prefer_match_file_name';
Expand All @@ -62,6 +64,7 @@ class PreferMatchFileNameRule extends SolidLintRule {
final config = RuleConfig(
configs: configs,
name: lintName,
paramsParser: PreferMatchFileNameParameters.fromJson,
problemMessage: (value) =>
'File name does not match with first declared element name.',
);
Expand All @@ -76,7 +79,11 @@ class PreferMatchFileNameRule extends SolidLintRule {
CustomLintContext context,
) {
context.registry.addCompilationUnit((node) {
final visitor = PreferMatchFileNameVisitor();
final excludedEntities = config.parameters.excludeEntity;

final visitor = PreferMatchFileNameVisitor(
excludedEntities: excludedEntities,
);

node.accept(visitor);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:solid_lints/src/common/parameters/excluded_identifiers_list_parameter.dart';
import 'package:solid_lints/src/lints/prefer_match_file_name/models/declaration_token_info.dart';

/// The AST visitor that will collect all Class, Enum, Extension and Mixin
/// declarations
class PreferMatchFileNameVisitor extends RecursiveAstVisitor<void> {
final _declarations = <DeclarationTokenInfo>[];

/// Iterable that contains the name of entity (or entities) that should
/// be ignored
final Iterable<String> excludedEntities;

/// Constructor of [PreferMatchFileNameVisitor] class
PreferMatchFileNameVisitor({
required this.excludedEntities,
});

/// List of all declarations
Iterable<DeclarationTokenInfo> get declarations => _declarations
..sort(
Expand All @@ -16,17 +26,38 @@ class PreferMatchFileNameVisitor extends RecursiveAstVisitor<void> {
(a, b) => _publicDeclarationsFirst(a, b) ?? _byDeclarationOrder(a, b),
);

bool _shouldIgnore(Declaration node) {
if (excludedEntities.isEmpty) return false;

if (node is ClassDeclaration && excludedEntities.contains('class')) {
return true;
} else if (node is MixinDeclaration && excludedEntities.contains('mixin')) {
return true;
} else if (node is EnumDeclaration && excludedEntities.contains('enum')) {
return true;
} else if (node is ExtensionDeclaration &&
excludedEntities.contains('extension')) {
return true;
}

return false;
}
Comment thread
yurii-prykhodko-solid marked this conversation as resolved.
Resolved

@override
void visitClassDeclaration(ClassDeclaration node) {
super.visitClassDeclaration(node);

if (_shouldIgnore(node)) return;
Comment thread
yurii-prykhodko-solid marked this conversation as resolved.
Resolved

_declarations.add((token: node.name, parent: node));
}

@override
void visitExtensionDeclaration(ExtensionDeclaration node) {
super.visitExtensionDeclaration(node);

if (_shouldIgnore(node)) return;

final name = node.name;
if (name != null) {
_declarations.add((token: name, parent: node));
Expand All @@ -37,13 +68,17 @@ class PreferMatchFileNameVisitor extends RecursiveAstVisitor<void> {
void visitMixinDeclaration(MixinDeclaration node) {
super.visitMixinDeclaration(node);

if (_shouldIgnore(node)) return;

_declarations.add((token: node.name, parent: node));
}

@override
void visitEnumDeclaration(EnumDeclaration node) {
super.visitEnumDeclaration(node);

if (_shouldIgnore(node)) return;

_declarations.add((token: node.name, parent: node));
}

Expand Down
5 changes: 5 additions & 0 deletions lint_test/prefer_match_file_name_ignore_extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// ignore_for_file: unused_element, unused_field

/// Check if the `prefer_match_file_name` rule ignored for extensions
// expect_lint: prefer_match_file_name
extension DefaultExtension on String {}
Comment thread
yurii-prykhodko-solid marked this conversation as resolved.
Resolved