Skip to content

Commit

Permalink
Elements. Add LibraryElement2.uri
Browse files Browse the repository at this point in the history
We used it often enough, and the question "what is the URI of the library" makes sense.

Change-Id: I58baebd7b32154ae7a2f90770e4edcade8a49a63
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395680
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed Nov 15, 2024
1 parent 3f7df09 commit 359aa74
Show file tree
Hide file tree
Showing 21 changed files with 29 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class OverrideHelper {
var interface = inheritanceManager.getInterface2(interfaceElement);
var interfaceMap = interface.map2;
var namesToOverride = _namesToOverride(
interfaceElement.library2.firstFragment.source.uri,
interfaceElement.library2.uri,
interface,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class ElementMatcher {
for (var libraryImport in part.libraryImports2) {
// TODO(brianwilkerson): Filter based on combinators to help avoid making
// invalid suggestions.
var uri = libraryImport.importedLibrary2?.firstFragment.source.uri;
var uri = libraryImport.importedLibrary2?.uri;
if (uri != null) {
// The [uri] is `null` if the literal string is not a valid URI.
importedUris.add(uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ extension on MethodDeclaration {
var enclosingElement = element.enclosingElement2;
if (enclosingElement is InterfaceElement2) {
var name = Name(
enclosingElement.library2.firstFragment.source.uri,
enclosingElement.library2.uri,
element.name3!,
);
return inheritanceManager.getInherited4(enclosingElement, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class MoveTopLevelToFile extends RefactoringProducer {
var hasShowCombinator =
import.combinators.whereType<ShowElementCombinator>().isNotEmpty;
builder.importLibrary(
library.firstFragment.source.uri,
library.uri,
prefix: import.prefix2?.element.name3,
showName: element.name3,
useShow: hasShowCombinator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class AbstractChangeMethodSignatureTest extends AbstractContextTest {

String _referenceToString(Reference reference) {
var selfLibrary = refactoringContext.resolvedLibraryResult.element2;
var selfUriStr = '${selfLibrary.firstFragment.source.uri}';
var selfUriStr = '${selfLibrary.uri}';

var name = reference.name;
if (name == selfUriStr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class SelectionCoverageTest {
var overridden = inheritanceManager.getOverridden4(
visitedNodeList.enclosingElement2 as InterfaceElement2,
Name(
visitedNodeList.library2!.firstFragment.source.uri,
visitedNodeList.library2!.uri,
visitedNodeList.name3!,
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ class FlutterDataCollector extends RecursiveAstVisitor<void> {
var childWidget = element.enclosingElement2.name3!;
var library = element.library2;
if (library != null &&
!library.firstFragment.source.uri.toString().startsWith(
'package:flutter/',
)) {
!library.uri.toString().startsWith('package:flutter/')) {
childWidget = 'user-defined';
}
data.recordWidgetCreation(childWidget, parentWidget);
Expand Down
5 changes: 5 additions & 0 deletions pkg/analyzer/lib/dart/element/element2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,11 @@ abstract class LibraryElement2 implements Element2, Annotatable {

/// The [TypeSystem] that is used in this library.
TypeSystem get typeSystem;

/// The canonical URI of the library.
///
/// This is the same URI as `firstFragment.source.uri` returns.
Uri get uri;
}

/// An `export` directive within a library fragment.
Expand Down
3 changes: 3 additions & 0 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6788,6 +6788,9 @@ class LibraryElementImpl extends ElementImpl
];
}

@override
Uri get uri => firstFragment.source.uri;

List<CompilationUnitElementImpl> get _partUnits {
var result = <CompilationUnitElementImpl>[];

Expand Down
3 changes: 1 addition & 2 deletions pkg/analyzer/lib/src/dart/element/inheritance_manager3.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1319,8 +1319,7 @@ class Name {
}

factory Name.forLibrary(LibraryElement2? library, String name) {
var uri = library?.firstFragment.source.uri;
return Name(uri, name);
return Name(library?.uri, name);
}

Name._internal(this.libraryUri, this.name, this.isPublic, this.hashCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class GetterSetterTypesVerifier {
}

void checkInterface(InterfaceElement2 element, Interface interface) {
var libraryUri = element.library2.firstFragment.source.uri;
var libraryUri = element.library2.uri;

var interfaceMap = interface.map2;
for (var entry in interfaceMap.entries) {
Expand Down
5 changes: 2 additions & 3 deletions pkg/analyzer/lib/src/error/must_call_super_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,11 @@ extension on InterfaceType? {
var element = self.element3;

var library = element.library2 as LibraryElementImpl;
var libraryUri = library.firstFragment.source.uri;

var inheritanceManager = library.session.inheritanceManager;

var concrete = inheritanceManager.getMember4(
element,
Name(libraryUri, name),
Name.forLibrary(library, name),
concrete: true,
);

Expand Down
6 changes: 2 additions & 4 deletions pkg/analyzer/lib/src/test_utilities/find_element2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ class FindElement2 extends _FindElementBase {
LibraryExport? result;

for (var export in libraryFragment.libraryExports2) {
var exportedUri =
export.exportedLibrary2?.firstFragment.source.uri.toString();
var exportedUri = export.exportedLibrary2?.uri.toString();
if (exportedUri == targetUri) {
if (result != null) {
throw StateError('Not unique: $targetUri');
Expand Down Expand Up @@ -55,8 +54,7 @@ class FindElement2 extends _FindElementBase {

for (var libraryFragment in libraryFragment.withEnclosing2) {
for (var import in libraryFragment.libraryImports2) {
var importedUri =
import.importedLibrary2?.firstFragment.source.uri.toString();
var importedUri = import.importedLibrary2?.uri.toString();
if (importedUri == targetUri) {
if (importElement == null) {
importElement = import;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1529,7 +1529,7 @@ class DartEditBuilderImpl extends EditBuilderImpl implements DartEditBuilder {

var import = dartFileEditBuilder._getImportElement2(element);
if (import == null) {
var library = element.library2?.firstFragment.source.uri;
var library = element.library2?.uri;
if (library != null) {
import = dartFileEditBuilder._importLibrary(library);
}
Expand Down Expand Up @@ -2183,7 +2183,7 @@ class DartFileEditBuilderImpl extends FileEditBuilderImpl
// Fall back to the element's library if we didn't find a better one.
element.library2;

var uriToImport = libraryToImport?.firstFragment.source.uri;
var uriToImport = libraryToImport?.uri;
if (uriToImport != null) {
var newImport = elementLibrariesToImport[element] = _importLibrary(
uriToImport,
Expand Down Expand Up @@ -2802,7 +2802,7 @@ class DartFileEditBuilderImpl extends FileEditBuilderImpl
var importedLibrary = import.importedLibrary2;
if (importedLibrary != null) {
return _LibraryImport(
uriText: importedLibrary.firstFragment.source.uri.toString(),
uriText: importedLibrary.uri.toString(),
isExplicitlyImported: true,
shownNames: [
for (var combinator in import.combinators)
Expand Down
6 changes: 1 addition & 5 deletions pkg/linter/lib/src/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ extension InterfaceElement2Extension on InterfaceElement2 {
/// Whether this element has the exact [name] and defined in the file with
/// the given [uri].
bool isExactly(String name, Uri uri) =>
name3 == name && enclosingElement2.firstFragment.source.uri == uri;
name3 == name && enclosingElement2.uri == uri;
}

extension InterfaceTypeExtension on InterfaceType {
Expand Down Expand Up @@ -616,10 +616,6 @@ extension InterfaceTypeExtension on InterfaceType {
setters.firstWhereOrNull((s) => s.canonicalName == name);
}

extension LibraryElementExtension2 on LibraryElement2? {
Uri? get uri => this?.library2.firstFragment.source.uri;
}

extension LinterContextExtension on LinterContext {
/// Whether the given [feature] is enabled in this linter context.
bool isEnabled(Feature feature) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class _Visitor extends SimpleAstVisitor<void> {
var name = member.name3;
if (name == null) return false;

var libraryUri = classElement.library2.firstFragment.source.uri;
var libraryUri = classElement.library2.uri;
return context.inheritanceManager
.getInherited3(classElement.thisType, Name(libraryUri, name)) !=
null;
Expand Down
1 change: 0 additions & 1 deletion pkg/linter/lib/src/rules/implementation_imports.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element2.dart';

import '../analyzer.dart';
import '../extensions.dart';

const _desc = r"Don't import implementation files from another package.";

Expand Down
1 change: 0 additions & 1 deletion pkg/linter/lib/src/rules/prefer_relative_imports.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:analyzer/dart/element/element2.dart';
import 'package:path/path.dart' as path;

import '../analyzer.dart';
import '../extensions.dart';
import 'implementation_imports.dart' show samePackage;

const _desc = r'Prefer relative imports for files in `lib/`.';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ extension on DartType? {
if (self is! InterfaceType) return false;
var element = self.element3;
if (element.name3 != 'Int32' && element.name3 != 'Int64') return false;
var uri = element.library2.firstFragment.source.uri;
var uri = element.library2.uri;
if (!uri.isScheme('package')) return false;
return uri.pathSegments.firstOrNull == 'fixnum';
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/linter/lib/src/rules/use_test_throws_matchers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element2.dart';

import '../analyzer.dart';
import '../extensions.dart';

const _desc = r'Use throwsA matcher instead of fail().';

Expand Down Expand Up @@ -40,7 +39,7 @@ class _Visitor extends SimpleAstVisitor<void> {
if (expression is! MethodInvocation) return false;
var element = expression.methodName.element;
return element is TopLevelFunctionElement &&
element.library2.uri ==
element.library2?.uri ==
Uri.parse('package:test_api/src/frontend/expect.dart') &&
element.name3 == functionName;
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/linter/lib/src/util/flutter_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class _Flutter {
bool isKDebugMode(Element2? element) =>
element != null &&
element.name3 == 'kDebugMode' &&
element.library2.uri == _uriFoundation;
element.library2?.uri == _uriFoundation;

bool isState(InterfaceElement element) =>
isExactly(element, _nameState, _uriFramework) ||
Expand Down

0 comments on commit 359aa74

Please sign in to comment.