Skip to content

Commit 1a999e7

Browse files
committed
leadingUnderscoreAccess: added exception list, fixed location bug
Ref #68
1 parent 0510ad7 commit 1a999e7

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,8 @@ function _f() {}
493493

494494
Ensures access declaration is set for `_underscored` function names
495495

496+
Ignores a bunch of popular identifiers: `__filename`, `__dirname`, `__proto__`, `__defineGetter__`, `super_`, `__constructor`, etc.
497+
496498
Type: `Boolean` or `String`
497499

498500
Values: `true` (means not public), `"private"`, `"protected"`

lib/rules/validate-jsdoc/leading-underscore-access.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,30 @@ module.exports.options = {
77
}
88
};
99

10+
var nativeUnderscoredProperties = [
11+
// standard
12+
'__proto__',
13+
// underscore
14+
'_',
15+
// node.js
16+
'__filename',
17+
'__dirname',
18+
'super_', // util.inherits
19+
// moz
20+
'__count__',
21+
'__parent__',
22+
'__defineGetter__',
23+
'__defineSetter__',
24+
'__lookupGetter__',
25+
'__lookupSetter__',
26+
'__noSuchMethod__',
27+
// dfilatov/inherit
28+
'__constructor',
29+
'__self',
30+
'__base',
31+
'__parent',
32+
];
33+
1034
/**
1135
* validator for jsdoc data existance
1236
* @param {(FunctionDeclaration|FunctionExpression)} node
@@ -20,31 +44,43 @@ function validateLeadingUnderscoresAccess(node, err) {
2044

2145
// fetch name from variable, property or function
2246
var name;
47+
var nameLocation;
2348
switch (node.parentNode.type) {
2449
case 'VariableDeclarator':
2550
name = node.parentNode.id.name;
51+
nameLocation = node.parentNode.id.loc;
2652
break;
2753
case 'Property':
2854
name = node.parentNode.key.name;
55+
nameLocation = node.parentNode.key.loc;
2956
break;
3057
default: // try to use func name itself (if not anonymous)
3158
name = (node.id || {}).name;
59+
nameLocation = (node.id || {}).loc;
3260
break;
3361
}
3462

63+
if (nativeUnderscoredProperties.indexOf(name) !== -1) {
64+
return;
65+
}
66+
3567
// skip anonymous and names without underscores at begin
3668
if (!name || name[0] !== '_') {
3769
return;
3870
}
3971

4072
var access;
73+
var accessTag;
4174
node.jsdoc.iterate(function(tag) {
4275
if (!access && ['private', 'protected', 'public', 'access'].indexOf(tag.id) !== -1) {
4376
access = (tag.id === 'access' ? tag.name.value : tag.id);
77+
accessTag = tag;
4478
}
4579
});
4680

47-
if (!access || [true, access].indexOf(option) === -1) {
48-
err('Method access doesn\'t match');
81+
if (!access || !accessTag) {
82+
err('Missing access tag for ' + (name || 'anonymous function'), (nameLocation || node.loc).start);
83+
} else if ([true, access].indexOf(option) === -1) {
84+
err('Method access doesn\'t match', accessTag.loc);
4985
}
5086
}

test/lib/rules/validate-jsdoc/leading-underscore-access.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ describe('lib/rules/validate-jsdoc/leading-underscore-access', function () {
9494
function _funcName(p) {}
9595
},
9696
errors: [{
97-
line: 6,
98-
column: 0,
99-
message: 'Method access doesn\'t match',
97+
line: 9,
98+
column: 9,
99+
message: 'Missing access tag for _funcName',
100100
rule: 'jsDoc',
101101
filename: 'input'
102102
}]
@@ -180,7 +180,27 @@ describe('lib/rules/validate-jsdoc/leading-underscore-access', function () {
180180
*/
181181
var _pubFuncWithUnderscore = function (p) {};
182182
},
183-
errors: 2
183+
errors: [{
184+
column: 3, line: 2, filename: 'input', rule: 'jsDoc',
185+
message: 'Method access doesn\'t match'
186+
}, {
187+
column: 3, line: 7, filename: 'input', rule: 'jsDoc',
188+
message: 'Method access doesn\'t match'
189+
}]
190+
}, {
191+
it: 'should not report standard idendifiers',
192+
rules: {leadingUnderscoreAccess: 'protected'},
193+
code: function () {
194+
/**
195+
* @access private
196+
*/
197+
var super_ = function (p) {};
198+
199+
/**
200+
* @access public
201+
*/
202+
var __proto__ = function (p) {};
203+
}
184204
}
185205
/* jshint ignore:end */
186206
]);

0 commit comments

Comments
 (0)