forked from palantir/tslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ban rule for banning specified functions
- Loading branch information
Showing
6 changed files
with
112 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2013 Palantir Technologies, Inc. | ||
* | ||
* 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. | ||
*/ | ||
|
||
/// <reference path='../../lib/tslint.d.ts' /> | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
public static FAILURE_STRING_PART = "use of the following function is disallowed: "; | ||
|
||
public apply(syntaxTree: TypeScript.SyntaxTree): Lint.RuleFailure[] { | ||
var options = this.getOptions(); | ||
var banFunctionWalker = new BanFunctionWalker(syntaxTree, options); | ||
var functionsToBan = options; | ||
functionsToBan.forEach((functionToBan) => { | ||
banFunctionWalker.addBannedFunction(functionToBan); | ||
}); | ||
return this.applyWithWalker(banFunctionWalker); | ||
} | ||
} | ||
|
||
export class BanFunctionWalker extends Lint.RuleWalker { | ||
|
||
private bannedFunctions: string[][] = []; | ||
|
||
public addBannedFunction(bannedFunction: string[]) { | ||
this.bannedFunctions.push(bannedFunction); | ||
} | ||
|
||
public visitInvocationExpression(node: TypeScript.InvocationExpressionSyntax): void { | ||
var expression = node.expression; | ||
|
||
if (expression.kind() === TypeScript.SyntaxKind.MemberAccessExpression && | ||
expression.childCount() >= 3) { | ||
|
||
var firstToken = expression.firstToken(); | ||
var secondToken = expression.childAt(1); | ||
var thirdToken = expression.childAt(2); | ||
|
||
if (secondToken.kind() === TypeScript.SyntaxKind.DotToken) { | ||
this.bannedFunctions.forEach((bannedFunction) => { | ||
if (firstToken.text() === bannedFunction[0] && thirdToken.fullText() === bannedFunction[1]) { | ||
var position = this.position() + node.leadingTriviaWidth(); | ||
this.addFailure(this.createFailure(position, expression.width(), Rule.FAILURE_STRING_PART + | ||
firstToken.text() + "." + thirdToken.fullText())); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
super.visitInvocationExpression(node); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
console.time(); | ||
window.toString(); | ||
console.log(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2013 Palantir Technologies, Inc. | ||
* | ||
* 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. | ||
*/ | ||
|
||
/// <reference path='../references.ts' /> | ||
|
||
describe("<ban>", () => { | ||
it("bans access to specified functions", () => { | ||
var fileName = "rules/ban.test.ts"; | ||
var BanRule = Lint.Test.getRule("ban"); | ||
var dirFailure = Lint.Test.createFailuresOnFile(fileName, BanRule.FAILURE_STRING_PART + "window.toString")([2, 1], [2, 16]); | ||
|
||
var actualFailures = Lint.Test.applyRuleOnFile(fileName, BanRule, [true, ["window", "toString"]]); | ||
Lint.Test.assertContainsFailure(actualFailures, dirFailure); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters