Skip to content

Commit 4d11d8f

Browse files
pqCommit Queue
authored andcommitted
[element model] migrate prefer_relative_imports
Bug: https://github.com/dart-lang/linter/issues/5099 Change-Id: I7977c6ef77dc0697830a78a440713f9a8d5a848f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391485 Auto-Submit: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 94e5df6 commit 4d11d8f

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

pkg/linter/analyzer_use_new_elements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ lib/src/rules/prefer_iterable_whereType.dart
139139
lib/src/rules/prefer_mixin.dart
140140
lib/src/rules/prefer_null_aware_method_calls.dart
141141
lib/src/rules/prefer_null_aware_operators.dart
142+
lib/src/rules/prefer_relative_imports.dart
142143
lib/src/rules/prefer_single_quotes.dart
143144
lib/src/rules/prefer_spread_collections.dart
144145
lib/src/rules/prefer_typing_uninitialized_variables.dart
@@ -203,6 +204,7 @@ lib/src/rules/valid_regexps.dart
203204
lib/src/test_utilities/analyzer_utils.dart
204205
lib/src/test_utilities/annotation.dart
205206
lib/src/test_utilities/formatter.dart
207+
lib/src/test_utilities/lint_driver.dart
206208
lib/src/test_utilities/test_linter.dart
207209
lib/src/test_utilities/test_resource_provider.dart
208210
lib/src/util/ascii_utils.dart

pkg/linter/lib/src/rules/prefer_relative_imports.dart

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/dart/ast/visitor.dart';
7-
import 'package:analyzer/dart/element/element.dart';
7+
import 'package:analyzer/dart/element/element2.dart';
88
import 'package:path/path.dart' as path;
99

1010
import '../analyzer.dart';
11+
import '../extensions.dart';
1112
import 'implementation_imports.dart' show samePackage;
1213

1314
const _desc = r'Prefer relative imports for files in `lib/`.';
@@ -31,34 +32,35 @@ class PreferRelativeImports extends LintRule {
3132
NodeLintRegistry registry, LinterContext context) {
3233
if (!context.isInLibDir) return;
3334

34-
var visitor = _Visitor(this, context);
35+
var sourceUri = context.libraryElement2?.uri;
36+
if (sourceUri == null) return;
37+
38+
var visitor = _Visitor(this, sourceUri, context);
3539
registry.addImportDirective(this, visitor);
3640
}
3741
}
3842

3943
class _Visitor extends SimpleAstVisitor<void> {
4044
final PreferRelativeImports rule;
45+
final Uri sourceUri;
4146
final LinterContext context;
4247

43-
_Visitor(this.rule, this.context);
48+
_Visitor(this.rule, this.sourceUri, this.context);
4449

4550
bool isPackageSelfReference(ImportDirective node) {
46-
var uri = node.element?.uri;
47-
if (uri is! DirectiveUriWithSource) {
48-
return false;
49-
}
51+
if (node.libraryImport?.uri case DirectiveUriWithSource importedLibrary) {
52+
var importUri = importedLibrary.relativeUri;
53+
if (!importUri.isScheme('package')) return false;
5054

51-
// Is it a package: import?
52-
var importUri = uri.relativeUri;
53-
if (!importUri.isScheme('package')) return false;
55+
if (!samePackage(importUri, sourceUri)) return false;
5456

55-
var sourceUri = node.element?.source.uri;
56-
if (!samePackage(importUri, sourceUri)) return false;
57+
// TODO(pq): context.package.contains(source) should work (but does not)
58+
var packageRoot = context.package?.root;
59+
return packageRoot != null &&
60+
path.isWithin(packageRoot, importedLibrary.source.fullName);
61+
}
5762

58-
// TODO(pq): context.package.contains(source) should work (but does not)
59-
var packageRoot = context.package?.root;
60-
return packageRoot != null &&
61-
path.isWithin(packageRoot, uri.source.fullName);
63+
return false;
6264
}
6365

6466
@override

0 commit comments

Comments
 (0)