nodeInList<T extends AstNode> method

SourceRange nodeInList<T extends AstNode>(
  1. NodeList<T> list,
  2. T item
)

Return a source range that covers the given item (including a leading or trailing comma as appropriate) in the containing list.

Implementation

SourceRange nodeInList<T extends AstNode>(NodeList<T> list, T item) {
  if (list.length == 1) {
    var nextToken = item.endToken.next;
    if (nextToken?.type == TokenType.COMMA) {
      return startEnd(item, nextToken!);
    }
    var owner = list.owner;
    if (owner is ConstructorDeclaration) {
      var previousToSeparator = owner.separator?.previous;
      if (previousToSeparator != null) {
        return endStart(previousToSeparator, owner.body);
      }
    }
    return node(item);
  }
  final index = list.indexOf(item);
  if (index == 0) {
    // Remove the trailing comma.
    return startStart(item, list[1]);
  } else {
    // Remove the leading comma.
    return endEnd(list[index - 1], item);
  }
}