Skip to content

Commit e8e547a

Browse files
committed
Merge pull request #63 from zxqfox/feature/enforce-existance-except-exports
enforceExistence: add exceptExports value
2 parents 425e292 + 26a6dcf commit e8e547a

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ To use plugin you should add these lines to configuration file `.jscsrc`:
5555
"jscs-jsdoc"
5656
],
5757
"jsDoc": {
58-
"enforceExistence": true
58+
"checkAnnotations": "closurecompiler",
59+
"checkTypes": "strictNativeCase",
60+
"enforceExistence": "exceptExports"
5961
}
6062
}
6163
```
@@ -519,9 +521,9 @@ function _e() {}
519521

520522
Ensures jsdoc block exist
521523

522-
Type: `Boolean`
524+
Type: `Boolean` or `String`
523525

524-
Values: `true`
526+
Values: `true` or `"exceptExports"` (skip `module.exports = function () {};`)
525527

526528
Context: `functions`
527529

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = enforceExistence;
22
module.exports.scopes = ['function'];
33
module.exports.options = {
4-
enforceExistence: {allowedValues: [true]}
4+
enforceExistence: {allowedValues: [true, 'exceptExports']}
55
};
66

77
/**
@@ -10,15 +10,28 @@ module.exports.options = {
1010
* @param {Function} err
1111
*/
1212
function enforceExistence(node, err) {
13+
var reportExports = this._options.enforceExistence !== 'exceptExports';
14+
1315
// if function is not anonymous or in variabledeclarator or in assignmentexpression
1416
var parentNode = node.parentNode || {};
1517
var needCheck = node.id ||
1618
parentNode.type === 'VariableDeclarator' ||
1719
parentNode.type === 'Property' ||
1820
parentNode.type === 'AssignmentExpression' && parentNode.operator === '=';
1921

20-
// and report unless jsdoc exists
21-
if (needCheck && !node.jsdoc) {
22-
err('jsdoc definition required', node.loc.start);
22+
if (!reportExports && needCheck && parentNode.type === 'AssignmentExpression') {
23+
var left = parentNode.left;
24+
if ((left.object && left.object.name) === 'module' &&
25+
(left.property && left.property.name) === 'exports') {
26+
needCheck = false;
27+
}
28+
}
29+
30+
// skip unless jsdoc exists and check is needed
31+
if (node.jsdoc || !needCheck) {
32+
return;
2333
}
34+
35+
// report absence
36+
err('jsdoc definition required', node.loc.start);
2437
}

test/lib/rules/validate-jsdoc/enforce-existence.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,33 @@ describe('lib/rules/validate-jsdoc/enforce-existence', function () {
118118
};
119119
})();
120120
}
121+
}, {
122+
it: 'should report jsdocs existence for module.exports anonymous function',
123+
code: function () {
124+
module.exports = function () {
125+
};
126+
},
127+
errors: 1
121128
}
122129
/* jshint ignore:end */
123130
]);
124131

125132
});
133+
134+
describe('with exceptExports', function() {
135+
checker.rules({enforceExistence: 'exceptExports'});
136+
137+
checker.cases([
138+
/* jshint ignore:start */
139+
{
140+
it: 'should not report jsdocs existence for module.exports anonymous function',
141+
code: function () {
142+
module.exports = function () {
143+
};
144+
}
145+
}
146+
/* jshint ignore:end */
147+
]);
148+
});
149+
126150
});

0 commit comments

Comments
 (0)