Skip to content

Commit

Permalink
leadingUnderscoreAccess: added exception list, fixed location bug
Browse files Browse the repository at this point in the history
Ref #68
  • Loading branch information
qfox committed Jan 23, 2015
1 parent 0510ad7 commit 1a999e7
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,8 @@ function _f() {}

Ensures access declaration is set for `_underscored` function names

Ignores a bunch of popular identifiers: `__filename`, `__dirname`, `__proto__`, `__defineGetter__`, `super_`, `__constructor`, etc.

Type: `Boolean` or `String`

Values: `true` (means not public), `"private"`, `"protected"`
Expand Down
40 changes: 38 additions & 2 deletions lib/rules/validate-jsdoc/leading-underscore-access.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@ module.exports.options = {
}
};

var nativeUnderscoredProperties = [
// standard
'__proto__',
// underscore
'_',
// node.js
'__filename',
'__dirname',
'super_', // util.inherits
// moz
'__count__',
'__parent__',
'__defineGetter__',
'__defineSetter__',
'__lookupGetter__',
'__lookupSetter__',
'__noSuchMethod__',
// dfilatov/inherit
'__constructor',
'__self',
'__base',
'__parent',
];

/**
* validator for jsdoc data existance
* @param {(FunctionDeclaration|FunctionExpression)} node
Expand All @@ -20,31 +44,43 @@ function validateLeadingUnderscoresAccess(node, err) {

// fetch name from variable, property or function
var name;
var nameLocation;
switch (node.parentNode.type) {
case 'VariableDeclarator':
name = node.parentNode.id.name;
nameLocation = node.parentNode.id.loc;
break;
case 'Property':
name = node.parentNode.key.name;
nameLocation = node.parentNode.key.loc;
break;
default: // try to use func name itself (if not anonymous)
name = (node.id || {}).name;
nameLocation = (node.id || {}).loc;
break;
}

if (nativeUnderscoredProperties.indexOf(name) !== -1) {
return;
}

// skip anonymous and names without underscores at begin
if (!name || name[0] !== '_') {
return;
}

var access;
var accessTag;
node.jsdoc.iterate(function(tag) {
if (!access && ['private', 'protected', 'public', 'access'].indexOf(tag.id) !== -1) {
access = (tag.id === 'access' ? tag.name.value : tag.id);
accessTag = tag;
}
});

if (!access || [true, access].indexOf(option) === -1) {
err('Method access doesn\'t match');
if (!access || !accessTag) {
err('Missing access tag for ' + (name || 'anonymous function'), (nameLocation || node.loc).start);
} else if ([true, access].indexOf(option) === -1) {
err('Method access doesn\'t match', accessTag.loc);
}
}
28 changes: 24 additions & 4 deletions test/lib/rules/validate-jsdoc/leading-underscore-access.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ describe('lib/rules/validate-jsdoc/leading-underscore-access', function () {
function _funcName(p) {}
},
errors: [{
line: 6,
column: 0,
message: 'Method access doesn\'t match',
line: 9,
column: 9,
message: 'Missing access tag for _funcName',
rule: 'jsDoc',
filename: 'input'
}]
Expand Down Expand Up @@ -180,7 +180,27 @@ describe('lib/rules/validate-jsdoc/leading-underscore-access', function () {
*/
var _pubFuncWithUnderscore = function (p) {};
},
errors: 2
errors: [{
column: 3, line: 2, filename: 'input', rule: 'jsDoc',
message: 'Method access doesn\'t match'
}, {
column: 3, line: 7, filename: 'input', rule: 'jsDoc',
message: 'Method access doesn\'t match'
}]
}, {
it: 'should not report standard idendifiers',
rules: {leadingUnderscoreAccess: 'protected'},
code: function () {
/**
* @access private
*/
var super_ = function (p) {};

/**
* @access public
*/
var __proto__ = function (p) {};
}
}
/* jshint ignore:end */
]);
Expand Down

0 comments on commit 1a999e7

Please sign in to comment.