-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add lint use_string_in_part_of_directives (dart-lang/linter#3567)
* add lint use_string_in_part_of_directives * migrate test to reflective_test_loader style
- Loading branch information
Showing
5 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/dart/ast/visitor.dart'; | ||
|
||
import '../analyzer.dart'; | ||
|
||
const _desc = r'Use string in part of directives.'; | ||
|
||
const _details = r''' | ||
From [effective dart](https://dart.dev/guides/language/effective-dart/usage#do-use-strings-in-part-of-directives): | ||
**DO** use strings in `part of` directives. | ||
**BAD:** | ||
```dart | ||
part of my_library; | ||
``` | ||
**GOOD:** | ||
```dart | ||
part of '../../my_library.dart'; | ||
``` | ||
'''; | ||
|
||
class UseStringInPartOfDirectives extends LintRule { | ||
UseStringInPartOfDirectives() | ||
: super( | ||
name: 'use_string_in_part_of_directives', | ||
description: _desc, | ||
details: _details, | ||
group: Group.style, | ||
); | ||
|
||
@override | ||
void registerNodeProcessors( | ||
NodeLintRegistry registry, | ||
LinterContext context, | ||
) { | ||
var visitor = _Visitor(this); | ||
registry.addPartOfDirective(this, visitor); | ||
} | ||
} | ||
|
||
class _Visitor extends SimpleAstVisitor<void> { | ||
_Visitor(this.rule); | ||
|
||
final LintRule rule; | ||
|
||
@override | ||
void visitPartOfDirective(PartOfDirective node) { | ||
if (node.libraryName != null) { | ||
rule.reportLint(node); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:test_reflective_loader/test_reflective_loader.dart'; | ||
|
||
import '../rule_test_support.dart'; | ||
|
||
main() { | ||
defineReflectiveSuite(() { | ||
defineReflectiveTests(UseStringInPartOfDirectivesTest); | ||
}); | ||
} | ||
|
||
@reflectiveTest | ||
class UseStringInPartOfDirectivesTest extends LintRuleTest { | ||
@override | ||
bool get addMetaPackageDep => true; | ||
|
||
@override | ||
String get lintRule => 'use_string_in_part_of_directives'; | ||
|
||
test_part_of_with_string() async { | ||
newFile2('$testPackageRootPath/lib/lib.dart', ''' | ||
part '$testFileName'; | ||
'''); | ||
await assertNoDiagnostics(r''' | ||
part of 'lib.dart'; | ||
'''); | ||
} | ||
|
||
test_part_of_with_library_name() async { | ||
newFile2('$testPackageRootPath/lib/lib.dart', ''' | ||
library lib; | ||
part '$testFileName'; | ||
'''); | ||
await assertDiagnostics( | ||
r''' | ||
part of lib; | ||
''', | ||
[ | ||
lint('use_string_in_part_of_directives', 0, 12), | ||
], | ||
); | ||
} | ||
} |