Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: modify require-return-type about annotateUndefined (#355)
* perf(require-return-type): use Promise<void> as void with async

* style(requireReturnType): fix code style
  • Loading branch information
HsuTing authored and gajus committed Jan 8, 2019
1 parent ae28b63 commit a405931
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
13 changes: 9 additions & 4 deletions src/rules/requireReturnType.js
Expand Up @@ -59,8 +59,14 @@ const create = (context) => {
const getIsReturnTypeAnnotationUndefined = (targetNode) => {
const isReturnTypeAnnotationLiteralUndefined = _.get(targetNode, 'functionNode.returnType.typeAnnotation.id.name') === 'undefined' && _.get(targetNode, 'functionNode.returnType.typeAnnotation.type') === 'GenericTypeAnnotation';
const isReturnTypeAnnotationVoid = _.get(targetNode, 'functionNode.returnType.typeAnnotation.type') === 'VoidTypeAnnotation';

return isReturnTypeAnnotationLiteralUndefined || isReturnTypeAnnotationVoid;
const isAsyncReturnTypeAnnotationVoid = _.get(targetNode, 'functionNode.async') &&
_.get(targetNode, 'functionNode.returnType.typeAnnotation.id.name') === 'Promise' && (
_.get(targetNode, 'functionNode.returnType.typeAnnotation.typeParameters.params[0].type') === 'VoidTypeAnnotation' ||
_.get(targetNode, 'functionNode.returnType.typeAnnotation.typeParameters.params[0].id.name') === 'undefined' &&
_.get(targetNode, 'functionNode.returnType.typeAnnotation.typeParameters.params[0].type') === 'GenericTypeAnnotation'
);

return isReturnTypeAnnotationLiteralUndefined || isReturnTypeAnnotationVoid || isAsyncReturnTypeAnnotationVoid;
};

const shouldFilterNode = (functionNode) => {
Expand Down Expand Up @@ -102,8 +108,7 @@ const create = (context) => {

const isArrow = functionNode.type === 'ArrowFunctionExpression';
const isArrowFunctionExpression = functionNode.expression;
const hasImplicitReturnType = functionNode.async || functionNode.generator;
const isFunctionReturnUndefined = !isArrowFunctionExpression && !hasImplicitReturnType && (!targetNode.returnStatementNode || isUndefinedReturnType(targetNode.returnStatementNode));
const isFunctionReturnUndefined = !isArrowFunctionExpression && !(functionNode.generator && !functionNode.async) && (!targetNode.returnStatementNode || isUndefinedReturnType(targetNode.returnStatementNode));
const isReturnTypeAnnotationUndefined = getIsReturnTypeAnnotationUndefined(targetNode);

if (skipArrows === 'expressionsOnly' && isArrowFunctionExpression || skipArrows === true && isArrow) {
Expand Down
43 changes: 30 additions & 13 deletions tests/rules/assertions/requireReturnType.js
Expand Up @@ -201,7 +201,7 @@ export default {
code: 'async () => {}',
errors: [
{
message: 'Missing return type annotation.'
message: 'Must annotate undefined return type.'
}
],
options: [
Expand All @@ -215,7 +215,7 @@ export default {
code: 'async function x() {}',
errors: [
{
message: 'Missing return type annotation.'
message: 'Must annotate undefined return type.'
}
],
options: [
Expand All @@ -226,52 +226,69 @@ export default {
]
},
{
code: 'class Test { constructor() { } }',
code: 'async (): Promise<void> => { return; }',
errors: [
{
message: 'Must annotate undefined return type.'
message: 'Must not annotate undefined return type.'
}
],
options: [
'always',
{
annotateUndefined: 'always'
annotateUndefined: 'never'
}
]
},
{
code: 'class Test { foo() { return 42; } }',
code: 'async (): Promise<undefined> => { return; }',
errors: [
{
message: 'Missing return type annotation.'
message: 'Must not annotate undefined return type.'
}
],
options: [
'always',
{
annotateUndefined: 'never'
}
]
},
{
code: 'class Test { foo = () => { return 42; } }',
code: 'class Test { constructor() { } }',
errors: [
{
message: 'Must annotate undefined return type.'
}
],
options: [
'always',
{
annotateUndefined: 'always'
}
]
},
{
code: 'class Test { foo() { return 42; } }',
errors: [
{
message: 'Missing return type annotation.'
}
]
},
{
code: 'class Test { foo = () => 42; }',
code: 'class Test { foo = () => { return 42; } }',
errors: [
{
message: 'Missing return type annotation.'
}
]
},
{
code: 'async () => { return; }',
code: 'class Test { foo = () => 42; }',
errors: [
{
message: 'Missing return type annotation.'
}
],
options: [
'always'
]
},
{
Expand Down

0 comments on commit a405931

Please sign in to comment.