Skip to content

Commit

Permalink
Elements. Migrate lib/src/utilities/extensions/element.dart
Browse files Browse the repository at this point in the history
Change-Id: Ibcf9d2449411413210e1d363ced4acdcfeede8d7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/398948
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed Dec 4, 2024
1 parent f773702 commit 6f992ae
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 59 deletions.
1 change: 0 additions & 1 deletion pkg/analysis_server/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ lib/src/services/search/element_visitors.dart
lib/src/services/search/hierarchy.dart
lib/src/services/search/search_engine.dart
lib/src/services/search/search_engine_internal.dart
lib/src/utilities/extensions/element.dart
test/abstract_single_unit.dart
test/plugin/protocol_dart_test.dart
test/services/correction/status_test.dart
Expand Down
23 changes: 18 additions & 5 deletions pkg/analysis_server/lib/src/protocol_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'package:analyzer/error/error.dart' as engine;
import 'package:analyzer/source/error_processor.dart';
import 'package:analyzer/source/source.dart' as engine;
import 'package:analyzer/source/source_range.dart' as engine;
import 'package:analyzer/src/utilities/extensions/element.dart';
import 'package:analyzer_plugin/protocol/protocol_common.dart';

export 'package:analysis_server/plugin/protocol/protocol_dart.dart';
Expand Down Expand Up @@ -409,9 +410,18 @@ List<Element> _computePath(engine.Element element) {
element = element.enclosingElement3;
}

for (var e in element.withAncestors) {
path.add(convertElement(e));
var element2 = element.asElement2;
if (element2 != null) {
for (var fragment in element2.firstFragment.withAncestors) {
if (fragment case engine.Element e) {
path.add(convertElement(e));
if (fragment is engine.LibraryFragment) {
path.add(convertElement2(fragment.element));
}
}
}
}

return path;
}

Expand All @@ -428,9 +438,12 @@ engine.CompilationUnitElement _getUnitElement(engine.Element element) {
return element.definingCompilationUnit;
}

