Skip to content

Avoid calling indexOf when checking array element types #18619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
3 commits merged into from
Sep 22, 2017
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
39 changes: 22 additions & 17 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13198,16 +13198,11 @@ namespace ts {
// the type of the property with the numeric name N in T, if one exists. Otherwise, if T has a numeric index signature,
// it is the type of the numeric index signature in T. Otherwise, in ES6 and higher, the contextual type is the iterated
// type of T.
function getContextualTypeForElementExpression(node: Expression): Type {
const arrayLiteral = <ArrayLiteralExpression>node.parent;
const type = getApparentTypeOfContextualType(arrayLiteral);
if (type) {
const index = indexOf(arrayLiteral.elements, node);
return getTypeOfPropertyOfContextualType(type, "" + index as __String)
|| getIndexTypeOfContextualType(type, IndexKind.Number)
|| getIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false, /*allowAsyncIterables*/ false, /*checkAssignability*/ false);
}
return undefined;
function getContextualTypeForElementExpression(arrayContextualType: Type | undefined, index: number): Type | undefined {
return arrayContextualType && (
getTypeOfPropertyOfContextualType(arrayContextualType, "" + index as __String)
|| getIndexTypeOfContextualType(arrayContextualType, IndexKind.Number)
|| getIteratedTypeOrElementType(arrayContextualType, /*errorNode*/ undefined, /*allowStringInput*/ false, /*allowAsyncIterables*/ false, /*checkAssignability*/ false));
}

// In a contextually typed conditional expression, the true/false expressions are contextually typed by the same type.
Expand Down Expand Up @@ -13321,8 +13316,11 @@ namespace ts {
return getContextualTypeForObjectLiteralElement(<ObjectLiteralElementLike>parent);
case SyntaxKind.SpreadAssignment:
return getApparentTypeOfContextualType(parent.parent as ObjectLiteralExpression);
case SyntaxKind.ArrayLiteralExpression:
return getContextualTypeForElementExpression(node);
case SyntaxKind.ArrayLiteralExpression: {
const arrayLiteral = <ArrayLiteralExpression>parent;
const type = getApparentTypeOfContextualType(arrayLiteral);
return getContextualTypeForElementExpression(type, indexOfNode(arrayLiteral.elements, node));
}
case SyntaxKind.ConditionalExpression:
return getContextualTypeForConditionalOperand(node);
case SyntaxKind.TemplateSpan:
Expand Down Expand Up @@ -13451,12 +13449,14 @@ namespace ts {
(node.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>node).operatorToken.kind === SyntaxKind.EqualsToken);
}

function checkArrayLiteral(node: ArrayLiteralExpression, checkMode?: CheckMode): Type {
function checkArrayLiteral(node: ArrayLiteralExpression, checkMode: CheckMode | undefined): Type {
const elements = node.elements;
let hasSpreadElement = false;
const elementTypes: Type[] = [];
const inDestructuringPattern = isAssignmentTarget(node);
for (const e of elements) {
const contextualType = getApparentTypeOfContextualType(node);
for (let index = 0; index < elements.length; index++) {
const e = elements[index];
if (inDestructuringPattern && e.kind === SyntaxKind.SpreadElement) {
// Given the following situation:
// var c: {};
Expand All @@ -13478,7 +13478,8 @@ namespace ts {
}
}
else {
const type = checkExpressionForMutableLocation(e, checkMode);
const elementContextualType = getContextualTypeForElementExpression(contextualType, index);
const type = checkExpressionForMutableLocation(e, checkMode, elementContextualType);
elementTypes.push(type);
}
hasSpreadElement = hasSpreadElement || e.kind === SyntaxKind.SpreadElement;
Expand Down Expand Up @@ -18023,9 +18024,13 @@ namespace ts {
return false;
}

function checkExpressionForMutableLocation(node: Expression, checkMode?: CheckMode): Type {
function checkExpressionForMutableLocation(node: Expression, checkMode: CheckMode, contextualType?: Type): Type {
if (arguments.length === 2) {
contextualType = getContextualType(node);
}
const type = checkExpression(node, checkMode);
return isTypeAssertion(node) || isLiteralContextualType(getContextualType(node)) ? type : getWidenedLiteralType(type);
const shouldWiden = isTypeAssertion(node) || isLiteralContextualType(contextualType);
return shouldWiden ? type : getWidenedLiteralType(type);
}

function checkPropertyAssignment(node: PropertyAssignment, checkMode?: CheckMode): Type {
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);
}

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