Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit 9b79547

Browse files
authored
feat: improve prefer-correct-type-name rule (#627)
* feat: improve `prefer-correct-type-name` rule * chore: update example
1 parent 3a25d26 commit 9b79547

File tree

6 files changed

+64
-16
lines changed

6 files changed

+64
-16
lines changed

CHANGELOG.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@
33
## Unreleased
44

55
* feat: add static code diagnostics `avoid-global-state`.
6-
* chore: migrate from deprecated api
7-
* fix: fixed issue with type check in prefer-match-file-name
8-
* fix: stabilized command usage tests
9-
* doc: add flutter favorite badge
10-
* chore: disable github_checks annotations from codecov
11-
* chore: activate language strict rules
12-
* fix: add missing severity for rules
13-
* feat: facelift console reporters
6+
* chore: migrate from deprecated api.
7+
* fix: fixed issue with type check in prefer-match-file-name.
8+
* fix: stabilized command usage tests.
9+
* doc: add flutter favorite badge.
10+
* chore: disable github_checks annotations from codecov.
11+
* chore: activate language strict rules.
12+
* fix: add missing severity for rules.
13+
* feat: facelift console reporters.
1414
* chore: restrict `analyzer` version to `>=2.4.0 <3.1.0`.
1515
* chore: restrict `analyzer_plugin` version to `>=0.8.0 <0.10.0`.
1616
* feat: support extensions for check-unused-l10n.
17+
* feat: improve `prefer-correct-type-name` rule.
1718

1819
## 4.8.1
1920

20-
* feat: add cli options for fatal exit if unused files or l10n are found
21+
* feat: add cli options for fatal exit if unused files or l10n are found.
2122

2223
## 4.8.0
2324

lib/src/analyzers/lint_analyzer/rules/rules_list/prefer_correct_type_name/validator.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class _Validator {
1212
});
1313

