Skip to content

Commit f709d6c

Browse files
author
Alexej Yaroshevich
committed
fixes #7 add support of null and undefined return types
1 parent 57cebbf commit f709d6c

3 files changed

Lines changed: 91 additions & 2 deletions

File tree

lib/jsdoc-helpers.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ function jsDocSimplifyNode (node) {
6767
result.nullable = (node.type === 'NullableType');
6868
return result;
6969

70+
case 'NullLiteral':
71+
result = {type: 'null'};
72+
break;
73+
74+
case 'UndefinedLiteral':
75+
result = {type: 'undefined'};
76+
break;
77+
7078
case 'UnionType':
7179
result = [];
7280
for (var i = 0, l = node.elements.length; i < l; i += 1) {
@@ -116,10 +124,20 @@ function jsDocMatchType (variants, argument) {
116124
var i, l, variant, type;
117125

118126
for (i = 0, l = variants.length; i < l; i += 1) {
119-
variant = variants[i];
127+
variant = variants[i][0] || variants[i];
128+
if (variant.unknown || !variant.type) {
129+
continue;
130+
}
131+
120132
type = variant.type.toLowerCase();
121133

122134
if (argument.type === 'Literal') {
135+
if (argument.value === null) {
136+
return type === 'null';
137+
}
138+
if (argument.value === undefined) {
139+
return type === 'undefined';
140+
}
123141
if (typeof argument.value !== 'object') {
124142
return type === typeof argument.value;
125143
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "jscs-jsdoc",
33
"author": "Alexej Yaroshevich <[email protected]>",
44
"description": "JSCS jsdoc plugin",
5-
"version": "0.0.4",
5+
"version": "0.0.5",
66
"main": "lib/index",
77
"homepage": "https://github.com/zxqfox/jscs-jsdoc",
88
"license": "MIT",

test/test.validate-jsdoc-returns.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,75 @@ describe('rules/validate-jsdoc', function () {
229229

230230
});
231231

232+
describe('bugfixes', function () {
233+
234+
it('should not throw exception on `@returns {null|undefined}` directive. issue #7', function () {
235+
236+
checker.configure({ jsDoc: { requireReturnTypes: true, checkReturnTypes: true } });
237+
assert(
238+
checker.checkString(
239+
'/**\n' +
240+
' * @return {null}\n' +
241+
' */\n' +
242+
'function funcName() {\n' +
243+
'return null;\n' +
244+
'}\n' +
245+
'/**\n' +
246+
' * @return {undefined}\n' +
247+
' */\n' +
248+
'function funcName() {\n' +
249+
'return undefined;\n' +
250+
'}\n' +
251+
'/**\n' +
252+
' * @return {null|undefined}\n' +
253+
' */\n' +
254+
'function funcName(flag) {\n' +
255+
' if (flag) {\n' +
256+
' return null;\n' +
257+
' }\n' +
258+
' return undefined;\n' +
259+
'}\n'
260+
).isEmpty()
261+
);
262+
263+
});
264+
265+
it('should report on `@returns {null|undefined}` vs (string|number|regexp). issue #7', function () {
266+
checker.configure({ jsDoc: { requireReturnTypes: true, checkReturnTypes: true } });
267+
assert(
268+
checker.checkString(
269+
'/**\n' +
270+
' * @return {null|undefined}\n' +
271+
' */\n' +
272+
'function funcName(flag) {\n' +
273+
' if (flag) {\n' +
274+
' return /qwe/i;\n' +
275+
' } else {\n' +
276+
' return 2;\n' +
277+
' }\n' +
278+
' return "";\n' +
279+
'}\n'
280+
).getErrorCount() === 3
281+
);
282+
});
283+
284+
it('should report on `@returns {null|undefined}` vs (array|object). issue #7', function () {
285+
checker.configure({ jsDoc: { requireReturnTypes: true, checkReturnTypes: true } });
286+
assert(
287+
checker.checkString(
288+
'/**\n' +
289+
' * @return {null|undefined}\n' +
290+
' */\n' +
291+
'function funcName(flag) {\n' +
292+
' if (flag) {\n' +
293+
' return [];\n' +
294+
' }\n' +
295+
' return {q: 1};\n' +
296+
'}\n'
297+
).getErrorCount() === 2
298+
);
299+
});
300+
301+
});
302+
232303
});

0 commit comments

Comments
 (0)