Skip to content

Commit 5f7d756

Browse files
author
Alexej Yaroshevich
committed
Fix: add support of es6 exports in enforceExistence
Fixes #159 Closes gh-160
1 parent 5df4bff commit 5f7d756

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

lib/rules/validate-jsdoc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ function patchNodesInFile(file) {
229229
*/
230230
function getJsdoc() {
231231
if (!this.hasOwnProperty('_jsdoc')) {
232-
var node = this.type === 'FunctionExpression' ? findFirstNodeInLine(this) : this;
232+
var node = this.type === 'FunctionExpression' || this.type === 'FunctionDeclaration' ?
233+
findFirstNodeInLine(this) : this;
233234
var res = findDocCommentBeforeNode(node);
234235
this._jsdoc = res ? jsdoc.createDocCommentByCommentNode(res) : null;
235236
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,14 @@ function enforceExistence(node, err) {
7272
}
7373
}
7474
if (policy.exports === false) {
75-
if (parentNode.type === 'AssignmentExpression') {
75+
if (parentNode.type === 'ExportDefaultDeclaration') {
76+
// don't check es6 export
77+
return;
78+
} else if (parentNode.type === 'AssignmentExpression') {
7679
var left = parentNode.left;
7780
if ((left.object && left.object.name) === 'module' &&
7881
(left.property && left.property.name) === 'exports') {
79-
// don't check exports functions
82+
// don't check commonjs exports format
8083
return;
8184
}
8285
}

test/init.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ function rulesChecker(opts) {
7070
*
7171
* @param {Object} rules
7272
*/
73-
rules: function(rules) {
73+
rules: function(rules, opts) {
7474
beforeEach(function() {
75-
checker.configure({jsDoc: rules});
75+
opts = opts || {};
76+
opts.jsDoc = rules;
77+
checker.configure(opts);
7678
});
7779
},
7880

@@ -109,7 +111,9 @@ function rulesChecker(opts) {
109111

110112
if (!test.hasOwnProperty('errors') || (typeof test.errors === 'number')) {
111113
expect(checked.getErrorCount())
112-
.to.eq(test.errors || 0);
114+
.to.eq(test.errors || 0, test.errors ?
115+
'Expect ' + test.errors + ' error(s)'
116+
: 'Unexpected error(s)');
113117
} else if (Array.isArray(test.errors)) {
114118
expect(errors)
115119
.to.containSubset(test.errors);

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,33 @@ describe('lib/rules/validate-jsdoc/enforce-existence', function () {
147147

148148
});
149149

150+
describe('with true and esnext', function() {
151+
checker.rules({enforceExistence: true}, {esnext: true});
152+
153+
checker.cases([
154+
/* jshint ignore:start */
155+
{
156+
it: 'should report jsdoc absence for named function export (#159)',
157+
code: [
158+
'export default function named (v) {',
159+
'};',
160+
].join('\n'),
161+
errors: 1,
162+
}, {
163+
it: 'should not report jsdoc absence for named function export (#159)',
164+
code: [
165+
'/**',
166+
' * Foo bar',
167+
' */',
168+
'export default function named (v) {',
169+
'};',
170+
].join('\n'),
171+
errors: 0,
172+
},
173+
/* jshint ignore:end */
174+
]);
175+
});
176+
150177
describe('with exceptExports', function() {
151178
checker.rules({enforceExistence: 'exceptExports'});
152179

@@ -163,6 +190,23 @@ describe('lib/rules/validate-jsdoc/enforce-existence', function () {
163190
]);
164191
});
165192

193+
describe('with exceptExports and esnext', function() {
194+
checker.rules({enforceExistence: 'exceptExports'}, {esnext: true});
195+
196+
checker.cases([
197+
/* jshint ignore:start */
198+
{
199+
it: 'should not report jsdoc absence for named function export (#159)',
200+
code: [
201+
'export default function named (v) {',
202+
'};',
203+
].join('\n'),
204+
errors: 0,
205+
}
206+
/* jshint ignore:end */
207+
]);
208+
});
209+
166210
describe('with allExcept exports', function() {
167211
checker.rules({enforceExistence: {allExcept: ['exports']}});
168212

0 commit comments

Comments
 (0)