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

Commit

Permalink
no-console: decouple from banRule, ban all methods by default (#2610)
Browse files Browse the repository at this point in the history
[rule-change] `no-console` bans all console methods when no methods are specified
Fixes: #2602
Fixes: #2492
  • Loading branch information
ajafff authored and nchen63 committed Apr 29, 2017
1 parent 58e6b86 commit 295bf29
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/rules/noConsoleRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
* limitations under the License.
*/

import { isCallExpression, isIdentifier, isPropertyAccessExpression } from "tsutils";
import * as ts from "typescript";

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

export class Rule extends BanRule.Rule {
export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "no-console",
description: "Bans the use of specified `console` methods.",
rationale: "In general, \`console\` methods aren't appropriate for production code.",
optionsDescription: "A list of method names to ban.",
optionsDescription: "A list of method names to ban. If no method names are provided, all console methods are banned.",
options: {
type: "array",
items: { type: "string" },
Expand All @@ -37,12 +37,25 @@ export class Rule extends BanRule.Rule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING_FACTORY(method: string) {
return `Calls to 'console.${method}' are not allowed.`;
}

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const options = this.getOptions();
const consoleBanWalker = new BanRule.BanFunctionWalker(sourceFile, this.getOptions());
for (const option of options.ruleArguments as string[]) {
consoleBanWalker.addBannedFunction(["console", option]);
}
return this.applyWithWalker(consoleBanWalker);
return this.applyWithFunction(sourceFile, walk, this.ruleArguments);
}
}

function walk(ctx: Lint.WalkContext<string[]>) {
return ts.forEachChild(ctx.sourceFile, function cb(node): void {
if (isCallExpression(node) &&
isPropertyAccessExpression(node.expression) &&
isIdentifier(node.expression.expression) &&
node.expression.expression.text === "console" &&
(ctx.options.length === 0 || ctx.options.indexOf(node.expression.name.text) !== -1)) {

ctx.addFailureAtNode(node.expression, Rule.FAILURE_STRING_FACTORY(node.expression.name.text));
}
return ts.forEachChild(node, cb);
});
}
20 changes: 20 additions & 0 deletions test/rules/no-console/all/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
console.time();
~~~~~~~~~~~~ [err % ('time')]
console.log("log");
~~~~~~~~~~~ [err % ('log')]
console.dir(object);
~~~~~~~~~~~ [err % ('dir')]
console.info("info");
~~~~~~~~~~~~ [err % ('info')]
console.trace("trace");
~~~~~~~~~~~~~ [err % ('trace')]
console.warn("warn");
~~~~~~~~~~~~ [err % ('warn')]
console.error("error");
~~~~~~~~~~~~~ [err % ('error')]
console.something();
~~~~~~~~~~~~~~~~~ [err % ('something')]
console.timeEnd();
~~~~~~~~~~~~~~~ [err % ('timeEnd')]

[err]: Calls to 'console.%s' are not allowed.
5 changes: 5 additions & 0 deletions test/rules/no-console/all/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-console": true
}
}
File renamed without changes.

0 comments on commit 295bf29

Please sign in to comment.