Skip to content

Commit

Permalink
Extend ban rule to also bn global methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nina Hartmann committed Jul 17, 2016
1 parent ae4ef37 commit 13b6c3d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
39 changes: 29 additions & 10 deletions src/rules/banRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,18 @@ export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "ban",
description: "Bans the use of specific functions.",
descriptionDetails: "At this time, there is no way to disable global methods with this rule.",
description: "Bans the use of specific functions or global methods.",
optionsDescription: "A list of `['object', 'method']` pairs which ban `object.method()`.",
options: {
type: "list",
listType: {
type: "array",
arrayMembers: [
{ type: "string" },
{ type: "string" },
],
items: {type: "string"},
minLength: 1,
maxLength: 2,
},
},
optionExamples: [`[true, ["console", "log"], ["someObject", "someFunction"]]`],
optionExamples: [`[true, ["someGlobalMethod"], ["console", "log"], ["someObject", "someFunction"]]`],
type: "functionality",
};
/* tslint:enable:object-literal-sort-keys */
Expand All @@ -53,17 +51,29 @@ export class Rule extends Lint.Rules.AbstractRule {
}

export class BanFunctionWalker extends Lint.RuleWalker {
private bannedGlobalFunctions: string[] = [];
private bannedFunctions: string[][] = [];

public addBannedFunction(bannedFunction: string[]) {
this.bannedFunctions.push(bannedFunction);
if (bannedFunction.length === 1) {
this.bannedGlobalFunctions.push(bannedFunction[0]);
} else if (bannedFunction.length === 2) {
this.bannedFunctions.push(bannedFunction);
}
}

public visitCallExpression(node: ts.CallExpression) {
const expression = node.expression;

this.checkForObjectMethodBan(expression);
this.checkForGlobalBan(expression);

super.visitCallExpression(node);
}

private checkForObjectMethodBan(expression: ts.LeftHandSideExpression) {
if (expression.kind === ts.SyntaxKind.PropertyAccessExpression
&& expression.getChildCount() >= 3) {
&& expression.getChildCount() >= 3) {

const firstToken = expression.getFirstToken();
const firstChild = expression.getChildAt(0);
Expand Down Expand Up @@ -93,7 +103,16 @@ export class BanFunctionWalker extends Lint.RuleWalker {
}
}
}
}

super.visitCallExpression(node);
private checkForGlobalBan(expression: ts.LeftHandSideExpression) {
if (expression.kind === ts.SyntaxKind.Identifier) {
const identifierName = (<ts.Identifier> expression).text;
if (this.bannedGlobalFunctions.indexOf(identifierName) !== -1) {
this.addFailure(this.createFailure(expression.getStart(), expression.getWidth(),
`${Rule.FAILURE_STRING_PART}${identifierName}`));
}

}
}
}
10 changes: 10 additions & 0 deletions test/rules/ban/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ globals.getDocument().window.toString();
_.keys(obj).forEach(fun);
_.forEach(fun);
~~~~~~~~~ [function invocation disallowed: _.forEach]
describe("some text", () => {});
xdescribe("some text", () => {});
~~~~~~~~~ [function invocation disallowed: xdescribe]
fdescribe("some text", () => {});
~~~~~~~~~ [function invocation disallowed: fdescribe]
it("some text", () => {});
xit("some text", () => {});
~~~ [function invocation disallowed: xit]
fit("some text", () => {});
~~~ [function invocation disallowed: fit]
2 changes: 1 addition & 1 deletion test/rules/ban/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rules": {
"ban": [true, ["window", "toString"], ["_", "forEach"]]
"ban": [true, ["xit"], ["fit"], ["xdescribe"], ["fdescribe"], ["window", "toString"], ["_", "forEach"]]
}
}

0 comments on commit 13b6c3d

Please sign in to comment.