blob: 24021741047923822b4c4acfcde85d7db5eb4997 [file] [log] [blame]
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// 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';
extension ClassElementExtensions on ClassElement {
/// Return `true` if this element represents the class `Iterable` from
/// `dart:core`.
bool get isDartCoreIterable => name3 == 'Iterable' && library2.isDartCore;
/// Return `true` if this element represents the class `List` from
/// `dart:core`.
bool get isDartCoreList => name3 == 'List' && library2.isDartCore;
/// Return `true` if this element represents the class `Map` from
/// `dart:core`.
bool get isDartCoreMap => name3 == 'Map' && library2.isDartCore;
/// Return `true` if this element represents the class `Set` from
/// `dart:core`.
bool get isDartCoreSet => name3 == 'Set' && library2.isDartCore;
}
extension ElementExtensions on Element {
/// The content of the documentation comment (including delimiters) for this
/// element.
///
/// If the receiver is an element that has fragments, the comment will be a
/// concatenation of the comments from all of the fragments.
///
/// Returns `null` if the receiver does not have or does not support
/// documentation.
String? get documentationCommentOrNull {
return switch (this) {
Annotatable(:var documentationComment) => documentationComment,
_ => null,
};
}
/// 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 (this case Annotatable annotatable) {
if (annotatable.metadata.hasDeprecated) {
return true;
}
}
var ancestor = enclosingElement;
if (ancestor is InterfaceElement) {
if (ancestor.metadata.hasDeprecated) {
return true;
}
ancestor = ancestor.enclosingElement;
}
return ancestor is LibraryElement && ancestor.metadata.hasDeprecated;
}
/// Return this element and all its enclosing elements.
Iterable<Element> get withAncestors sync* {
var current = this;
while (true) {
yield current;
var enclosing = current.enclosingElement;
if (enclosing == null) {
break;
}
current = enclosing;
}
}
}
extension FragmentExtensions 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 MethodElementExtensions on MethodElement {
/// Return `true` if this element represents the method `cast` from either
/// `Iterable`, `List`, `Map`, or `Set`.
bool get isCastMethod {
if (name3 != 'cast') {
return false;
}
var definingClass = enclosingElement;
if (definingClass is! ClassElement) {
return false;
}
return definingClass.isDartCoreIterable ||
definingClass.isDartCoreList ||
definingClass.isDartCoreMap ||
definingClass.isDartCoreSet;
}
/// Return `true` if this element represents the method `toList` from
/// `Iterable`.
bool get isToListMethod {
if (name3 != 'toList') {
return false;
}
var definingClass = enclosingElement;
if (definingClass is! ClassElement) {
return false;
}
return definingClass.isDartCoreIterable;
}
/// Return `true` if this element represents the method `toSet` from
/// `Iterable`.
bool get isToSetMethod {
if (name3 != 'toSet') {
return false;
}
var definingClass = enclosingElement;
if (definingClass is! ClassElement) {
return false;
}
return definingClass.isDartCoreIterable;
}
}