Skip to content

Commit 298dd2e

Browse files
author
Alexej Yaroshevich
committed
enforceExistence: fixup enforce existence checking
- add tests for nested methods - add tests for variable expressions (aka privates) - add tests for objects properties - now rule checking not only node.id (function name) Fixes #21 Closes #22
1 parent af3f64d commit 298dd2e

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ module.exports.options = {
1010
* @param {Function} err
1111
*/
1212
function enforceExistence(node, err) {
13-
if (!node.jsDoc && node.id) {
13+
// if function is not anonymous or in variabledeclarator or in assignmentexpression
14+
var parentNode = node.parentNode || {};
15+
var needCheck = node.id ||
16+
parentNode.type === 'VariableDeclarator' ||
17+
parentNode.type === 'Property' ||
18+
parentNode.type === 'AssignmentExpression' && parentNode.operator === '=';
19+
20+
// and report unless jsdoc exists
21+
if (needCheck && !node.jsDoc) {
1422
err('jsdoc definition required', node.loc.start);
1523
}
1624
}

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,72 @@ describe('rules/validate-jsdoc', function () {
3636
function funcName(p) {
3737
}
3838
}
39+
}, {
40+
it: 'should report jsdoc absence for function expressions',
41+
errors: 1,
42+
code: function () {
43+
var myFunc = function (v) {
44+
};
45+
}
3946
}, {
4047
it: 'should not report jsdoc absence for anonymous functions',
4148
code: function () {
4249
[].forEach(function (v) {
4350
});
4451
}
52+
}, {
53+
it: 'should report nested jsdoc absence',
54+
errors: 4,
55+
code: function () {
56+
var MyNamespace = MyNamespace || {};
57+
58+
MyNamespace.Test = function () {
59+
};
60+
61+
MyNamespace.Test.Sub = {
62+
yellow: function () {
63+
}
64+
};
65+
66+
(function () {
67+
MyNamespace.Test.prototype.foo = function() {
68+
function bar() {
69+
}
70+
};
71+
})();
72+
}
73+
}, {
74+
it: 'shouldn\'t report nested jsdocs existence',
75+
code: function () {
76+
var MyNamespace = MyNamespace || {};
77+
78+
/**
79+
* Test
80+
*/
81+
MyNamespace.Test = function () {
82+
};
83+
84+
MyNamespace.Test.Sub = {
85+
/**
86+
* yellow submarine
87+
*/
88+
yellow: function () {
89+
}
90+
};
91+
92+
(function () {
93+
/**
94+
* Test.foo
95+
*/
96+
MyNamespace.Test.prototype.foo = function() {
97+
/**
98+
* bar
99+
*/
100+
function bar() {
101+
}
102+
};
103+
})();
104+
}
45105
}
46106
/* jshint ignore:end */
47107
]);

0 commit comments

Comments
 (0)