Skip to content

Commit 9c170c0

Browse files
author
Alexej Yaroshevich
committed
fixes #8 fixup nullable types support
1 parent f709d6c commit 9c170c0

3 files changed

Lines changed: 78 additions & 19 deletions

File tree

lib/jsdoc-helpers.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ function jsDocSimplifyNode (node) {
6464
case 'NullableType':
6565
case 'NonNullableType':
6666
result = jsDocSimplifyNode (node.expression);
67-
result.nullable = (node.type === 'NullableType');
67+
if (node.type === 'NullableType') {
68+
result.push({type: 'null'});
69+
}
6870
return result;
6971

7072
case 'NullLiteral':
@@ -121,42 +123,49 @@ function jsDocSimplifyNode (node) {
121123
* @param {Object} argument - esprima source code node
122124
*/
123125
function jsDocMatchType (variants, argument) {
124-
var i, l, variant, type;
126+
var i, l, variant, type, result = null;
125127

126128
for (i = 0, l = variants.length; i < l; i += 1) {
127129
variant = variants[i][0] || variants[i];
128130
if (variant.unknown || !variant.type) {
129-
continue;
131+
result = true;
132+
break;
130133
}
131134

132135
type = variant.type.toLowerCase();
133136

134137
if (argument.type === 'Literal') {
135138
if (argument.value === null) {
136-
return type === 'null';
137-
}
138-
if (argument.value === undefined) {
139-
return type === 'undefined';
140-
}
141-
if (typeof argument.value !== 'object') {
142-
return type === typeof argument.value;
143-
}
144-
if (!argument.value.type) {
145-
return type === (argument.value instanceof RegExp ? 'regexp' : 'object');
139+
result = result || (type === 'null');
140+
141+
} else if (argument.value === undefined) {
142+
result = result || (type === 'undefined');
143+
144+
} else if (typeof argument.value !== 'object') {
145+
result = result || (type === typeof argument.value);
146+
147+
} else if (!argument.value.type) {
148+
result = result || (type === (argument.value instanceof RegExp ? 'regexp' : 'object'));
149+
150+
} else {
151+
result = result || (type === argument.value.type);
146152
}
147-
return type === argument.value.type;
148153

149154
} else if (argument.type === 'ObjectExpression') {
150-
return (type === 'object');
155+
result = result || (type === 'object');
151156

152157
} else if (argument.type === 'ArrayExpression') {
153-
return (type === 'array');
158+
result = result || (type === 'array');
154159

155160
} else if (argument.type === 'NewExpression') {
156-
return (type === 'object') || (type === argument.callee.name.toLowerCase());
161+
result = result || ((type === 'object') || (type === argument.callee.name.toLowerCase()));
162+
}
163+
164+
if (result) {
165+
break;
157166
}
158167
}
159168

160169
// variables, expressions, another behavior
161-
return true;
170+
return result !== false;
162171
}

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.5",
5+
"version": "0.0.6",
66
"main": "lib/index",
77
"homepage": "https://github.com/zxqfox/jscs-jsdoc",
88
"license": "MIT",

test/test.validate-jsdoc-returns.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,56 @@ describe('rules/validate-jsdoc', function () {
298298
);
299299
});
300300

301+
it('should not report on `@returns {string|null}` vs (null). issue #8', function () {
302+
checker.configure({ jsDoc: { requireReturnTypes: true, checkReturnTypes: true } });
303+
assert(
304+
checker.checkString(
305+
'/**\n' +
306+
' * @return {string|null}\n' +
307+
' */\n' +
308+
'function funcName(flag) {\n' +
309+
' if (!this.something) {\n' +
310+
' return null;\n' +
311+
' }\n' +
312+
'}\n' +
313+
'/**\n' +
314+
' * @return {?string}\n' +
315+
' */\n' +
316+
'function funcName(flag) {\n' +
317+
' if (!this.something) {\n' +
318+
' return null;\n' +
319+
' }\n' +
320+
'}\n'
321+
).isEmpty()
322+
);
323+
});
324+
325+
it('should not report on `@returns {?number}` vs (null|number). issue #8', function () {
326+
checker.configure({ jsDoc: { requireReturnTypes: true, checkReturnTypes: true } });
327+
assert(
328+
checker.checkString(
329+
'/**\n' +
330+
' * @return {number|null}\n' +
331+
' */\n' +
332+
'function funcName(flag) {\n' +
333+
' if (!this.something) {\n' +
334+
' return null;\n' +
335+
' }\n' +
336+
' return 3;\n' +
337+
'}\n' +
338+
'/**\n' +
339+
' * @return {?number}\n' +
340+
' */\n' +
341+
'function funcName(flag) {\n' +
342+
' if (!this.something) {\n' +
343+
' return null;\n' +
344+
' }\n' +
345+
' return 3;\n' +
346+
'}\n'
347+
).isEmpty()
348+
);
349+
});
350+
301351
});
302352

303353
});

0 commit comments

Comments
 (0)