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

Add noForIn rule #4747

Merged
merged 29 commits into from Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/configs/all.ts
Expand Up @@ -51,6 +51,7 @@ export const rules = {
},
"no-any": true,
"no-empty-interface": true,
"no-for-in": true,
"no-import-side-effect": true,
// Technically this is not the strictest setting, but don't want to conflict with "typedef"
"no-inferrable-types": { options: ["ignore-params"] },
Expand Down
58 changes: 58 additions & 0 deletions src/rules/noForInRule.ts
@@ -0,0 +1,58 @@
/**
* @license
* Copyright 2013 Palantir Technologies, Inc.
jpike88 marked this conversation as resolved.
Show resolved Hide resolved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { isForInStatement } from "tsutils";
jpike88 marked this conversation as resolved.
Show resolved Hide resolved
import * as ts from "typescript";

import * as Lint from "../index";

export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
description:
jpike88 marked this conversation as resolved.
Show resolved Hide resolved
"Recommended the avoidance of 'for-in' statements. They can be replaced by Object.keys in a 'for-of' loop.",
jpike88 marked this conversation as resolved.
Show resolved Hide resolved
optionsDescription: "Not configurable.",
options: null,
optionExamples: [true],
jpike88 marked this conversation as resolved.
Show resolved Hide resolved
rationale:
"A for(... of ...) loop is easier to implement and read when a for(... in ...) loop, as for(... in ...) require a hasOwnProperty check on objects to ensure proper behaviour.",
jpike88 marked this conversation as resolved.
Show resolved Hide resolved
ruleName: "no-for-in",
type: "typescript",
typescriptOnly: false,
};

public static FAILURE_STRING_FACTORY(initializer: string, expression: string): string {
return `Do not use the 'for in' statement: 'for (${initializer} in ${expression})'. If this is an object, use 'Object.keys' instead. If this is an array use a standard 'for' or 'for of' loop instead.`;
jpike88 marked this conversation as resolved.
Show resolved Hide resolved
}

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk);
}
}

function walk(ctx: Lint.WalkContext) {
function cb(node: ts.Node): void {
if (isForInStatement(node)) {
const initializer: string = node.initializer.getText();
const expression: string = node.expression.getText();

const msg: string = Rule.FAILURE_STRING_FACTORY(initializer, expression);
jpike88 marked this conversation as resolved.
Show resolved Hide resolved
ctx.addFailureAt(node.getStart(), node.getWidth(), msg);
}
return ts.forEachChild(node, cb);
}

return ts.forEachChild(ctx.sourceFile, cb);
}
20 changes: 20 additions & 0 deletions test/rules/no-for-in/test.ts.lint
@@ -0,0 +1,20 @@
// this should pass
Copy link
Contributor

Choose a reason for hiding this comment

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

no need to write this, it's implied by the lack of lint failure marker

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
// this should pass

jpike88 marked this conversation as resolved.
Show resolved Hide resolved
const columns = [{ data: [1] }];
for (let i = 0; i < columns[0].data.length; i++) {
columns.map((x) => x.data[i]);
}

// this should NOT pass
jpike88 marked this conversation as resolved.
Show resolved Hide resolved
const object = {test:1, test2:1 ,test3:1};
jpike88 marked this conversation as resolved.
Show resolved Hide resolved
for (const key in object) {
~~~~~~~~~~~~~~~~~~~~~~~~~ [Expected a 'for-of' loop instead of a 'for-in' loop.]
if (object.hasOwnProperty(key)) {
const element = object[key];

}
}

// this should pass
jpike88 marked this conversation as resolved.
Show resolved Hide resolved
for (const key of Object.keys(object)) {
const value = object[key];
}
5 changes: 5 additions & 0 deletions test/rules/no-for-in/tslint.json
@@ -0,0 +1,5 @@
{
"rules": {
"no-for-in": true
}
}