-
Notifications
You must be signed in to change notification settings - Fork 0
/
disallowIdentifierNamesWithMessage.js
106 lines (95 loc) · 2.46 KB
/
disallowIdentifierNamesWithMessage.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
/**
* Disallows a specified set of identifier names with custom error messages.
*
* Type: `Array`
*
* Values: Array of strings, which should be disallowed as identifier names
*
* #### Example
*
* ```js
*"disallowIdentifierNamesWithMessages": [
* "parseFloat",
* ["parseInt","parseInt(n) has unexpected behavior, use int(n) instead"]
*]
* ```
*
* ##### Valid
*
* ```js
*
*
*
* ```
*
* ##### Invalid
*
* ```js
*
*
*
* ```
*/
var assert = require('assert');
module.exports = function() {};
module.exports.prototype = {
configure : function(identifiers) {
var allowAfter;
assert(
Array.isArray(identifiers),
'disallowIdentifierNamesWithMessages option requires an array'
);
this._identifierIndex = {};
this._allowAfter = {};
for (var i = 0, l = identifiers.length; i < l; i++) {
allowAfter = [];
if (Array.isArray(identifiers[i])) {
this._identifierIndex[identifiers[i][0]] = identifiers[i][1];
allowAfter = (identifiers[i][2] instanceof Array)
? identifiers[i][2]
: (typeof identifiers[i][2] == 'undefined' || identifiers[i][2] === null)
? []
: [identifiers[i][2]];
this._allowAfter[identifiers[i][0]] = allowAfter;
}
else {
this._identifierIndex[identifiers[i]] = "Illegal Identifier Name: " + identifiers[i];
this._allowAfter[identifiers[i]] = [];
}
}
},
getOptionName : function() {
return 'disallowIdentifierNamesWithMessages';
},
check : function(file, errors) {
var disallowedIdentifiers = this._identifierIndex;
var allowList = this._allowAfter;
file.iterateNodesByType('Identifier', function(node) {
if (Object.prototype.hasOwnProperty.call(disallowedIdentifiers, node.name)) {
if (
node.parentNode
&& node.parentNode.object
&& node.parentNode.object.type == 'Identifier'
) {
if (
allowList[node.name].length === 1
&& allowList[node.name][0] === '*'
) return;
for(var i = 0;i < allowList[node.name].length;i++) {
if (node.parentNode.object.name == allowList[node.name][i]) {
return;
}
}
}
errors.add(disallowedIdentifiers[node.name], node.loc.start);
}
});
file.iterateNodesByType('MemberExpression', function(node) {
if (node.property.type === 'Literal') {
if (Object.prototype.hasOwnProperty.call(disallowedIdentifiers, node.property.value)) {
errors.add(disallowedIdentifiers[node.property.value], node.property.loc.start);
}
}
});
}
};