Skip to content

Commit 7794dd6

Browse files
authored
fix: require returns
Require returns
2 parents 0f4a878 + a053125 commit 7794dd6

File tree

5 files changed

+76
-15
lines changed

5 files changed

+76
-15
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2648,7 +2648,18 @@ const quux = (bar) => bar.filter(({ corge }) => corge())
26482648
* @inheritdoc
26492649
*/
26502650
function quux (foo) {
2651-
return "test"
2651+
}
2652+
2653+
/**
2654+
* @override
2655+
*/
2656+
function quux (foo) {
2657+
}
2658+
2659+
/**
2660+
* @constructor
2661+
*/
2662+
function quux (foo) {
26522663
}
26532664
````
26542665

src/iterateJsdoc.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,14 @@ const curryUtils = (
4848
return jsdocUtils.getFunctionParameterNames(functionNode);
4949
};
5050

51-
utils.getFunctionSourceCode = function () {
51+
utils.getFunctionSourceCode = () => {
5252
return sourceCode.getText(functionNode);
5353
};
5454

55+
utils.isConstructor = () => {
56+
return functionNode.parent && functionNode.parent.kind === 'constructor';
57+
};
58+
5559
utils.getJsdocParameterNamesDeep = () => {
5660
return jsdocUtils.getJsdocParameterNamesDeep(jsdoc, utils.getPreferredTagName('param'));
5761
};
@@ -80,6 +84,10 @@ const curryUtils = (
8084
return jsdocUtils.isValidTag(name, additionalTagNames);
8185
};
8286

87+
utils.hasATag = (name) => {
88+
return jsdocUtils.hasATag(jsdoc, name);
89+
};
90+
8391
utils.hasTag = (name) => {
8492
return jsdocUtils.hasTag(jsdoc, name);
8593
};
@@ -124,20 +132,28 @@ const curryUtils = (
124132
return allowAugmentsExtendsWithoutParam;
125133
};
126134

127-
utils.classHasTag = (tagName) => {
135+
utils.getClassJsdocNode = () => {
128136
const greatGrandParent = ancestors.slice(-3)[0];
129137
const greatGrandParentValue = greatGrandParent && sourceCode.getFirstToken(greatGrandParent).value;
130138

131139
if (greatGrandParentValue === 'class') {
132140
const classJsdocNode = sourceCode.getJSDocComment(greatGrandParent);
133141

134-
if (classJsdocNode) {
135-
const indent = _.repeat(' ', classJsdocNode.loc.start.column);
136-
const classJsdoc = parseComment(classJsdocNode, indent);
142+
return classJsdocNode;
143+
}
137144

138-
if (jsdocUtils.hasTag(classJsdoc, tagName)) {
139-
return true;
140-
}
145+
return false;
146+
};
147+
148+
utils.classHasTag = (tagName) => {
149+
const classJsdocNode = utils.getClassJsdocNode();
150+
151+
if (classJsdocNode) {
152+
const indent = _.repeat(' ', classJsdocNode.loc.start.column);
153+
const classJsdoc = parseComment(classJsdocNode, indent);
154+
155+
if (jsdocUtils.hasTag(classJsdoc, tagName)) {
156+
return true;
141157
}
142158
}
143159

src/jsdocUtils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,18 @@ const hasTag = (jsdoc : Object, targetTagName : string) : boolean => {
8686
});
8787
};
8888

89+
const hasATag = (jsdoc : Object, targetTagNames : Array) : boolean => {
90+
return targetTagNames.some((targetTagName) => {
91+
return hasTag(jsdoc, targetTagName);
92+
});
93+
};
94+
8995
export default {
9096
getFunctionParameterNames,
9197
getJsdocParameterNames,
9298
getJsdocParameterNamesDeep,
9399
getPreferredTagName,
100+
hasATag,
94101
hasTag,
95102
isValidTag
96103
};

src/rules/requireReturns.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,21 @@ export default iterateJsdoc(({
66
report,
77
utils
88
}) => {
9-
// inheritdoc implies that all documentation is inherited
10-
// see http://usejsdoc.org/tags-inheritdoc.html
11-
//
12-
// As we do not know the parent method, we cannot perform any checks.
13-
if (utils.hasTag('inheritdoc')) {
9+
if (utils.hasATag([
10+
// inheritdoc implies that all documentation is inherited
11+
// see http://usejsdoc.org/tags-inheritdoc.html
12+
//
13+
// As we do not know the parent method, we cannot perform any checks.
14+
'inheritdoc',
15+
'override',
16+
17+
// A constructor function is assumed to return a class instance
18+
'constructor'
19+
])) {
20+
return;
21+
}
22+
23+
if (utils.isConstructor()) {
1424
return;
1525
}
1626

test/rules/assertions/requireReturns.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,24 @@ export default {
145145
* @inheritdoc
146146
*/
147147
function quux (foo) {
148-
return "test"
148+
}
149+
`
150+
},
151+
{
152+
code: `
153+
/**
154+
* @override
155+
*/
156+
function quux (foo) {
157+
}
158+
`
159+
},
160+
{
161+
code: `
162+
/**
163+
* @constructor
164+
*/
165+
function quux (foo) {
149166
}
150167
`
151168
}

0 commit comments

Comments
 (0)