@@ -2,7 +2,7 @@ module.exports = checkRedundantAccess;
22module . exports . scopes = [ 'function' ] ;
33
44module . exports . options = {
5- checkRedundantAccess : { allowedValues : [ true ] }
5+ checkRedundantAccess : { allowedValues : [ true , 'enforceLeadingUnderscore' , 'enforceTrailingUnderscore' ] }
66} ;
77
88/**
@@ -11,16 +11,14 @@ module.exports.options = {
1111 * @param {Function } err
1212 */
1313function checkRedundantAccess ( node , err ) {
14+ var enforceLeadingUnderscore = this . _options . checkRedundantAccess === 'enforceLeadingUnderscore' ;
15+ var enforceTrailingUnderscore = this . _options . checkRedundantAccess === 'enforceTrailingUnderscore' ;
1416 if ( ! node . jsdoc ) {
1517 return ;
1618 }
1719
1820 var access ;
19- node . jsdoc . iterate ( function ( tag ) {
20- if ( [ 'private' , 'protected' , 'public' , 'access' ] . indexOf ( tag . id ) === - 1 ) {
21- return ;
22- }
23-
21+ node . jsdoc . iterateByType ( [ 'private' , 'protected' , 'public' , 'access' ] , function ( tag ) {
2422 if ( access ) {
2523 err ( 'Multiple access definition' , tag . loc ) ;
2624 return ;
@@ -30,6 +28,32 @@ function checkRedundantAccess(node, err) {
3028 err ( 'Invalid access definition' , tag . loc ) ;
3129 }
3230
33- access = tag . id === 'access' ? tag . name : tag . id || 'unspecified' ;
31+ access = tag . id === 'access' ? tag . name . value : tag . id ;
3432 } ) ;
33+
34+ if ( access !== 'public' && ( enforceLeadingUnderscore || enforceTrailingUnderscore ) ) {
35+ // fetch name from variable, property or function
36+ var name ;
37+ var nameLocation ;
38+ switch ( node . parentNode . type ) {
39+ case 'VariableDeclarator' :
40+ name = node . parentNode . id . name ;
41+ nameLocation = node . parentNode . id . loc ;
42+ break ;
43+ case 'Property' :
44+ name = node . parentNode . key . name ;
45+ nameLocation = node . parentNode . key . loc ;
46+ break ;
47+ default : // try to use func name itself (if not anonymous)
48+ name = ( node . id || { } ) . name ;
49+ nameLocation = ( node . id || { } ) . loc ;
50+ break ;
51+ }
52+
53+ // skip anonymous and names without underscores at begin
54+ if ( name && name [ enforceLeadingUnderscore ? 0 : ( name . length - 1 ) ] !== '_' ) {
55+ err ( 'Missing ' + ( enforceLeadingUnderscore ? 'leading' : 'trailing' ) +
56+ ' underscore for ' + name , ( nameLocation || node . loc ) . start ) ;
57+ }
58+ }
3559}
0 commit comments