Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

prefer-for-of: No error if 'delete' is used #2458

Merged
merged 1 commit into from
Apr 4, 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
32 changes: 16 additions & 16 deletions src/rules/preferForOfRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ interface IncrementorState {
}

function walk(ctx: Lint.WalkContext<void>): void {
const { sourceFile } = ctx;
const scopes: IncrementorState[] = [];

return ts.forEachChild(ctx.sourceFile, cb);
return ts.forEachChild(sourceFile, cb);

function cb(node: ts.Node): void {
switch (node.kind) {
Expand Down Expand Up @@ -85,8 +86,8 @@ function walk(ctx: Lint.WalkContext<void>): void {

function visitIdentifier(node: ts.Identifier): void {
const state = getStateForVariable(node.text);
if (state) {
updateIncrementorState(node, state);
if (state !== undefined && state.onlyArrayReadAccess && isNonSimpleIncrementorUse(node, state.arrayExpr, sourceFile)) {
state.onlyArrayReadAccess = false;
}
}

Expand All @@ -101,22 +102,21 @@ function walk(ctx: Lint.WalkContext<void>): void {
}
}

function updateIncrementorState(node: ts.Identifier, state: IncrementorState): void {
function isNonSimpleIncrementorUse(node: ts.Identifier, arrayExpr: ts.Expression, sourceFile: ts.SourceFile): boolean {
// check if iterator is used for something other than reading data from array
const elementAccess = node.parent!;
if (!utils.isElementAccessExpression(elementAccess)) {
state.onlyArrayReadAccess = false;
return;
}
const accessParent = elementAccess.parent!;
return !utils.isElementAccessExpression(elementAccess)
// `a[i] = ...`
|| isAssignment(accessParent)
// `delete a[i]`
|| accessParent.kind === ts.SyntaxKind.DeleteExpression
// `b[i]`
|| !nodeEquals(arrayExpr, unwrapParentheses(elementAccess.expression), sourceFile);
}

const arrayExpr = unwrapParentheses(elementAccess.expression);
if (state.arrayExpr.getText() !== arrayExpr.getText()) {
// iterator used in array other than one iterated over
state.onlyArrayReadAccess = false;
} else if (isAssignment(elementAccess.parent!)) {
// array position is assigned a new value
state.onlyArrayReadAccess = false;
}
function nodeEquals(a: ts.Node, b: ts.Node, sourceFile: ts.SourceFile): boolean {
return a.getText(sourceFile) === b.getText(sourceFile);
}

// returns the iterator and array of a `for` loop if the `for` loop is basic.
Expand Down
5 changes: 5 additions & 0 deletions test/rules/prefer-for-of/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,9 @@ for (let shadow = 0; shadow < arr.length; shadow++) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]
}
}

for (let i = 0; i < arr.length; i++) {
delete arr[i];
}

[0]: Expected a 'for-of' loop instead of a 'for' loop with this simple iteration