Skip to content

Commit 5d5dc85

Browse files
authored
Merge pull request #217 from brettz9/reworkRequireReturns
Rework require returns
2 parents 75b4b6f + 6af8fde commit 5d5dc85

File tree

7 files changed

+866
-99
lines changed

7 files changed

+866
-99
lines changed

README.md

Lines changed: 190 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2515,7 +2515,7 @@ The following patterns are considered problems:
25152515
function quux (foo) {
25162516

25172517
}
2518-
// Message: Present JSDoc @returns declaration but not available return expression in function.
2518+
// Message: JSDoc @returns declaration present but return expression not available in function.
25192519

25202520
/**
25212521
* @return
@@ -2524,13 +2524,13 @@ function quux (foo) {
25242524

25252525
}
25262526
// Settings: {"jsdoc":{"tagNamePreference":{"returns":"return"}}}
2527-
// Message: Present JSDoc @return declaration but not available return expression in function.
2527+
// Message: JSDoc @return declaration present but return expression not available in function.
25282528

25292529
/**
25302530
* @returns
25312531
*/
25322532
const quux = () => {}
2533-
// Message: Present JSDoc @returns declaration but not available return expression in function.
2533+
// Message: JSDoc @returns declaration present but return expression not available in function.
25342534

25352535
/**
25362536
* @returns {undefined} Foo.
@@ -2541,6 +2541,17 @@ function quux () {
25412541
return foo;
25422542
}
25432543
// Message: Found more than one @returns declaration.
2544+
2545+
const language = {
2546+
/**
2547+
* @param {string} name
2548+
* @returns {string}
2549+
*/
2550+
get name() {
2551+
this._name = name;
2552+
}
2553+
}
2554+
// Message: JSDoc @returns declaration present but return expression not available in function.
25442555
````
25452556

25462557
The following patterns are not considered problems:
@@ -2555,15 +2566,15 @@ function quux () {
25552566
}
25562567

25572568
/**
2558-
* @returns {void} Foo.
2569+
* @returns {string} Foo.
25592570
*/
25602571
function quux () {
25612572

25622573
return foo;
25632574
}
25642575

