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
Changes from 1 commit
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
Next Next commit
Add noForIn rule
  • Loading branch information
Josh Pike committed May 29, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 0ee9fa293a7f8b369394ead813bcbb2df87cc294
57 changes: 57 additions & 0 deletions src/rules/noForInRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @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 = {
ruleName: 'no-for-in',
description: "Recommended the avoidance of 'for-in' statements. They can be replaced by Object.keys in a 'for-of' loop.",
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
optionsDescription: "Not configurable.",
options: null,
optionExamples: [true],
jpike88 marked this conversation as resolved.
Show resolved Hide resolved
type: "typescript",
typescriptOnly: false,
};

public static FAILURE_STRING_FACTORY(initializer: string, expression: string): string {
//tslint:disable-next-line:max-line-length
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<void>) {
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);
}