-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
135 lines (124 loc) · 4.26 KB
/
index.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Generated by CoffeeScript 1.12.6
(function() {
var CallbackHandleError, getNodeType;
getNodeType = function(node) {
return node.constructor.name;
};
module.exports = CallbackHandleError = (function() {
function CallbackHandleError() {}
CallbackHandleError.prototype.rule = {
name: 'callback_handle_error',
level: 'error',
message: 'Error in callback not handled',
description: 'Finds instances of error objects passed through a callback not being handled',
patterns: ["^err(or)?", "[Ee]rr(or)?$"]
};
CallbackHandleError.prototype.lintAST = function(node, astApi) {
var pattern, patterns, ref;
this.astApi = astApi;
patterns = (ref = this.astApi.config.patterns) != null ? ref : this.rule.patterns;
this.errorVariablePatterns = (function() {
var i, len, results;
results = [];
for (i = 0, len = patterns.length; i < len; i++) {
pattern = patterns[i];
results.push(new RegExp(pattern));
}
return results;
})();
this.lintNode(node);
};
CallbackHandleError.prototype.lintNode = function(node) {
var i, j, len, len1, node_type, param, pattern, ref, ref1, ref2, var_name;
node_type = getNodeType(node);
switch (node_type) {
case 'Code':
ref = node.params;
for (i = 0, len = ref.length; i < len; i++) {
param = ref[i];
var_name = (ref1 = param.name) != null ? ref1.value : void 0;
ref2 = this.errorVariablePatterns;
for (j = 0, len1 = ref2.length; j < len1; j++) {
pattern = ref2[j];
if (pattern.test(var_name) && !this.handlesError(node, var_name)) {
this.throwError(node, "Error object '" + var_name + "' in callback not handled");
break;
}
}
}
}
node.eachChild((function(_this) {
return function(child) {
_this.lintNode(child);
};
})(this));
};
CallbackHandleError.prototype.handlesError = function(code_node, var_name) {
var found_usage;
found_usage = false;
code_node.traverseChildren(true, function(child) {
var arg, i, inner_child_type, j, len, len1, node_type, param, ref, ref1;
node_type = getNodeType(child);
switch (node_type) {
case 'If':
child.condition.traverseChildren(false, function(inner_child) {
var inner_type;
inner_type = getNodeType(inner_child);
switch (inner_type) {
case 'Literal':
case 'IdentifierLiteral':
if (inner_child.value === var_name) {
found_usage = true;
return false;
}
}
});
break;
case 'Call':
ref = child.args;
for (i = 0, len = ref.length; i < len; i++) {
arg = ref[i];
arg.traverseChildren(false, function(inner_child) {
var inner_type;
inner_type = getNodeType(inner_child);
switch (inner_type) {
case 'Literal':
case 'IdentifierLiteral':
if (inner_child.value === var_name) {
found_usage = true;
return false;
}
}
});
}
return;
case 'Code':
ref1 = child.params;
for (j = 0, len1 = ref1.length; j < len1; j++) {
param = ref1[j];
inner_child_type = getNodeType(param);
if (inner_child_type === 'Param') {
if (param.name.value === var_name) {
return false;
}
}
}
}
if (found_usage) {
return false;
}
});
return found_usage;
};
CallbackHandleError.prototype.throwError = function(node, message) {
var err;
err = this.astApi.createError({
lineNumber: node.locationData.first_line + 1,
message: message
});
this.errors.push(err);
};
return CallbackHandleError;
})();
}).call(this);
//# sourceMappingURL=index.js.map