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

add lint use_string_in_part_of_directives #3567

Merged
merged 2 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions example/all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ linter:
- use_rethrow_when_possible
- use_setters_to_change_properties
- use_string_buffers
- use_string_in_part_of_directives
- use_super_parameters
- use_test_throws_matchers
- use_to_and_as_if_applicable
Expand Down
2 changes: 2 additions & 0 deletions lib/src/rules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ import 'rules/use_raw_strings.dart';
import 'rules/use_rethrow_when_possible.dart';
import 'rules/use_setters_to_change_properties.dart';
import 'rules/use_string_buffers.dart';
import 'rules/use_string_in_part_of_directives.dart';
import 'rules/use_super_parameters.dart';
import 'rules/use_test_throws_matchers.dart';
import 'rules/use_to_and_as_if_applicable.dart';
Expand Down Expand Up @@ -424,6 +425,7 @@ void registerLintRules({bool inTestMode = false}) {
..register(UseRawStrings())
..register(UseSettersToChangeAProperty())
..register(UseStringBuffers())
..register(UseStringInPartOfDirectives())
..register(UseSuperParameters())
..register(UseTestThrowsMatchers())
..register(UseToAndAsIfApplicable())
Expand Down
62 changes: 62 additions & 0 deletions lib/src/rules/use_string_in_part_of_directives.dart
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);
}
}
}
4 changes: 3 additions & 1 deletion test/rule_test_support.dart
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,10 @@ class PubPackageResolutionTest extends _ContextResolutionTest {
/// The path that is not in [workspaceRootPath], contains external packages.
String get packagesRootPath => '/packages';

String get testFileName => 'test.dart';

@override
String get testFilePath => '$testPackageLibPath/test.dart';
String get testFilePath => '$testPackageLibPath/$testFileName';

String? get testPackageLanguageVersion => null;

Expand Down
46 changes: 46 additions & 0 deletions test/rules/use_string_in_part_of_directives.dart
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),
],
);
}
}