for (var e in element.withAncestors) {
if (e is engine.CompilationUnitElement) {
return e;
var element2 = element.asElement2;
if (element2 != null) {
for (var fragment in element2.firstFragment.withAncestors) {
if (fragment case engine.CompilationUnitElement unit) {
return unit;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'package:analyzer/source/source_range.dart';
import 'package:analyzer/src/dart/ast/extensions.dart';
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
import 'package:analyzer/src/dart/resolver/applicable_extensions.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';
import 'package:analyzer_plugin/src/utilities/change_builder/change_builder_dart.dart';
import 'package:analyzer_plugin/src/utilities/library.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
Expand Down Expand Up @@ -86,9 +87,12 @@ class ImportLibrary extends MultiCorrectionProducer {
continue;
}
foundImport = true;
var instantiatedExtensions = importedLibrary.exportedExtensions
var instantiatedExtensions = importedLibrary.asElement2.exportedExtensions
.havingMemberWithBaseName(memberName)
.applicableTo(targetLibrary: libraryElement, targetType: targetType);
.applicableTo(
targetLibrary: libraryElement2,
targetType: targetType,
);
for (var instantiatedExtension in instantiatedExtensions) {
// If the import has a combinator that needs to be updated, then offer
// to update it.
Expand All @@ -100,7 +104,7 @@ class ImportLibrary extends MultiCorrectionProducer {
_ImportLibraryCombinator(
libraryToImport.source.uri.toString(),
combinator,
instantiatedExtension.extension.name!,
instantiatedExtension.extension.name3!,
context: context,
),
);
Expand All @@ -109,7 +113,7 @@ class ImportLibrary extends MultiCorrectionProducer {
_ImportLibraryCombinator(
libraryToImport.source.uri.toString(),
combinator,
instantiatedExtension.extension.name!,
instantiatedExtension.extension.name3!,
context: context,
),
);
Expand Down Expand Up @@ -757,9 +761,9 @@ class _ImportLibraryContainingExtension extends ResolvedCorrectionProducer {

@override
Future<void> compute(ChangeBuilder builder) async {
var instantiatedExtensions = library.exportedExtensions
var instantiatedExtensions = library.asElement2.exportedExtensions
.havingMemberWithBaseName(memberName)
.applicableTo(targetLibrary: libraryElement, targetType: targetType);
.applicableTo(targetLibrary: libraryElement2, targetType: targetType);
if (instantiatedExtensions.isNotEmpty) {
await builder.addDartFileEdit(file, (builder) {
_uriText = builder.importLibrary(library.source.uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
import 'package:analyzer/src/dart/resolver/applicable_extensions.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';
Expand Down Expand Up @@ -106,10 +107,10 @@ class _UseDifferentDivisionOperator extends ResolvedCorrectionProducer {
var name = Name(unitResult.libraryElement.source.uri, otherOperator.lexeme);
var hasNoExtensionWithOtherDivisionOperator =
await librariesWithExtensions(otherOperator.lexeme).where((library) {
return library.exportedExtensions
return library.asElement2.exportedExtensions
.havingMemberWithBaseName(name)
.applicableTo(
targetLibrary: libraryElement,
targetLibrary: libraryElement2,
targetType: leftType!,
)
.isNotEmpty;
Expand Down
60 changes: 22 additions & 38 deletions pkg/analysis_server/lib/src/utilities/extensions/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// 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/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';

extension ClassElementExtensions on ClassElement2 {
Expand Down Expand Up @@ -43,70 +42,55 @@ extension Element2Extension on Element2 {
/// the enclosing library, has been annotated with the `@deprecated`
/// annotation.
bool get hasOrInheritsDeprecated {
if (this is Annotatable && (this as Annotatable).metadata2.hasDeprecated) {
return true;
}
if (this is FormalParameterElement) {
return false;
if (this case Annotatable annotatable) {
if (annotatable.metadata2.hasDeprecated) {
return true;
}
}

var ancestor = enclosingElement2;
if (ancestor is InterfaceElement2) {
if (ancestor.metadata2.hasDeprecated) {
return true;
}
ancestor = ancestor.enclosingElement2;
}
return ancestor is LibraryFragment &&
(ancestor as LibraryFragment).metadata2.hasDeprecated;
}
}

extension ElementExtension on Element {
/// Return `true` if this element, the enclosing class (if there is one), or
/// the enclosing library, has been annotated with the `@deprecated`
/// annotation.
bool get hasOrInheritsDeprecated {
if (hasDeprecated) {
return true;
}
var ancestor = enclosingElement3;
if (ancestor is InterfaceElement) {
if (ancestor.hasDeprecated) {
return true;
}
ancestor = ancestor.enclosingElement3;
}
return ancestor is CompilationUnitElement && ancestor.library.hasDeprecated;
return ancestor is LibraryElement2 && ancestor.metadata2.hasDeprecated;
}

/// Return this element and all its enclosing elements.
Iterable<Element> get withAncestors sync* {
Iterable<Element2> get withAncestors sync* {
var current = this;
while (true) {
yield current;
var enclosing = current.enclosingElement3;
var enclosing = current.enclosingElement2;
if (enclosing == null) {
if (current is CompilationUnitElement) {
yield current.library;
}
break;
}
current = enclosing;
}
}
}

extension LibraryElementExtensions on LibraryElement {
/// Return all extensions exported from this library.
Iterable<ExtensionElement> get exportedExtensions {
return exportNamespace.definedNames.values.whereType();
extension FragmentExtension on Fragment {
/// Return this fragment and all its enclosing fragment.
Iterable<Fragment> get withAncestors sync* {
var current = this;
while (true) {
yield current;
var enclosing = current.enclosingFragment;
if (enclosing == null) {
break;
}
current = enclosing;
}
}
}

extension LibraryElementExtensions2 on LibraryElement2 {
/// Return all extensions exported from this library.
Iterable<ExtensionElement> get exportedExtensions {
return exportNamespace.definedNames.values.whereType();
Iterable<ExtensionElement2> get exportedExtensions {
return exportNamespace.definedNames2.values.whereType();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,10 @@ abstract class MultiCorrectionProducer
/// produced.
LibraryElement get libraryElement => unitResult.libraryElement;

/// The library element for the library in which a correction is being
/// produced.
LibraryElement2 get libraryElement2 => unitResult.libraryElement2;

@override
ResolvedLibraryResult get libraryResult =>
super.libraryResult as ResolvedLibraryResult;
Expand Down
16 changes: 9 additions & 7 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5024,13 +5024,15 @@ class FunctionElementImpl extends ExecutableElementImpl

@override
Fragment? get enclosingFragment {
if (enclosingElement3 is CompilationUnitElement) {
// TODO(augmentations): Support the fragment chain.
return enclosingElement3 as LibraryFragment;
} else {
// Local functions cannot be augmented.
throw UnsupportedError('This is not a fragment');
}
switch (enclosingElement3) {
case LibraryFragment libraryFragment:
// TODO(augmentations): Support the fragment chain.
return libraryFragment;
case ExecutableFragment executableFragment:
return executableFragment;
}
// Local functions cannot be augmented.
throw UnsupportedError('This is not a fragment');
}

@override
Expand Down

0 comments on commit 6f992ae

Please sign in to comment.