Skip to content

Commit 203521d

Browse files
committed
checkRedundantAccess: added rule to enforcing dangling underscores
Fixes #70
1 parent 452fcbf commit 203521d

File tree

3 files changed

+133
-12
lines changed

3 files changed

+133
-12
lines changed

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,9 @@ var x = 1;
456456

457457
Reports redundant access declarations
458458

459-
Type: `Boolean`
459+
Type: `Boolean` or `String`
460460

461-
Values: `true`
461+
Values: `true` or `"enforceLeadingUnderscore"` or `"enforceTrailingUnderscore"`
462462

463463
Context: `functions`
464464

@@ -468,18 +468,24 @@ Tags: `access`, `private`, `protected`, `public`
468468

469469
```js
470470
"checkRedundantAccess": true
471+
"checkRedundantAccess": "enforceLeadingUnderscore"
471472
```
472473

473-
##### Valid
474+
##### Valid for true, "enforceLeadingUnderscore"
474475

475476
```js
476477
/**
477478
* @access private
478479
*/
479480
function _f() {}
481+
482+
/**
483+
* @access public
484+
*/
485+
function f() {}
480486
```
481487

482-
##### Invalid
488+
##### Invalid for true
483489

484490
```js
485491
/**
@@ -489,6 +495,15 @@ function _f() {}
489495
function _f() {}
490496
```
491497

498+
##### Invalid for "enforceLeadingUnderscore"
499+
500+
```js
501+
/**
502+
* @private
503+
*/
504+
function _f() {}
505+
```
506+
492507
### leadingUnderscoreAccess
493508

494509
Ensures access declaration is set for `_underscored` function names
@@ -506,7 +521,7 @@ Tags: `access`, `private`, `protected`, `public`
506521
#### Example
507522

508523
```js
509-
"checkRedundantAccess": "protected"
524+
"leadingUnderscoreAccess": "protected"
510525
```
511526

512527
##### Valid

lib/rules/validate-jsdoc/check-redundant-access.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = checkRedundantAccess;
22
module.exports.scopes = ['function'];
33

44
module.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
*/
1313
function 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
}

test/lib/rules/validate-jsdoc/check-redundant-access.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,86 @@ describe('lib/rules/validate-jsdoc/check-redundant-access', function () {
7272

7373
});
7474

75+
describe('with enforceLeadingUnderscore ', function() {
76+
checker.rules({checkRedundantAccess: 'enforceLeadingUnderscore'});
77+
78+
checker.cases([
79+
/* jshint ignore:start */
80+
{
81+
it: 'should not report valid jsdoc',
82+
code: function () {
83+
/**
84+
* @access protected
85+
*/
86+
function _funcName(p) {}
87+
},
88+
errors: []
89+
}, {
90+
it: 'should not force publics',
91+
code: function () {
92+
/**
93+
* @access public
94+
*/
95+
function funcName(p) {}
96+
},
97+
errors: []
98+
}, {
99+
it: 'should report missing leading underscore',
100+
code: function () {
101+
/**
102+
* @access protected
103+
*/
104+
function funcName(p) {}
105+
},
106+
errors: [{
107+
line: 4, column: 9, filename: 'input', rule: 'jsDoc',
108+
message: 'Missing leading underscore for funcName'
109+
}]
110+
}
111+
/* jshint ignore:end */
112+
]);
113+
114+
});
115+
116+
describe('with enforceTrailingUnderscore', function() {
117+
checker.rules({checkRedundantAccess: 'enforceTrailingUnderscore'});
118+
119+
checker.cases([
120+
/* jshint ignore:start */
121+
{
122+
it: 'should not report valid jsdoc',
123+
code: function () {
124+
/**
125+
* @access protected
126+
*/
127+
function funcName_(p) {}
128+
},
129+
errors: []
130+
}, {
131+
it: 'should not force publics',
132+
code: function () {
133+
/**
134+
* @access public
135+
*/
136+
function funcName(p) {}
137+
},
138+
errors: []
139+
}, {
140+
it: 'should report missing leading underscore',
141+
code: function () {
142+
/**
143+
* @access protected
144+
*/
145+
function funcName(p) {}
146+
},
147+
errors: [{
148+
line: 4, column: 9, filename: 'input', rule: 'jsDoc',
149+
message: 'Missing trailing underscore for funcName'
150+
}]
151+
}
152+
/* jshint ignore:end */
153+
]);
154+
155+
});
156+
75157
});

0 commit comments

Comments
 (0)