From fc23c9cb4de194bf30634df4f821666c8d465e4e Mon Sep 17 00:00:00 2001 From: Yurii Prykhodko Date: Fri, 22 Sep 2023 13:59:04 +0300 Subject: [PATCH] Typos, naming --- .../prefer_match_file_name_rule.dart | 53 +++++++++++-------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/lib/lints/prefer_match_file_name/prefer_match_file_name_rule.dart b/lib/lints/prefer_match_file_name/prefer_match_file_name_rule.dart index ba4e0b2a..53a67961 100644 --- a/lib/lints/prefer_match_file_name/prefer_match_file_name_rule.dart +++ b/lib/lints/prefer_match_file_name/prefer_match_file_name_rule.dart @@ -42,33 +42,40 @@ class PreferMatchFileNameRule extends SolidLintRule { if (visitor.declarations.isEmpty) return; - final info = visitor.declarations.first; - if (!_hasMatchName(resolver.source.fullName, info.token.lexeme)) { - final nodeType = humanReadableNodeType(info.parent).toLowerCase(); + final firstDeclaration = visitor.declarations.first; - reporter.reportErrorForToken( - LintCode( - name: lintName, - problemMessage: - 'File name does not match with first $nodeType name.', - ), - info.token, - ); - } + if (_doNormalizedNamesMatch( + resolver.source.fullName, + firstDeclaration.token.lexeme, + )) return; + + final nodeType = + humanReadableNodeType(firstDeclaration.parent).toLowerCase(); + + reporter.reportErrorForToken( + LintCode( + name: lintName, + problemMessage: 'File name does not match with first $nodeType name.', + ), + firstDeclaration.token, + ); }); } - bool _hasMatchName(String path, String identifierName) { - final identifierNameFormatted = - identifierName.replaceAll(_onlySymbolsRegex, '').toLowerCase(); - - final fileNameFormatted = p - .basename(path) - .split('.') - .first - .replaceAll(_onlySymbolsRegex, '') - .toLowerCase(); + bool _doNormalizedNamesMatch(String path, String identifierName) { + final fileName = _normalizePath(path); + final dartIdentifier = _normalizeDartIdentifierName(identifierName); - return identifierNameFormatted == fileNameFormatted; + return fileName == dartIdentifier; } + + String _normalizePath(String s) => p + .basename(s) + .split('.') + .first + .replaceAll(_onlySymbolsRegex, '') + .toLowerCase(); + + String _normalizeDartIdentifierName(String s) => + s.replaceAll(_onlySymbolsRegex, '').toLowerCase(); }