Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
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.
Outdated
ignore_extensions: true
Comment thread
yurii-prykhodko-solid marked this conversation as resolved.
Outdated
Comment thread
yurii-prykhodko-solid marked this conversation as resolved.
Outdated
- proper_super_calls

linter:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// A data model class that represents the "prefer factmatch file name" input
/// parameters
class PreferMatchFileNameParameters {
/// A variable that indicates whether to ignore extensions
final bool ignoreExtensions;

static const bool _defaultIgnoreExtensionsValue = false;

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

/// Method for creating from json data
factory PreferMatchFileNameParameters.fromJson(Map<String, Object?> json) =>
PreferMatchFileNameParameters(
ignoreExtensions:
json['ignore_extensions'] as bool? ?? _defaultIgnoreExtensionsValue,
);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
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';
import 'package:solid_lints/src/utils/node_utils.dart';

import '../number_of_parameters/models/number_of_parameters_parameters.dart';

/// Warns about a mismatch between file name and first declared element inside.
///
/// This improves navigation by matching file content and file name.
Expand Down Expand Up @@ -48,7 +51,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 +66,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 +81,9 @@ class PreferMatchFileNameRule extends SolidLintRule {
CustomLintContext context,
) {
context.registry.addCompilationUnit((node) {
final visitor = PreferMatchFileNameVisitor();
final isIgnored = config.parameters.ignoreExtensions;

final visitor = PreferMatchFileNameVisitor(ignoreExtensions: isIgnored);

node.accept(visitor);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import 'package:solid_lints/src/lints/prefer_match_file_name/models/declaration_
class PreferMatchFileNameVisitor extends RecursiveAstVisitor<void> {
final _declarations = <DeclarationTokenInfo>[];

/// Variable for making sure if extensions should be ignored
final bool ignoreExtensions;

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

/// List of all declarations
Iterable<DeclarationTokenInfo> get declarations => _declarations
..sort(
Expand All @@ -27,9 +35,11 @@ class PreferMatchFileNameVisitor extends RecursiveAstVisitor<void> {
void visitExtensionDeclaration(ExtensionDeclaration node) {
super.visitExtensionDeclaration(node);

final name = node.name;
if (name != null) {
_declarations.add((token: name, parent: node));
if (!ignoreExtensions) {
Comment thread
yurii-prykhodko-solid marked this conversation as resolved.
Outdated
final name = node.name;
if (name != null) {
_declarations.add((token: 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 {}