Skip to content

Commit

Permalink
Deprecate Element.getAncestor, replacing it with methods matching tho…
Browse files Browse the repository at this point in the history
…se in AstNode

Change-Id: Iebb046791c1fce57a228f997e9c5936809fbc872
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/127481
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
bwilkerson authored and commit-bot@chromium.org committed Dec 6, 2019
1 parent dfa3203 commit 652926b
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 19 deletions.
3 changes: 1 addition & 2 deletions pkg/analysis_server/lib/src/computer/computer_hover.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ class DartUnitHoverComputer {
// not local element
if (element.enclosingElement is! ExecutableElement) {
// containing class
ClassElement containingClass =
element.getAncestor((e) => e is ClassElement);
ClassElement containingClass = element.thisOrAncestorOfType();
if (containingClass != null) {
hover.containingClassDescription = containingClass.displayName;
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/analysis_server/lib/src/services/correction/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ CompilationUnitElement getCompilationUnitElement(Element element) {
if (element is CompilationUnitElement) {
return element;
}
return element.getAncestor((e) => e is CompilationUnitElement);
return element.thisOrAncestorOfType();
}

String getDefaultValueCode(DartType type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class _ClassMemberValidator {
var visibleRangeMap = <LocalElement, SourceRange>{};

Future<List<LocalElement>> getLocalElements(Element element) async {
var unitElement = element.getAncestor((e) => e is CompilationUnitElement);
var unitElement = element.thisOrAncestorOfType<CompilationUnitElement>();
var localElements = localElementMap[unitElement];

if (localElements == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class _ExtensionMemberValidator {
var visibleRangeMap = <LocalElement, SourceRange>{};

Future<List<LocalElement>> getLocalElements(Element element) async {
var unitElement = element.getAncestor((e) => e is CompilationUnitElement);
var unitElement = element.thisOrAncestorOfType<CompilationUnitElement>();
var localElements = localElementMap[unitElement];

if (localElements == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ class _RenameUnitMemberValidator {
void _validateWillBeShadowed() {
for (SearchMatch reference in references) {
Element refElement = reference.element;
ClassElement refClass = refElement.getAncestor((e) => e is ClassElement);
ClassElement refClass = refElement.thisOrAncestorOfType();
if (refClass != null) {
visitChildren(refClass, (shadow) {
if (hasDisplayName(shadow, name)) {
Expand Down Expand Up @@ -304,8 +304,7 @@ class _RenameUnitMemberValidator {
continue;
}
// cannot be shadowed if declared in the same class as reference
ClassElement refClass =
refElement.getAncestor((e) => e is ClassElement);
ClassElement refClass = refElement.thisOrAncestorOfType();
if (refClass == declaringClass) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/dart/ast/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ abstract class AstNode implements SyntacticEntity {

/// Return either this node or the most immediate ancestor of this node that
/// has the given type, or `null` if there is no such node.
T thisOrAncestorOfType<T extends AstNode>();
E thisOrAncestorOfType<E extends AstNode>();

/// Return a textual description of this node in a form approximating valid
/// source.
Expand Down
10 changes: 10 additions & 0 deletions pkg/analyzer/lib/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ abstract class Element implements AnalysisTarget {
/// Return the most immediate ancestor of this element for which the
/// [predicate] returns `true`, or `null` if there is no such ancestor. Note
/// that this element will never be returned.
@Deprecated('Use either thisOrAncestorMatching or thisOrAncestorOfType')
E getAncestor<E extends Element>(Predicate<Element> predicate);

/// Return a display name for the given element that includes the path to the
Expand All @@ -664,6 +665,15 @@ abstract class Element implements AnalysisTarget {
/// </blockquote>
bool isAccessibleIn(LibraryElement library);

/// Return either this element or the most immediate ancestor of this element
/// for which the [predicate] returns `true`, or `null` if there is no such
/// element.
E thisOrAncestorMatching<E extends Element>(Predicate<Element> predicate);

/// Return either this element or the most immediate ancestor of this element
/// that has the given type, or `null` if there is no such element.
E thisOrAncestorOfType<E extends Element>();

/// Use the given [visitor] to visit all of the children of this element.
/// There is no guarantee of the order in which the children will be visited.
void visitChildren(ElementVisitor visitor);
Expand Down
7 changes: 3 additions & 4 deletions pkg/analyzer/lib/src/dart/ast/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,6 @@ abstract class AstNodeImpl implements AstNode {

@override
E thisOrAncestorMatching<E extends AstNode>(Predicate<AstNode> predicate) {
// TODO(brianwilkerson) It is a bug that this method can return `this`.
AstNode node = this;
while (node != null && !predicate(node)) {
node = node.parent;
Expand All @@ -823,12 +822,12 @@ abstract class AstNodeImpl implements AstNode {
}

@override
T thisOrAncestorOfType<T extends AstNode>() {
E thisOrAncestorOfType<E extends AstNode>() {
AstNode node = this;
while (node != null && node is! T) {
while (node != null && node is! E) {
node = node.parent;
}
return node as T;
return node as E;
}

@override
Expand Down
30 changes: 28 additions & 2 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2916,8 +2916,7 @@ abstract class ElementImpl implements Element {
}

@override
LibraryElement get library =>
getAncestor((element) => element is LibraryElement);
LibraryElement get library => thisOrAncestorOfType();

@override
Source get librarySource => library?.source;
Expand Down Expand Up @@ -3029,6 +3028,7 @@ abstract class ElementImpl implements Element {
}
}

@Deprecated('Use either thisOrAncestorMatching or thisOrAncestorOfType')
@override
E getAncestor<E extends Element>(Predicate<Element> predicate) {
var ancestor = _enclosingElement;
Expand Down Expand Up @@ -3087,6 +3087,24 @@ abstract class ElementImpl implements Element {
_modifiers = BooleanArray.set(_modifiers, modifier.ordinal, value);
}

@override
E thisOrAncestorMatching<E extends Element>(Predicate<Element> predicate) {
Element element = this;
while (element != null && !predicate(element)) {
element = element.enclosingElement;
}
return element as E;
}

@override
E thisOrAncestorOfType<E extends Element>() {
Element element = this;
while (element != null && element is! E) {
element = element.enclosingElement;
}
return element as E;
}

@override
String toString() {
StringBuffer buffer = StringBuffer();
Expand Down Expand Up @@ -6234,6 +6252,7 @@ class MultiplyDefinedElementImpl implements MultiplyDefinedElement {
T accept<T>(ElementVisitor<T> visitor) =>
visitor.visitMultiplyDefinedElement(this);

@Deprecated('Use either thisOrAncestorMatching or thisOrAncestorOfType')
@override
E getAncestor<E extends Element>(Predicate<Element> predicate) => null;

Expand All @@ -6255,6 +6274,13 @@ class MultiplyDefinedElementImpl implements MultiplyDefinedElement {
return false;
}

@override
E thisOrAncestorMatching<E extends Element>(Predicate<Element> predicate) =>
null;

@override
E thisOrAncestorOfType<E extends Element>() => null;

@override
String toString() {
StringBuffer buffer = StringBuffer();
Expand Down
10 changes: 10 additions & 0 deletions pkg/analyzer/lib/src/dart/element/member.dart
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ abstract class Member implements Element {
*/
MapSubstitution get substitution => _substitution;

@Deprecated('Use either thisOrAncestorMatching or thisOrAncestorOfType')
@override
E getAncestor<E extends Element>(Predicate<Element> predicate) =>
declaration.getAncestor(predicate);
Expand All @@ -575,6 +576,14 @@ abstract class Member implements Element {
}
}

@override
E thisOrAncestorMatching<E extends Element>(Predicate<Element> predicate) =>
declaration.thisOrAncestorMatching(predicate);

@override
E thisOrAncestorOfType<E extends Element>() =>
declaration.thisOrAncestorOfType<E>();

@override
void visitChildren(ElementVisitor visitor) {
// There are no children to visit
Expand Down Expand Up @@ -840,6 +849,7 @@ class ParameterMember extends VariableMember
@override
T accept<T>(ElementVisitor<T> visitor) => visitor.visitParameterElement(this);

@Deprecated('Use either thisOrAncestorMatching or thisOrAncestorOfType')
@override
E getAncestor<E extends Element>(Predicate<Element> predicate) {
Element element = declaration.getAncestor(predicate);
Expand Down
3 changes: 1 addition & 2 deletions pkg/analyzer/lib/src/diagnostic/diagnostic_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ class DiagnosticFactory {
List<DiagnosticMessage> contextMessages;
int declarationOffset = staticElement.nameOffset;
if (declarationOffset >= 0 && staticElement != null) {
CompilationUnitElement unit = staticElement
.getAncestor((element) => element is CompilationUnitElement);
CompilationUnitElement unit = staticElement.thisOrAncestorOfType();
CharacterLocation location = unit.lineInfo.getLocation(declarationOffset);
contextMessages = [
DiagnosticMessageImpl(
Expand Down
4 changes: 2 additions & 2 deletions pkg/analyzer/lib/src/generated/element_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1225,8 +1225,8 @@ class ElementResolver extends SimpleAstVisitor<void> {
}
// The target has been found.
labelNode.staticElement = definingScope.element;
ExecutableElement labelContainer = definingScope.element
.getAncestor((element) => element is ExecutableElement);
ExecutableElement labelContainer =
definingScope.element.thisOrAncestorOfType();
if (!identical(labelContainer, _resolver.enclosingFunction)) {
_resolver.errorReporter.reportErrorForNode(
CompileTimeErrorCode.LABEL_IN_OUTER_SCOPE,
Expand Down

0 comments on commit 652926b

Please sign in to comment.