Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13319,7 +13319,7 @@ namespace ts {
case SyntaxKind.ArrayLiteralExpression: {
const arrayLiteral = <ArrayLiteralExpression>parent;
const type = getApparentTypeOfContextualType(arrayLiteral);
return getContextualTypeForElementExpression(type, indexOf(arrayLiteral.elements, node));
return getContextualTypeForElementExpression(type, indexOfNode(arrayLiteral.elements, node));
}
case SyntaxKind.ConditionalExpression:
return getContextualTypeForConditionalOperand(node);
Expand Down
12 changes: 12 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,18 @@ namespace ts {
return getSourceTextOfNodeFromSourceFile(getSourceFileOfNode(node), node, includeTrivia);
}

/**
* Note: it is expected that the `nodeArray` and the `node` are within the same file.
* For example, searching for a `SourceFile` in a `SourceFile[]` wouldn't work.
*/
export function indexOfNode(nodeArray: ReadonlyArray<Node>, node: Node) {
return binarySearch(nodeArray, node, compareNodePos);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One place this wouldn't work is with synthesized nodes. Not sure if those cases can pop up where it's been used so far.

}

function compareNodePos({ pos: aPos }: Node, { pos: bPos}: Node) {
return aPos < bPos ? Comparison.LessThan : bPos < aPos ? Comparison.GreaterThan : Comparison.EqualTo;
}

/**
* Gets flags that control emit behavior of a node.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ namespace ts.textChanges {
Debug.fail("node is not a list element");
return this;
}
const index = containingList.indexOf(node);
const index = indexOfNode(containingList, node);
if (index < 0) {
return this;
}
Expand Down Expand Up @@ -356,7 +356,7 @@ namespace ts.textChanges {
Debug.fail("node is not a list element");
return this;
}
const index = containingList.indexOf(after);
const index = indexOfNode(containingList, after);
if (index < 0) {
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ namespace ts {
}

const children = list.getChildren();
const listItemIndex = indexOf(children, node);
const listItemIndex = indexOfNode(children, node);

return {
listItemIndex,
Expand Down