Skip to content

Commit

Permalink
[enhanced-parts] don’t report use_string_in_part_of_directives
Browse files Browse the repository at this point in the history
As this will produce a diagnostic, we don't want to double-report.

Fixes: dart-lang/linter#5026

Change-Id: Ia745875a35d3c83c6fecf2a892b65a91c91fbb4d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/376760
Commit-Queue: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
pq authored and Commit Queue committed Jul 19, 2024
1 parent 29091ee commit 7282f03
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,26 @@ class ReplaceWithPartOfUriTest extends FixProcessorLintTest {

Future<void> test_packageLib_nestedDirectory() async {
newFile('$testPackageLibPath/nested/a.dart', r'''
// @dart = 3.4
// (pre enhanced-parts)
library my.lib;
part '../test.dart';
''');

await resolveTestCode('''
// @dart = 3.4
// (pre enhanced-parts)
part of my.lib;
class A {}
''');

await assertHasFix('''
// @dart = 3.4
// (pre enhanced-parts)
part of 'nested/a.dart';
class A {}
Expand All @@ -44,19 +53,28 @@ class A {}

Future<void> test_packageLib_parentDirectory() async {
newFile('$testPackageLibPath/a.dart', r'''
// @dart = 3.4
// (pre enhanced-parts)
library my.lib;
part 'nested/test.dart';
''');

testFilePath = getFile('$testPackageLibPath/nested/test.dart').path;

await resolveTestCode('''
// @dart = 3.4
// (pre enhanced-parts)
part of my.lib;
class A {}
''');

await assertHasFix('''
// @dart = 3.4
// (pre enhanced-parts)
part of '../a.dart';
class A {}
Expand All @@ -65,17 +83,26 @@ class A {}

Future<void> test_packageLib_sameDirectory() async {
newFile('$testPackageLibPath/a.dart', r'''
// @dart = 3.4
// (pre enhanced-parts)
library my.lib;
part 'test.dart';
''');

await resolveTestCode('''
// @dart = 3.4
// (pre enhanced-parts)
part of my.lib;
class A {}
''');

await assertHasFix('''
// @dart = 3.4
// (pre enhanced-parts)
part of 'a.dart';
class A {}
Expand All @@ -84,19 +111,28 @@ class A {}

Future<void> test_packageLib_siblingDirectory() async {
newFile('$testPackageLibPath/first/a.dart', r'''
// @dart = 3.4
// (pre enhanced-parts)
library my.lib;
part '../second/test.dart';
''');

testFilePath = getFile('$testPackageLibPath/second/test.dart').path;

await resolveTestCode('''
// @dart = 3.4
// (pre enhanced-parts)
part of my.lib;
class A {}
''');

await assertHasFix('''
// @dart = 3.4
// (pre enhanced-parts)
part of '../first/a.dart';
class A {}
Expand Down
16 changes: 14 additions & 2 deletions pkg/linter/lib/src/rules/use_string_in_part_of_directives.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// 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/analysis/features.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/src/lint/linter.dart'; //ignore: implementation_imports

import '../analyzer.dart';

Expand Down Expand Up @@ -51,8 +53,10 @@ class UseStringInPartOfDirectives extends LintRule {
NodeLintRegistry registry,
LinterContext context,
) {
var visitor = _Visitor(this);
registry.addPartOfDirective(this, visitor);
if (!context.hasEnancedPartsFeatureEnabled) {
var visitor = _Visitor(this);
registry.addPartOfDirective(this, visitor);
}
}
}

Expand All @@ -68,3 +72,11 @@ class _Visitor extends SimpleAstVisitor<void> {
}
}
}

extension on LinterContext {
bool get hasEnancedPartsFeatureEnabled {
var self = this;
return self is LinterContextWithResolvedResults &&
self.libraryElement.featureSet.isEnabled(Feature.enhanced_parts);
}
}
3 changes: 3 additions & 0 deletions pkg/linter/test/rules/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ import 'use_rethrow_when_possible_test.dart' as use_rethrow_when_possible;
import 'use_setters_to_change_properties_test.dart'
as use_setters_to_change_properties;
import 'use_string_buffers_test.dart' as use_string_buffers;
import 'use_string_in_part_of_directives_test.dart'
as use_string_in_part_of_directives;
import 'use_super_parameters_test.dart' as use_super_parameters;
import 'use_test_throws_matchers_test.dart' as use_test_throws_matchers;
import 'use_to_and_as_if_applicable_test.dart' as use_to_and_as_if_applicable;
Expand Down Expand Up @@ -498,6 +500,7 @@ void main() {
use_rethrow_when_possible.main();
use_setters_to_change_properties.main();
use_string_buffers.main();
use_string_in_part_of_directives.main();
use_super_parameters.main();
use_test_throws_matchers.main();
use_to_and_as_if_applicable.main();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,31 @@ class UseStringInPartOfDirectivesTest extends LintRuleTest {

test_part_of_with_library_name() async {
newFile('$testPackageRootPath/lib/lib.dart', '''
library lib;
part '$testFileName';
''');
await assertNoDiagnostics(r'''
part of lib;
''');
}

test_part_of_with_library_name_preEnhancedParts() async {
newFile('$testPackageRootPath/lib/lib.dart', '''
// @dart = 3.4
// (pre enhanced-parts)
library lib;
part '$testFileName';
''');
await assertDiagnostics(
r'''
// @dart = 3.4
// (pre enhanced-parts)
part of lib;
''',
[
lint(0, 12),
lint(40, 12),
],
);
}
Expand Down

0 comments on commit 7282f03

Please sign in to comment.