1414
bool isValid(String name) =>
15+
name.isEmpty ||
1516
exceptions.contains(name) ||
1617
(isUpperCase(name) &&
1718
withoutUnderscore(name).length >= minLength &&

test/src/analyzers/lint_analyzer/rules/rules_list/prefer_correct_type_name/examples/class_example.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class _ex {
1515

1616
// LINT Class with short name
1717
class ex {
18-
_ex();
18+
ex();
1919
}
2020

2121
// LINT Private class with long name

test/src/analyzers/lint_analyzer/rules/rules_list/prefer_correct_type_name/prefer_correct_type_name_rule_test.dart

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import '../../../../../helpers/rule_test_helper.dart';
77
const _path = 'prefer_correct_type_name/examples';
88
const _classExample = '$_path/class_example.dart';
99
const _enumExample = '$_path/enum_example.dart';
10-
const _mixinExample = '$_path/mixin_example.dart';
1110
const _extensionExample = '$_path/extension_example.dart';
11+
const _mixinExample = '$_path/mixin_example.dart';
1212

1313
void main() {
1414
group('PreferCorrectTypeNameRule', () {
@@ -25,6 +25,7 @@ void main() {
2525

2626
test('reports about all found issues in class_example.dart', () async {
2727
final unit = await RuleTestHelper.resolveFromFile(_classExample);
28+
2829
final issues = PreferCorrectTypeNameRule({
2930
'max-length': 15,
3031
'min-length': 3,
@@ -33,10 +34,10 @@ void main() {
3334

3435
RuleTestHelper.verifyIssues(
3536
issues: issues,
36-
startOffsets: [52, 124, 196, 250, 310, 397],
37+
startOffsets: [52, 124, 196, 250, 309, 396],
3738
startLines: [2, 7, 12, 17, 22, 27],
3839
startColumns: [7, 7, 7, 7, 7, 7],
39-
endOffsets: [59, 132, 199, 252, 330, 416],
40+
endOffsets: [59, 132, 199, 252, 329, 415],
4041
locationTexts: [
4142
'example',
4243
'_example',
@@ -58,6 +59,7 @@ void main() {
5859

5960
test('reports about all found issues in enum_example.dart', () async {
6061
final unit = await RuleTestHelper.resolveFromFile(_enumExample);
62+
6163
final issues = PreferCorrectTypeNameRule({
6264
'max-length': 15,
6365
'min-length': 3,
@@ -91,6 +93,7 @@ void main() {
9193

9294
test('reports about all found issues in extension_example.dart', () async {
9395
final unit = await RuleTestHelper.resolveFromFile(_extensionExample);
96+
9497
final issues = PreferCorrectTypeNameRule({
9598
'max-length': 15,
9699
'min-length': 3,
@@ -124,6 +127,7 @@ void main() {
124127

125128
test('reports about all found issues in mixin_example.dart', () async {
126129
final unit = await RuleTestHelper.resolveFromFile(_mixinExample);
130+
127131
final issues = PreferCorrectTypeNameRule({
128132
'max-length': 15,
129133
'min-length': 3,
@@ -154,5 +158,33 @@ void main() {
154158
],
155159
);
156160
});
161+
162+
test('works with invalid files', () async {
163+
final rule = PreferCorrectTypeNameRule();
164+
165+
final classExampleUnit = await RuleTestHelper.createAndResolveFromFile(
166+
content: 'class ',
167+
filePath: '$_path/invalid_class_example.dart',
168+
);
169+
expect(rule.check(classExampleUnit), isEmpty);
170+
171+
final classEnumUnit = await RuleTestHelper.createAndResolveFromFile(
172+
content: 'enum ',
173+
filePath: '$_path/invalid_enum_example.dart',
174+
);
175+
expect(rule.check(classEnumUnit), isEmpty);
176+
177+
final classExtensionUnit = await RuleTestHelper.createAndResolveFromFile(
178+
content: 'extension ',
179+
filePath: '$_path/invalid_extension_example.dart',
180+
);
181+
expect(rule.check(classExtensionUnit), isEmpty);
182+
183+
final classMixinUnit = await RuleTestHelper.createAndResolveFromFile(
184+
content: 'mixin ',
185+
filePath: '$_path/invalid_mixin_example.dart',
186+
);
187+
expect(rule.check(classMixinUnit), isEmpty);
188+
});
157189
});
158190
}

test/src/helpers/file_resolver.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import 'package:dart_code_metrics/src/analyzers/lint_analyzer/models/internal_re
66
import 'package:path/path.dart';
77

88
class FileResolver {
9-
static Future<InternalResolvedUnitResult> resolve(
10-
String filePath,
11-
) async {
9+
static Future<InternalResolvedUnitResult> resolve(String filePath) async {
1210
final file = File(filePath);
1311

1412
if (!file.existsSync()) {

test/src/helpers/rule_test_helper.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:io';
2+
13
import 'package:dart_code_metrics/src/analyzers/lint_analyzer/models/internal_resolved_unit_result.dart';
24
import 'package:dart_code_metrics/src/analyzers/lint_analyzer/models/issue.dart';
35
import 'package:dart_code_metrics/src/analyzers/lint_analyzer/models/severity.dart';
@@ -15,6 +17,20 @@ class RuleTestHelper {
1517
return FileResolver.resolve(fullPath);
1618
}
1719

20+
static Future<InternalResolvedUnitResult> createAndResolveFromFile({
21+
required String content,
22+
required String filePath,
23+
}) async {
24+
final fullPath =
25+
'test/src/analyzers/lint_analyzer/rules/rules_list/$filePath';
26+
27+
final file = File(fullPath)..writeAsStringSync(content);
28+
final result = await FileResolver.resolve(fullPath);
29+
file.deleteSync();
30+
31+
return result;
32+
}
33+
1834
static void verifyInitialization({
1935
required Iterable<Issue> issues,
2036
required String ruleId,

0 commit comments

Comments
 (0)