diff --git a/pkg/analysis_server/analyzer_use_new_elements.txt b/pkg/analysis_server/analyzer_use_new_elements.txt index e9b92bc190dd..149542805d1a 100644 --- a/pkg/analysis_server/analyzer_use_new_elements.txt +++ b/pkg/analysis_server/analyzer_use_new_elements.txt @@ -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 diff --git a/pkg/analysis_server/lib/src/protocol_server.dart b/pkg/analysis_server/lib/src/protocol_server.dart index 3d7dcd36d263..177bd7de59a1 100644 --- a/pkg/analysis_server/lib/src/protocol_server.dart +++ b/pkg/analysis_server/lib/src/protocol_server.dart @@ -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'; @@ -409,9 +410,18 @@ List _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; } @@ -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; + } } } diff --git a/pkg/analysis_server/lib/src/services/correction/dart/import_library.dart b/pkg/analysis_server/lib/src/services/correction/dart/import_library.dart index ede462542391..94514d24a72c 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/import_library.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/import_library.dart @@ -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'; @@ -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. @@ -100,7 +104,7 @@ class ImportLibrary extends MultiCorrectionProducer { _ImportLibraryCombinator( libraryToImport.source.uri.toString(), combinator, - instantiatedExtension.extension.name!, + instantiatedExtension.extension.name3!, context: context, ), ); @@ -109,7 +113,7 @@ class ImportLibrary extends MultiCorrectionProducer { _ImportLibraryCombinator( libraryToImport.source.uri.toString(), combinator, - instantiatedExtension.extension.name!, + instantiatedExtension.extension.name3!, context: context, ), ); @@ -757,9 +761,9 @@ class _ImportLibraryContainingExtension extends ResolvedCorrectionProducer { @override Future 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); diff --git a/pkg/analysis_server/lib/src/services/correction/dart/use_different_division_operator.dart b/pkg/analysis_server/lib/src/services/correction/dart/use_different_division_operator.dart index 97fd3497a23e..8174862d11ef 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/use_different_division_operator.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/use_different_division_operator.dart @@ -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'; @@ -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; diff --git a/pkg/analysis_server/lib/src/utilities/extensions/element.dart b/pkg/analysis_server/lib/src/utilities/extensions/element.dart index 30a0fdb746b7..d10fde1c6a25 100644 --- a/pkg/analysis_server/lib/src/utilities/extensions/element.dart +++ b/pkg/analysis_server/lib/src/utilities/extensions/element.dart @@ -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 { @@ -43,12 +42,12 @@ 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) { @@ -56,39 +55,16 @@ extension Element2Extension on Element2 { } 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 get withAncestors sync* { + Iterable 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; @@ -96,17 +72,25 @@ extension ElementExtension on Element { } } -extension LibraryElementExtensions on LibraryElement { - /// Return all extensions exported from this library. - Iterable get exportedExtensions { - return exportNamespace.definedNames.values.whereType(); +extension FragmentExtension on Fragment { + /// Return this fragment and all its enclosing fragment. + Iterable 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 get exportedExtensions { - return exportNamespace.definedNames.values.whereType(); + Iterable get exportedExtensions { + return exportNamespace.definedNames2.values.whereType(); } } diff --git a/pkg/analysis_server_plugin/lib/edit/dart/correction_producer.dart b/pkg/analysis_server_plugin/lib/edit/dart/correction_producer.dart index 27c213d9eb59..460e558b238c 100644 --- a/pkg/analysis_server_plugin/lib/edit/dart/correction_producer.dart +++ b/pkg/analysis_server_plugin/lib/edit/dart/correction_producer.dart @@ -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; diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart index 50cdfe963aee..32ef965460c8 100644 --- a/pkg/analyzer/lib/src/dart/element/element.dart +++ b/pkg/analyzer/lib/src/dart/element/element.dart @@ -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