Skip to content

Commit 174f64f

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Deprecate V1 element visitors.
Change-Id: I51a19c20bfed38a92e6faba561949225a6d1d7a8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/413840 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Paul Berry <[email protected]>
1 parent e4cd515 commit 174f64f

File tree

10 files changed

+84
-46
lines changed

10 files changed

+84
-46
lines changed

pkg/analysis_server/lib/src/services/search/element_visitors.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/element/element2.dart';
6-
import 'package:analyzer/dart/element/visitor.dart';
76
import 'package:analyzer/dart/element/visitor2.dart';
87

98
/// Returns the fragment that is either [fragment], or one of its direct or
@@ -22,7 +21,7 @@ void visitChildren(Element2 element, BoolElementProcessor processor) {
2221
/// If `true` is returned, children of [element] will be visited.
2322
typedef BoolElementProcessor = bool Function(Element2 element);
2423

25-
/// A [GeneralizingElementVisitor] adapter for [BoolElementProcessor].
24+
/// A [GeneralizingElementVisitor2] adapter for [BoolElementProcessor].
2625
class _ElementVisitorAdapter extends GeneralizingElementVisitor2<void> {
2726
final BoolElementProcessor processor;
2827

pkg/analyzer/api.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3171,15 +3171,15 @@ package:analyzer/dart/element/element.dart:
31713171
session (getter: AnalysisSession?)
31723172
sinceSdkVersion (getter: Version?)
31733173
source (getter: Source?)
3174-
accept (method: T? Function<T>(ElementVisitor<T>))
3174+
accept (method: T? Function<T>(ElementVisitor<T>), deprecated)
31753175
getDisplayString (method: String Function({bool multiline, bool withNullability}))
31763176
getExtendedDisplayName (method: String Function(String?))
31773177
isAccessibleIn (method: bool Function(LibraryElement))
31783178
thisOrAncestorMatching (method: E? Function<E extends Element>(bool Function(Element)))
31793179
thisOrAncestorMatching3 (method: E? Function<E extends Element>(bool Function(Element)))
31803180
thisOrAncestorOfType (method: E? Function<E extends Element>())
31813181
thisOrAncestorOfType3 (method: E? Function<E extends Element>())
3182-
visitChildren (method: void Function(ElementVisitor<dynamic>))
3182+
visitChildren (method: void Function(ElementVisitor<dynamic>), deprecated)
31833183
ElementAnnotation (class extends Object implements ConstantEvaluationTarget):
31843184
new (constructor: ElementAnnotation Function())
31853185
constantEvaluationErrors (getter: List<AnalysisError>?)
@@ -3264,7 +3264,7 @@ package:analyzer/dart/element/element.dart:
32643264
new (constructor: ElementLocation Function())
32653265
components (getter: List<String>)
32663266
encoding (getter: String)
3267-
ElementVisitor (class<R> extends Object):
3267+
ElementVisitor (class<R> extends Object, deprecated):
32683268
new (constructor: ElementVisitor<R> Function())
32693269
visitClassElement (method: R? Function(ClassElement))
32703270
visitCompilationUnitElement (method: R? Function(CompilationUnitElement))
@@ -4620,7 +4620,7 @@ package:analyzer/dart/element/type_visitor.dart:
46204620
visitTypeParameterType (method: R Function(TypeParameterType, A))
46214621
visitVoidType (method: R Function(VoidType, A))
46224622
package:analyzer/dart/element/visitor.dart:
4623-
GeneralizingElementVisitor (class<R> extends Object implements ElementVisitor<R>):
4623+
GeneralizingElementVisitor (class<R> extends Object implements ElementVisitor<R>, deprecated):
46244624
new (constructor: GeneralizingElementVisitor<R> Function())
46254625
visitClassElement (method: R? Function(ClassElement))
46264626
visitCompilationUnitElement (method: R? Function(CompilationUnitElement))
@@ -4653,7 +4653,7 @@ package:analyzer/dart/element/visitor.dart:
46534653
visitTypeAliasElement (method: R? Function(TypeAliasElement))
46544654
visitTypeParameterElement (method: R? Function(TypeParameterElement))
46554655
visitVariableElement (method: R? Function(VariableElement))
4656-
RecursiveElementVisitor (class<R> extends Object implements ElementVisitor<R>):
4656+
RecursiveElementVisitor (class<R> extends Object implements ElementVisitor<R>, deprecated):
46574657
new (constructor: RecursiveElementVisitor<R> Function())
46584658
visitClassElement (method: R? Function(ClassElement))
46594659
visitCompilationUnitElement (method: R? Function(CompilationUnitElement))
@@ -4681,7 +4681,7 @@ package:analyzer/dart/element/visitor.dart:
46814681
visitTopLevelVariableElement (method: R? Function(TopLevelVariableElement))
46824682
visitTypeAliasElement (method: R? Function(TypeAliasElement))
46834683
visitTypeParameterElement (method: R? Function(TypeParameterElement))
4684-
SimpleElementVisitor (class<R> extends Object implements ElementVisitor<R>):
4684+
SimpleElementVisitor (class<R> extends Object implements ElementVisitor<R>, deprecated):
46854685
new (constructor: SimpleElementVisitor<R> Function())
46864686
visitClassElement (method: R? Function(ClassElement))
46874687
visitCompilationUnitElement (method: R? Function(CompilationUnitElement))
@@ -4709,7 +4709,7 @@ package:analyzer/dart/element/visitor.dart:
47094709
visitTopLevelVariableElement (method: R? Function(TopLevelVariableElement))
47104710
visitTypeAliasElement (method: R? Function(TypeAliasElement))
47114711
visitTypeParameterElement (method: R? Function(TypeParameterElement))
4712-
ThrowingElementVisitor (class<R> extends Object implements ElementVisitor<R>):
4712+
ThrowingElementVisitor (class<R> extends Object implements ElementVisitor<R>, deprecated):
47134713
new (constructor: ThrowingElementVisitor<R> Function())
47144714
visitClassElement (method: R? Function(ClassElement))
47154715
visitCompilationUnitElement (method: R? Function(CompilationUnitElement))

pkg/analyzer/doc/tutorial/element.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ parameters). Hence, you'd want to create a subclass of
135135
`GeneralizingElementVisitor`.
136136

137137
```dart
138-
class ParameterCounter extends GeneralizingElementVisitor<void> {
138+
class ParameterCounter extends GeneralizingElementVisitor2<void> {
139139
int maxParameterCount = 0;
140140
141141
@override
142-
void visitExecutableElement(ExecutableElement element) {
143-
maxParameterCount = math.max(maxParameterCount, element.parameters.length);
142+
void visitExecutableElement(ExecutableElement2 element) {
143+
maxParameterCount = math.max(maxParameterCount, element.formalParameters.length);
144144
super.visitExecutableElement(element);
145145
}
146146
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,7 @@ abstract class Element implements AnalysisTarget {
797797
///
798798
/// Returns the value returned by the visitor as a result of visiting this
799799
/// element.
800+
@Deprecated('Use Element2 and accept2() instead')
800801
T? accept<T>(ElementVisitor<T> visitor);
801802

802803
/// Returns the presentation of this element as it should appear when
@@ -862,6 +863,7 @@ abstract class Element implements AnalysisTarget {
862863

863864
/// Uses the given [visitor] to visit all of the children of this element.
864865
/// There is no guarantee of the order in which the children will be visited.
866+
@Deprecated('Use Element2 and visitChildren2() instead')
865867
void visitChildren(ElementVisitor visitor);
866868
}
867869

@@ -1194,6 +1196,7 @@ abstract class ElementLocation {
11941196
/// visited, and
11951197
/// * ThrowingElementVisitor which implements every visit method by throwing an
11961198
/// exception.
1199+
@Deprecated('Use ElementVisitor2 instead')
11971200
abstract class ElementVisitor<R> {
11981201
R? visitClassElement(ClassElement element);
11991202

pkg/analyzer/lib/dart/element/visitor.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
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-
// ignore_for_file: analyzer_use_new_elements
6-
75
/// Defines element visitors that support useful patterns for visiting the
86
/// elements in an [element model](element.dart).
97
///
108
/// Dart is an evolving language, and the element model must evolved with it.
119
/// When the element model changes, the visitor interface will sometimes change
1210
/// as well. If it is desirable to get a compilation error when the structure of
1311
/// the element model has been modified, then you should consider implementing
14-
/// the interface [ElementVisitor] directly. Doing so will ensure that changes
12+
/// the interface `ElementVisitor` directly. Doing so will ensure that changes
1513
/// that introduce new classes of elements will be flagged. (Of course, not all
1614
/// changes to the element model require the addition of a new class of element,
1715
/// and hence cannot be caught this way.)
@@ -20,7 +18,7 @@
2018
/// you will probably want to extend one of the classes in this library because
2119
/// doing so will simplify the task of writing your visitor and guard against
2220
/// future changes to the element model. For example, the
23-
/// [RecursiveElementVisitor] automates the process of visiting all of the
21+
/// `RecursiveElementVisitor` automates the process of visiting all of the
2422
/// descendants of an element.
2523
library;
2624

@@ -82,6 +80,7 @@ import 'package:analyzer/dart/element/element.dart';
8280
/// invoked and will cause the children of the visited node to not be visited.
8381
///
8482
/// Clients may extend this class.
83+
@Deprecated('Use Element2, ElementVisitor2, and accept2() instead')
8584
class GeneralizingElementVisitor<R> implements ElementVisitor<R> {
8685
/// Initialize a newly created visitor.
8786
const GeneralizingElementVisitor();
@@ -216,6 +215,7 @@ class GeneralizingElementVisitor<R> implements ElementVisitor<R> {
216215
/// not be visited.
217216
///
218217
/// Clients may extend this class.
218+
@Deprecated('Use Element2, ElementVisitor2, and accept2() instead')
219219
class RecursiveElementVisitor<R> implements ElementVisitor<R> {
220220
/// Initialize a newly created visitor.
221221
const RecursiveElementVisitor();
@@ -383,6 +383,7 @@ class RecursiveElementVisitor<R> implements ElementVisitor<R> {
383383
/// structure) and that only need to visit a small number of element types.
384384
///
385385
/// Clients may extend this class.
386+
@Deprecated('Use Element2, ElementVisitor2, and accept2() instead')
386387
class SimpleElementVisitor<R> implements ElementVisitor<R> {
387388
/// Initialize a newly created visitor.
388389
const SimpleElementVisitor();
@@ -476,6 +477,7 @@ class SimpleElementVisitor<R> implements ElementVisitor<R> {
476477
/// want to catch when any other visit methods have been invoked.
477478
///
478479
/// Clients may extend this class.
480+
@Deprecated('Use Element2, ElementVisitor2, and accept2() instead')
479481
class ThrowingElementVisitor<R> implements ElementVisitor<R> {
480482
/// Initialize a newly created visitor.
481483
const ThrowingElementVisitor();

pkg/analyzer/lib/src/dart/analysis/search.dart

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'package:analyzer/dart/analysis/results.dart';
66
import 'package:analyzer/dart/ast/syntactic_entity.dart';
77
import 'package:analyzer/dart/ast/visitor.dart';
88
import 'package:analyzer/dart/element/element2.dart';
9-
import 'package:analyzer/dart/element/visitor.dart';
109
import 'package:analyzer/source/line_info.dart';
1110
import 'package:analyzer/source/source_range.dart';
1211
import 'package:analyzer/src/dart/analysis/driver.dart';
@@ -27,15 +26,30 @@ Fragment _getEnclosingFragment(
2726
CompilationUnitElementImpl libraryFragment,
2827
int offset,
2928
) {
30-
var finder = _ContainingFragmentFinder(offset);
31-
libraryFragment.accept(finder);
32-
var result = finder.containingFragment;
33-
if (result == null) {
34-
throw StateError(
35-
'No containing fragment in ${libraryFragment.source.fullName} at $offset',
36-
);
29+
Fragment? visitFragment(Fragment fragment) {
30+
var fragmentImpl = fragment as ElementImpl;
31+
var codeOffset = fragmentImpl.codeOffset;
32+
var codeLength = fragmentImpl.codeLength;
33+
if (codeOffset == null || codeLength == null) {
34+
return null;
35+
}
36+
37+
var codeEnd = codeOffset + codeLength;
38+
if (codeOffset <= offset && offset <= codeEnd) {
39+
for (var child in fragment.children3) {
40+
var result = visitFragment(child);
41+
if (result != null) {
42+
return result;
43+
}
44+
}
45+
return fragment;
46+
}
47+
48+
return null;
3749
}
38-
return result;
50+
51+
var result = visitFragment(libraryFragment);
52+
return result ?? libraryFragment;
3953
}
4054

4155
DeclarationKind? _getSearchElementKind(Element2 element) {
@@ -1090,24 +1104,6 @@ class WorkspaceSymbols {
10901104
}
10911105
}
10921106

1093-
/// A visitor that finds the deep-most [ElementImpl] that contains the [offset].
1094-
class _ContainingFragmentFinder extends GeneralizingElementVisitor<void> {
1095-
final int offset;
1096-
Fragment? containingFragment;
1097-
1098-
_ContainingFragmentFinder(this.offset);
1099-
1100-
@override
1101-
void visitElement(covariant ElementImpl element) {
1102-
if (element.codeOffset != null &&
1103-
element.codeOffset! <= offset &&
1104-
offset <= element.codeOffset! + element.codeLength!) {
1105-
containingFragment = element as Fragment;
1106-
super.visitElement(element);
1107-
}
1108-
}
1109-
}
1110-
11111107
/// Searches through [fileEntries] for declarations.
11121108
class _FindDeclarations {
11131109
final List<MapEntry<Uri, AnalysisDriver>> fileEntries;

0 commit comments

Comments
 (0)