Skip to content

Commit a405931

Browse files
HsuTinggajus
authored andcommitted
fix: modify require-return-type about annotateUndefined (#355)
* perf(require-return-type): use Promise<void> as void with async * style(requireReturnType): fix code style
1 parent ae28b63 commit a405931

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

src/rules/requireReturnType.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,14 @@ const create = (context) => {
5959
const getIsReturnTypeAnnotationUndefined = (targetNode) => {
6060
const isReturnTypeAnnotationLiteralUndefined = _.get(targetNode, 'functionNode.returnType.typeAnnotation.id.name') === 'undefined' && _.get(targetNode, 'functionNode.returnType.typeAnnotation.type') === 'GenericTypeAnnotation';
6161
const isReturnTypeAnnotationVoid = _.get(targetNode, 'functionNode.returnType.typeAnnotation.type') === 'VoidTypeAnnotation';
62-
63-
return isReturnTypeAnnotationLiteralUndefined || isReturnTypeAnnotationVoid;
62+
const isAsyncReturnTypeAnnotationVoid = _.get(targetNode, 'functionNode.async') &&
63+
_.get(targetNode, 'functionNode.returnType.typeAnnotation.id.name') === 'Promise' && (
64+
_.get(targetNode, 'functionNode.returnType.typeAnnotation.typeParameters.params[0].type') === 'VoidTypeAnnotation' ||
65+
_.get(targetNode, 'functionNode.returnType.typeAnnotation.typeParameters.params[0].id.name') === 'undefined' &&
66+
_.get(targetNode, 'functionNode.returnType.typeAnnotation.typeParameters.params[0].type') === 'GenericTypeAnnotation'
67+
);
68+
69+
return isReturnTypeAnnotationLiteralUndefined || isReturnTypeAnnotationVoid || isAsyncReturnTypeAnnotationVoid;
6470
};
6571

6672
const shouldFilterNode = (functionNode) => {
@@ -102,8 +108,7 @@ const create = (context) => {
102108

103109
const isArrow = functionNode.type === 'ArrowFunctionExpression';
104110
const isArrowFunctionExpression = functionNode.expression;
105-
const hasImplicitReturnType = functionNode.async || functionNode.generator;
106-
const isFunctionReturnUndefined = !isArrowFunctionExpression && !hasImplicitReturnType && (!targetNode.returnStatementNode || isUndefinedReturnType(targetNode.returnStatementNode));
111+
const isFunctionReturnUndefined = !isArrowFunctionExpression && !(functionNode.generator && !functionNode.async) && (!targetNode.returnStatementNode || isUndefinedReturnType(targetNode.returnStatementNode));
107112
const isReturnTypeAnnotationUndefined = getIsReturnTypeAnnotationUndefined(targetNode);
108113

109114
if (skipArrows === 'expressionsOnly' && isArrowFunctionExpression || skipArrows === true && isArrow) {

tests/rules/assertions/requireReturnType.js

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export default {
201201
code: 'async () => {}',
202202
errors: [
203203
{
204-
message: 'Missing return type annotation.'
204+
message: 'Must annotate undefined return type.'
205205
}
206206
],
207207
options: [
@@ -215,7 +215,7 @@ export default {
215215
code: 'async function x() {}',
216216
errors: [
217217
{
218-
message: 'Missing return type annotation.'
218+
message: 'Must annotate undefined return type.'
219219
}
220220
],
221221
options: [
@@ -226,52 +226,69 @@ export default {
226226
]
227227
},
228228
{
229-
code: 'class Test { constructor() { } }',
229+
code: 'async (): Promise<void> => { return; }',
230230
errors: [
231231
{
232-
message: 'Must annotate undefined return type.'
232+
message: 'Must not annotate undefined return type.'
233233
}
234234
],
235235
options: [
236236
'always',
237237
{
238-
annotateUndefined: 'always'
238+
annotateUndefined: 'never'
239239
}
240240
]
241241
},
242242
{
243-
code: 'class Test { foo() { return 42; } }',
243+
code: 'async (): Promise<undefined> => { return; }',
244244
errors: [
245245
{
246-
message: 'Missing return type annotation.'
246+
message: 'Must not annotate undefined return type.'
247+
}
248+
],
249+
options: [
250+
'always',
251+
{
252+
annotateUndefined: 'never'
247253
}
248254
]
249255
},
250256
{
251-
code: 'class Test { foo = () => { return 42; } }',
257+
code: 'class Test { constructor() { } }',
258+
errors: [
259+
{
260+
message: 'Must annotate undefined return type.'
261+
}
262+
],
263+
options: [
264+
'always',
265+
{
266+
annotateUndefined: 'always'
267+
}
268+
]
269+
},
270+
{
271+
code: 'class Test { foo() { return 42; } }',
252272
errors: [
253273
{
254274
message: 'Missing return type annotation.'
255275
}
256276
]
257277
},
258278
{
259-
code: 'class Test { foo = () => 42; }',
279+
code: 'class Test { foo = () => { return 42; } }',
260280
errors: [
261281
{
262282
message: 'Missing return type annotation.'
263283
}
264284
]
265285
},
266286
{
267-
code: 'async () => { return; }',
287+
code: 'class Test { foo = () => 42; }',
268288
errors: [
269289
{
270290
message: 'Missing return type annotation.'
271291
}
272-
],
273-
options: [
274-
'always'
275292
]
276293
},
277294
{

0 commit comments

Comments
 (0)