Skip to content

Commit 6f992ae

Browse files
scheglovCommit Queue
authored and
Commit Queue
committed
Elements. Migrate lib/src/utilities/extensions/element.dart
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]>
1 parent f773702 commit 6f992ae

File tree

7 files changed

+66
-59
lines changed

7 files changed

+66
-59
lines changed

pkg/analysis_server/analyzer_use_new_elements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ lib/src/services/search/element_visitors.dart
6262
lib/src/services/search/hierarchy.dart
6363
lib/src/services/search/search_engine.dart
6464
lib/src/services/search/search_engine_internal.dart
65-
lib/src/utilities/extensions/element.dart
6665
test/abstract_single_unit.dart
6766
test/plugin/protocol_dart_test.dart
6867
test/services/correction/status_test.dart

pkg/analysis_server/lib/src/protocol_server.dart

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import 'package:analyzer/error/error.dart' as engine;
1919
import 'package:analyzer/source/error_processor.dart';
2020
import 'package:analyzer/source/source.dart' as engine;
2121
import 'package:analyzer/source/source_range.dart' as engine;
22+
import 'package:analyzer/src/utilities/extensions/element.dart';
2223
import 'package:analyzer_plugin/protocol/protocol_common.dart';
2324

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

412-
for (var e in element.withAncestors) {
413-
path.add(convertElement(e));
413+
var element2 = element.asElement2;
414+
if (element2 != null) {
415+
for (var fragment in element2.firstFragment.withAncestors) {
416+
if (fragment case engine.Element e) {
417+
path.add(convertElement(e));
418+
if (fragment is engine.LibraryFragment) {
419+
path.add(convertElement2(fragment.element));
420+
}
421+
}
422+
}
414423
}
424+
415425
return path;
416426
}
417427

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