25652576
/**
2566-
* @returns {undefined} Foo.
2577+
* @returns {string} Foo.
25672578
*/
25682579
function quux () {
25692580

@@ -2599,7 +2610,12 @@ async function quux() {}
25992610
/**
26002611
* @returns {Promise<void>}
26012612
*/
2602-
async () => {}
2613+
const quux = async function () {}
2614+
2615+
/**
2616+
* @returns {Promise<void>}
2617+
*/
2618+
const quux = async () => {}
26032619

26042620
/**
26052621
* @returns Foo.
@@ -2608,6 +2624,33 @@ async () => {}
26082624
function quux () {
26092625
throw new Error('must be implemented by subclass!');
26102626
}
2627+
2628+
/**
2629+
* @returns Foo.
2630+
* @virtual
2631+
*/
2632+
function quux () {
2633+
throw new Error('must be implemented by subclass!');
2634+
}
2635+
2636+
/**
2637+
* @returns Foo.
2638+
* @constructor
2639+
*/
2640+
function quux () {
2641+
}
2642+
2643+
/**
2644+
* @returns {undefined} Foo.
2645+
*/
2646+
function quux () {
2647+
}
2648+
2649+
/**
2650+
* @returns {void} Foo.
2651+
*/
2652+
function quux () {
2653+
}
26112654
````
26122655

26132656

@@ -2662,6 +2705,42 @@ function quux (foo) {
26622705
}
26632706
// Settings: {"jsdoc":{"tagNamePreference":{"returns":"return"}}}
26642707
// Message: Missing JSDoc @return declaration.
2708+
2709+
/**
2710+
*
2711+
*/
2712+
async function quux() {}
2713+
// Message: Missing JSDoc @returns declaration.
2714+
2715+
/**
2716+
*
2717+
*/
2718+
const quux = async function () {}
2719+
// Message: Missing JSDoc @returns declaration.
2720+
2721+
/**
2722+
*
2723+
*/
2724+
const quux = async () => {}
2725+
// Message: Missing JSDoc @returns declaration.
2726+
2727+
/**
2728+
*
2729+
*/
2730+
function quux () {
2731+
}
2732+
// Settings: {"jsdoc":{"forceRequireReturn":true}}
2733+
// Message: Missing JSDoc @returns declaration.
2734+
2735+
const language = {
2736+
/**
2737+
* @param {string} name
2738+
*/
2739+
get name() {
2740+
return this._name;
2741+
}
2742+
}
2743+
// Message: Missing JSDoc @returns declaration.
26652744
````
26662745

26672746
The following patterns are not considered problems:
@@ -2710,16 +2789,121 @@ const quux = (bar) => bar.filter(({ corge }) => corge())
27102789
function quux (foo) {
27112790
}
27122791

2792+
/**
2793+
* @implements
2794+
*/
2795+
function quux (foo) {
2796+
}
2797+
2798+
/**
2799+
* @override
2800+
*/
2801+
function quux (foo) {
2802+
}
2803+
2804+
/**
2805+
* @constructor
2806+
*/
2807+
function quux (foo) {
2808+
}
2809+
27132810
/**
27142811
* @override
27152812
*/
27162813
function quux (foo) {
2814+
2815+
return foo;
2816+
}
2817+
2818+
/**
2819+
* @class
2820+
*/
2821+
function quux (foo) {
2822+
27172823
}
27182824

27192825
/**
27202826
* @constructor
27212827
*/
27222828
function quux (foo) {
2829+
2830+
}
2831+
2832+
/**
2833+
* @returns {Object}
2834+
*/
2835+
function quux () {
2836+
2837+
return {a: foo};
2838+
}
2839+
2840+
/**
2841+
* @returns {Object}
2842+
*/
2843+
const quux = () => ({a: foo});
2844+
2845+
/**
2846+
* @returns {Object}
2847+
*/
2848+
const quux = () => {
2849+
return {a: foo}
2850+
};
2851+
2852+
/**
2853+
* @returns {void}
2854+
*/
2855+
function quux () {
2856+
}
2857+
2858+
/**
2859+
* @returns {void}
2860+
*/
2861+
const quux = () => {
2862+
2863+
}
2864+
2865+
/**
2866+
* @returns {undefined}
2867+
*/
2868+
function quux () {
2869+
}
2870+
2871+
/**
2872+
* @returns {undefined}
2873+
*/
2874+
const quux = () => {
2875+
2876+
}
2877+
2878+
/**
2879+
*
2880+
*/
2881+
function quux () {
2882+
}
2883+
2884+
/**
2885+
*
2886+
*/
2887+
const quux = () => {
2888+
2889+
}
2890+
2891+
class Foo {
2892+
/**
2893+
*
2894+
*/
2895+
constructor () {
2896+
}
2897+
}
2898+
// Settings: {"jsdoc":{"forceRequireReturn":true}}
2899+
2900+
const language = {
2901+
/**
2902+
* @param {string} name
2903+
*/
2904+
set name(name) {
2905+
this._name = name;
2906+
}
27232907
}
27242908
````
27252909

src/iterateJsdoc.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ const curryUtils = (
4141
allowImplementsWithoutParam,
4242
allowAugmentsExtendsWithoutParam,
4343
checkSeesForNamepaths,
44+
forceRequireReturn,
4445
ancestors,
45-
sourceCode
46+
sourceCode,
47+
context
4648
) => {
4749
const utils = {};
4850

@@ -58,6 +60,10 @@ const curryUtils = (
5860
return node.parent && node.parent.kind === 'constructor';
5961
};
6062

63+
utils.isSetter = () => {
64+
return node.parent.kind === 'set';
65+
};
66+
6167
utils.getJsdocParameterNamesDeep = () => {
6268
return jsdocUtils.getJsdocParameterNamesDeep(jsdoc, utils.getPreferredTagName('param'));
6369
};
@@ -143,6 +149,32 @@ const curryUtils = (
143149
], tag.tag);
144150
};
145151

152+
utils.isArrowExpression = () => {
153+
return node.type === 'ArrowFunctionExpression' && node.expression;
154+
};
155+
156+
utils.hasDefinedTypeReturnTag = (tag) => {
157+
return jsdocUtils.hasDefinedTypeReturnTag(tag);
158+
};
159+
160+
utils.hasReturnValue = () => {
161+
return jsdocUtils.hasReturnValue(node, context);
162+
};
163+
164+
utils.getTags = (tagName) => {
165+
if (!jsdoc.tags) {
166+
return [];
167+
}
168+
169+
return jsdoc.tags.filter((item) => {
170+
return item.tag === tagName;
171+
});
172+
};
173+
174+
utils.isForceRequireReturn = () => {
175+
return forceRequireReturn;
176+
};
177+
146178
utils.getClassJsdocNode = () => {
147179
const greatGrandParent = ancestors.slice(-3)[0];
148180
const greatGrandParentValue = greatGrandParent && sourceCode.getFirstToken(greatGrandParent).value;
@@ -210,6 +242,7 @@ export default (iterator, options) => {
210242
const allowImplementsWithoutParam = Boolean(_.get(context, 'settings.jsdoc.allowImplementsWithoutParam'));
211243
const allowAugmentsExtendsWithoutParam = Boolean(_.get(context, 'settings.jsdoc.allowAugmentsExtendsWithoutParam'));
212244
const checkSeesForNamepaths = Boolean(_.get(context, 'settings.jsdoc.checkSeesForNamepaths'));
245+
const forceRequireReturn = Boolean(_.get(context, 'settings.jsdoc.forceRequireReturn'));
213246

214247
const checkJsdoc = (node) => {
215248
const jsdocNode = sourceCode.getJSDocComment(node);
@@ -277,6 +310,7 @@ export default (iterator, options) => {
277310
allowImplementsWithoutParam,
278311
allowAugmentsExtendsWithoutParam,
279312
checkSeesForNamepaths,
313+
forceRequireReturn,
280314
ancestors,
281315
sourceCode
282316
);

0 commit comments

Comments
 (0)