Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Commit 7e1703b

Browse files
authored
add lint use_string_in_part_of_directives (#3567)
* add lint use_string_in_part_of_directives * migrate test to reflective_test_loader style
1 parent d5d3347 commit 7e1703b

File tree

5 files changed

+114
-1
lines changed

5 files changed

+114
-1
lines changed

example/all.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ linter:
203203
- use_rethrow_when_possible
204204
- use_setters_to_change_properties
205205
- use_string_buffers
206+
- use_string_in_part_of_directives
206207
- use_super_parameters
207208
- use_test_throws_matchers
208209
- use_to_and_as_if_applicable

lib/src/rules.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ import 'rules/use_raw_strings.dart';
208208
import 'rules/use_rethrow_when_possible.dart';
209209
import 'rules/use_setters_to_change_properties.dart';
210210
import 'rules/use_string_buffers.dart';
211+
import 'rules/use_string_in_part_of_directives.dart';
211212
import 'rules/use_super_parameters.dart';
212213
import 'rules/use_test_throws_matchers.dart';
213214
import 'rules/use_to_and_as_if_applicable.dart';
@@ -424,6 +425,7 @@ void registerLintRules({bool inTestMode = false}) {
424425
..register(UseRawStrings())
425426
..register(UseSettersToChangeAProperty())
426427
..register(UseStringBuffers())
428+
..register(UseStringInPartOfDirectives())
427429
..register(UseSuperParameters())
428430
..register(UseTestThrowsMatchers())
429431
..register(UseToAndAsIfApplicable())
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analyzer/dart/ast/ast.dart';
6+
import 'package:analyzer/dart/ast/visitor.dart';
7+
8+
import '../analyzer.dart';
9+
10+
const _desc = r'Use string in part of directives.';
11+
12+
const _details = r'''
13+
14+
From [effective dart](https://dart.dev/guides/language/effective-dart/usage#do-use-strings-in-part-of-directives):
15+
16+
**DO** use strings in `part of` directives.
17+
18+
**BAD:**
19+
20+
```dart
21+
part of my_library;
22+
```
23+
24+
**GOOD:**
25+
26+
```dart
27+
part of '../../my_library.dart';
28+
```
29+
30+
''';
31+
32+
class UseStringInPartOfDirectives extends LintRule {
33+
UseStringInPartOfDirectives()
34+
: super(
35+
name: 'use_string_in_part_of_directives',
36+
description: _desc,
37+
details: _details,
38+
group: Group.style,
39+
);
40+
41+
@override
42+
void registerNodeProcessors(
43+
NodeLintRegistry registry,
44+
LinterContext context,
45+
) {
46+
var visitor = _Visitor(this);
47+
registry.addPartOfDirective(this, visitor);
48+
}
49+
}
50+
51+
class _Visitor extends SimpleAstVisitor<void> {
52+
_Visitor(this.rule);
53+
54+
final LintRule rule;
55+
56+
@override
57+
void visitPartOfDirective(PartOfDirective node) {
58+
if (node.libraryName != null) {
59+
rule.reportLint(node);
60+
}
61+
}
62+
}

test/rule_test_support.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,10 @@ class PubPackageResolutionTest extends _ContextResolutionTest {
237237
/// The path that is not in [workspaceRootPath], contains external packages.
238238
String get packagesRootPath => '/packages';
239239

240+
String get testFileName => 'test.dart';
241+
240242
@override
241-
String get testFilePath => '$testPackageLibPath/test.dart';
243+
String get testFilePath => '$testPackageLibPath/$testFileName';
242244

243245
String? get testPackageLanguageVersion => null;
244246

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:test_reflective_loader/test_reflective_loader.dart';
6+
7+
import '../rule_test_support.dart';
8+
9+
main() {
10+
defineReflectiveSuite(() {
11+
defineReflectiveTests(UseStringInPartOfDirectivesTest);
12+
});
13+
}
14+
15+
@reflectiveTest
16+
class UseStringInPartOfDirectivesTest extends LintRuleTest {
17+
@override
18+
bool get addMetaPackageDep => true;
19+
20+
@override
21+
String get lintRule => 'use_string_in_part_of_directives';
22+
23+
test_part_of_with_string() async {
24+
newFile2('$testPackageRootPath/lib/lib.dart', '''
25+
part '$testFileName';
26+
''');
27+
await assertNoDiagnostics(r'''
28+
part of 'lib.dart';
29+
''');
30+
}
31+
32+
test_part_of_with_library_name() async {
33+
newFile2('$testPackageRootPath/lib/lib.dart', '''
34+
library lib;
35+
part '$testFileName';
36+
''');
37+
await assertDiagnostics(
38+
r'''
39+
part of lib;
40+
''',
41+
[
42+
lint('use_string_in_part_of_directives', 0, 12),
43+
],
44+
);
45+
}
46+
}

0 commit comments

Comments
 (0)