431-
for (var e in element.withAncestors) {
432-
if (e is engine.CompilationUnitElement) {
433-
return e;
441+
var element2 = element.asElement2;
442+
if (element2 != null) {
443+
for (var fragment in element2.firstFragment.withAncestors) {
444+
if (fragment case engine.CompilationUnitElement unit) {
445+
return unit;
446+
}
434447
}
435448
}
436449

pkg/analysis_server/lib/src/services/correction/dart/import_library.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import 'package:analyzer/source/source_range.dart';
1717
import 'package:analyzer/src/dart/ast/extensions.dart';
1818
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
1919
import 'package:analyzer/src/dart/resolver/applicable_extensions.dart';
20+
import 'package:analyzer/src/utilities/extensions/element.dart';
2021
import 'package:analyzer_plugin/src/utilities/change_builder/change_builder_dart.dart';
2122
import 'package:analyzer_plugin/src/utilities/library.dart';
2223
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
@@ -86,9 +87,12 @@ class ImportLibrary extends MultiCorrectionProducer {
8687
continue;
8788
}
8889
foundImport = true;
89-
var instantiatedExtensions = importedLibrary.exportedExtensions
90+
var instantiatedExtensions = importedLibrary.asElement2.exportedExtensions
9091
.havingMemberWithBaseName(memberName)
91-
.applicableTo(targetLibrary: libraryElement, targetType: targetType);
92+
.applicableTo(
93+
targetLibrary: libraryElement2,
94+
targetType: targetType,
95+
);
9296
for (var instantiatedExtension in instantiatedExtensions) {
9397
// If the import has a combinator that needs to be updated, then offer
9498
// to update it.
@@ -100,7 +104,7 @@ class ImportLibrary extends MultiCorrectionProducer {
100104
_ImportLibraryCombinator(
101105
libraryToImport.source.uri.toString(),
102106
combinator,
103-
instantiatedExtension.extension.name!,
107+
instantiatedExtension.extension.name3!,
104108
context: context,
105109
),
106110
);
@@ -109,7 +113,7 @@ class ImportLibrary extends MultiCorrectionProducer {
109113
_ImportLibraryCombinator(
110114
libraryToImport.source.uri.toString(),
111115
combinator,
112-
instantiatedExtension.extension.name!,
116+
instantiatedExtension.extension.name3!,
113117
context: context,
114118
),
115119
);
@@ -757,9 +761,9 @@ class _ImportLibraryContainingExtension extends ResolvedCorrectionProducer {
757761

758762
@override
759763
Future<void> compute(ChangeBuilder builder) async {
760-
var instantiatedExtensions = library.exportedExtensions
764+
var instantiatedExtensions = library.asElement2.exportedExtensions
761765
.havingMemberWithBaseName(memberName)
762-
.applicableTo(targetLibrary: libraryElement, targetType: targetType);
766+
.applicableTo(targetLibrary: libraryElement2, targetType: targetType);
763767
if (instantiatedExtensions.isNotEmpty) {
764768
await builder.addDartFileEdit(file, (builder) {
765769
_uriText = builder.importLibrary(library.source.uri);

pkg/analysis_server/lib/src/services/correction/dart/use_different_division_operator.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:analyzer/dart/element/element.dart';
1111
import 'package:analyzer/dart/element/type.dart';
1212
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
1313
import 'package:analyzer/src/dart/resolver/applicable_extensions.dart';
14+
import 'package:analyzer/src/utilities/extensions/element.dart';
1415
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
1516
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
1617
import 'package:analyzer_plugin/utilities/range_factory.dart';
@@ -106,10 +107,10 @@ class _UseDifferentDivisionOperator extends ResolvedCorrectionProducer {
106107
var name = Name(unitResult.libraryElement.source.uri, otherOperator.lexeme);
107108
var hasNoExtensionWithOtherDivisionOperator =
108109
await librariesWithExtensions(otherOperator.lexeme).where((library) {
109-
return library.exportedExtensions
110+
return library.asElement2.exportedExtensions
110111
.havingMemberWithBaseName(name)
111112
.applicableTo(
112-
targetLibrary: libraryElement,
113+
targetLibrary: libraryElement2,
113114
targetType: leftType!,
114115
)
115116
.isNotEmpty;

pkg/analysis_server/lib/src/utilities/extensions/element.dart

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:analyzer/dart/element/element.dart';
65
import 'package:analyzer/dart/element/element2.dart';
76

87
extension ClassElementExtensions on ClassElement2 {
@@ -43,70 +42,55 @@ extension Element2Extension on Element2 {
4342
/// the enclosing library, has been annotated with the `@deprecated`
4443
/// annotation.
4544
bool get hasOrInheritsDeprecated {
46-
if (this is Annotatable && (this as Annotatable).metadata2.hasDeprecated) {
47-
return true;
48-
}
49-
if (this is FormalParameterElement) {
50-
return false;
45+
if (this case Annotatable annotatable) {
46+
if (annotatable.metadata2.hasDeprecated) {
47+
return true;
48+
}
5149
}
50+
5251
var ancestor = enclosingElement2;
5352
if (ancestor is InterfaceElement2) {
5453
if (ancestor.metadata2.hasDeprecated) {
5554
return true;
5655
}
5756
ancestor = ancestor.enclosingElement2;
5857
}
59-
return ancestor is LibraryFragment &&
60-
(ancestor as LibraryFragment).metadata2.hasDeprecated;
61-
}
62-
}
63-
64-
extension ElementExtension on Element {
65-
/// Return `true` if this element, the enclosing class (if there is one), or
66-
/// the enclosing library, has been annotated with the `@deprecated`
67-
/// annotation.
68-
bool get hasOrInheritsDeprecated {
69-
if (hasDeprecated) {
70-
return true;
71-
}
72-
var ancestor = enclosingElement3;
73-
if (ancestor is InterfaceElement) {
74-
if (ancestor.hasDeprecated) {
75-
return true;
76-
}
77-
ancestor = ancestor.enclosingElement3;
78-
}
79-
return ancestor is CompilationUnitElement && ancestor.library.hasDeprecated;
58+
return ancestor is LibraryElement2 && ancestor.metadata2.hasDeprecated;
8059
}
8160

8261
/// Return this element and all its enclosing elements.
83-
Iterable<Element> get withAncestors sync* {
62+
Iterable<Element2> get withAncestors sync* {
8463
var current = this;
8564
while (true) {
8665
yield current;
87-
var enclosing = current.enclosingElement3;
66+
var enclosing = current.enclosingElement2;
8867
if (enclosing == null) {
89-
if (current is CompilationUnitElement) {
90-
yield current.library;
91-
}
9268
break;
9369
}
9470
current = enclosing;
9571
}
9672
}
9773
}
9874

99-
extension LibraryElementExtensions on LibraryElement {
100-
/// Return all extensions exported from this library.
101-
Iterable<ExtensionElement> get exportedExtensions {
102-
return exportNamespace.definedNames.values.whereType();
75+
extension FragmentExtension on Fragment {
76+
/// Return this fragment and all its enclosing fragment.
77+
Iterable<Fragment> get withAncestors sync* {
78+
var current = this;
79+
while (true) {
80+
yield current;
81+
var enclosing = current.enclosingFragment;
82+
if (enclosing == null) {
83+
break;
84+
}
85+
current = enclosing;
86+
}
10387
}
10488
}
10589

10690
extension LibraryElementExtensions2 on LibraryElement2 {
10791
/// Return all extensions exported from this library.
108-
Iterable<ExtensionElement> get exportedExtensions {
109-
return exportNamespace.definedNames.values.whereType();
92+
Iterable<ExtensionElement2> get exportedExtensions {
93+
return exportNamespace.definedNames2.values.whereType();
11094
}
11195
}
11296

pkg/analysis_server_plugin/lib/edit/dart/correction_producer.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,10 @@ abstract class MultiCorrectionProducer
343343
/// produced.
344344
LibraryElement get libraryElement => unitResult.libraryElement;
345345

346+
/// The library element for the library in which a correction is being
347+
/// produced.
348+
LibraryElement2 get libraryElement2 => unitResult.libraryElement2;
349+
346350
@override
347351
ResolvedLibraryResult get libraryResult =>
348352
super.libraryResult as ResolvedLibraryResult;

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5024,13 +5024,15 @@ class FunctionElementImpl extends ExecutableElementImpl
50245024

50255025
@override
50265026
Fragment? get enclosingFragment {
5027-
if (enclosingElement3 is CompilationUnitElement) {
5028-
// TODO(augmentations): Support the fragment chain.
5029-
return enclosingElement3 as LibraryFragment;
5030-
} else {
5031-
// Local functions cannot be augmented.
5032-
throw UnsupportedError('This is not a fragment');
5033-
}
5027+
switch (enclosingElement3) {
5028+
case LibraryFragment libraryFragment:
5029+
// TODO(augmentations): Support the fragment chain.
5030+
return libraryFragment;
5031+
case ExecutableFragment executableFragment:
5032+
return executableFragment;
5033+
}
5034+
// Local functions cannot be augmented.
5035+
throw UnsupportedError('This is not a fragment');
50345036
}
50355037

50365038
@override

0 commit comments

Comments
 (0)