This repository was archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathdisallow-dangling-underscores.js
110 lines (97 loc) · 2.67 KB
/
disallow-dangling-underscores.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
/**
* Disallows identifiers that start or end in `_`.
*
* Types: `Boolean` or `Object`
*
* Values:
* - `true`
* - `Object`:
* - `allExcept`: array of quoted identifiers
*
* JSHint: [`nomen`](http://www.jshint.com/docs/options/#nomen)
*
* Some popular identifiers are automatically listed as exceptions:
*
* - `__proto__` (javascript)
* - `_` (underscore.js)
* - `__filename` (node.js global)
* - `__dirname` (node.js global)
* - `super_` (node.js, used by
* [`util.inherits`](http://nodejs.org/docs/latest/api/util.html#util_util_inherits_constructor_superconstructor))
*
* #### Example
*
* ```js
* "disallowDanglingUnderscores": { "allExcept": ["_exception"] }
* ```
*
* ##### Valid
*
* ```js
* var x = 1;
* var o = obj.__proto__;
* var y = _.extend;
* var z = __dirname;
* var w = __filename;
* var x_y = 1;
* var v = _exception;
* ```
*
* ##### Invalid
*
* ```js
* var _x = 1;
* var x_ = 1;
* var x_y_ = 1;
* ```
*/
var assert = require('assert');
module.exports = function() {};
module.exports.prototype = {
configure: function(identifiers) {
assert(
identifiers === true ||
typeof identifiers === 'object',
this.getOptionName() + ' option requires the value `true` ' +
'or an object with String[] `allExcept` property'
);
// verify first item in `allExcept` property in object (if it's an object)
assert(
typeof identifiers !== 'object' ||
Array.isArray(identifiers.allExcept) &&
typeof identifiers.allExcept[0] === 'string',
'Property `allExcept` in ' + this.getOptionName() + ' should be an array of strings'
);
var isTrue = identifiers === true;
var defaultIdentifiers = [
'__proto__',
'_',
'__dirname',
'__filename',
'super_'
];
if (isTrue) {
identifiers = defaultIdentifiers;
} else {
identifiers = (identifiers.allExcept).concat(defaultIdentifiers);
}
this._identifierIndex = identifiers;
},
getOptionName: function() {
return 'disallowDanglingUnderscores';
},
check: function(file, errors) {
var allowedIdentifiers = this._identifierIndex;
file.iterateTokensByType('Identifier', function(token) {
var value = token.value;
if ((value[0] === '_' || value.slice(-1) === '_') &&
allowedIdentifiers.indexOf(value) < 0
) {
errors.add(
'Invalid dangling underscore found',
token
);
}
});
}
};