banRule.js
3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var ts = require("typescript");
var Lint = require("../lint");
var Rule = (function (_super) {
__extends(Rule, _super);
function Rule() {
_super.apply(this, arguments);
}
Rule.prototype.apply = function (sourceFile) {
var options = this.getOptions();
var banFunctionWalker = new BanFunctionWalker(sourceFile, options);
var functionsToBan = options.ruleArguments;
functionsToBan.forEach(function (f) { return banFunctionWalker.addBannedFunction(f); });
return this.applyWithWalker(banFunctionWalker);
};
Rule.metadata = {
ruleName: "ban",
description: "Bans the use of specific functions.",
descriptionDetails: "At this time, there is no way to disable global methods with this rule.",
optionsDescription: "A list of `['object', 'method', 'optional explanation here']` which ban `object.method()`.",
options: {
type: "list",
listType: {
type: "array",
arrayMembers: [
{ type: "string" },
{ type: "string" },
{ type: "string" },
],
},
},
optionExamples: ["[true, [\"someObject\", \"someFunction\"], [\"someObject\", \"otherFunction\", \"Optional explanation\"]]"],
type: "functionality",
};
Rule.FAILURE_STRING_FACTORY = function (expression, messageAddition) {
return "Calls to '" + expression + "' are not allowed." + (messageAddition ? " " + messageAddition : "");
};
return Rule;
}(Lint.Rules.AbstractRule));
exports.Rule = Rule;
var BanFunctionWalker = (function (_super) {
__extends(BanFunctionWalker, _super);
function BanFunctionWalker() {
_super.apply(this, arguments);
this.bannedFunctions = [];
}
BanFunctionWalker.prototype.addBannedFunction = function (bannedFunction) {
this.bannedFunctions.push(bannedFunction);
};
BanFunctionWalker.prototype.visitCallExpression = function (node) {
var expression = node.expression;
if (expression.kind === ts.SyntaxKind.PropertyAccessExpression
&& expression.getChildCount() >= 3) {
var firstToken = expression.getFirstToken();
var firstChild = expression.getChildAt(0);
var secondChild = expression.getChildAt(1);
var thirdChild = expression.getChildAt(2);
var rightSideExpression = thirdChild.getFullText();
var leftSideExpression = void 0;
if (firstChild.getChildCount() > 0) {
leftSideExpression = firstChild.getLastToken().getText();
}
else {
leftSideExpression = firstToken.getText();
}
if (secondChild.kind === ts.SyntaxKind.DotToken) {
for (var _i = 0, _a = this.bannedFunctions; _i < _a.length; _i++) {
var bannedFunction = _a[_i];
if (leftSideExpression === bannedFunction[0] && rightSideExpression === bannedFunction[1]) {
var failure = this.createFailure(expression.getStart(), expression.getWidth(), Rule.FAILURE_STRING_FACTORY(leftSideExpression + "." + rightSideExpression, bannedFunction[2]));
this.addFailure(failure);
}
}
}
}
_super.prototype.visitCallExpression.call(this, node);
};
return BanFunctionWalker;
}(Lint.RuleWalker));
exports.BanFunctionWalker = BanFunctionWalker;