Skip to content

Commit

Permalink
apply reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjuan committed Jan 24, 2024
1 parent 7844fb0 commit b176702
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 10 deletions.
42 changes: 32 additions & 10 deletions packages/eslint-plugin/src/rules/consistent-return.ts
Expand Up @@ -38,11 +38,18 @@ export default createRule<Options, MessageIds>({
const services = getParserServices(context);
const checker = services.program.getTypeChecker();
const rules = baseRule.create(context);
const functions: FunctionNode[] = [];

let functionNode: FunctionNode | null;
function enterFunction(node: FunctionNode): void {
functions.push(node);
}

function exitFunction(): void {
functions.pop();
}

function setFunctionNode(node: FunctionNode): void {
functionNode = node;
function getCurrentFunction(): FunctionNode | null {
return functions[functions.length - 1] ?? null;
}

function isReturnPromiseVoid(
Expand Down Expand Up @@ -79,14 +86,29 @@ export default createRule<Options, MessageIds>({

return {
...rules,
FunctionDeclaration: setFunctionNode,
FunctionExpression: setFunctionNode,
ArrowFunctionExpression: setFunctionNode,
FunctionDeclaration: enterFunction,
FunctionExpression: enterFunction,
ArrowFunctionExpression: enterFunction,
'FunctionDeclaration:exit'(node): void {
exitFunction();
rules['FunctionDeclaration:exit'](node);
},
'FunctionExpression:exit'(node): void {
exitFunction();
rules['FunctionExpression:exit'](node);
},
'ArrowFunctionExpression:exit'(node): void {
exitFunction();
rules['ArrowFunctionExpression:exit'](node);
},
ReturnStatement(node): void {
if (!node.argument && functionNode) {
if (isReturnVoidOrThenableVoid(functionNode)) {
return;
}
const functionNode = getCurrentFunction();
if (
!node.argument &&
functionNode &&
isReturnVoidOrThenableVoid(functionNode)
) {
return;
}
rules.ReturnStatement(node);
},
Expand Down
59 changes: 59 additions & 0 deletions packages/eslint-plugin/tests/rules/consistent-return.test.ts
Expand Up @@ -114,6 +114,32 @@ ruleTester.run('consistent-return', rule, {
}
}
`,
`
declare function bar(): void;
function foo(flag: boolean): void {
function fn(): string {
return '1';
}
if (flag) {
return bar();
}
return;
}
`,
`
class Foo {
foo(flag: boolean): void {
const bar = (): void => {
if (flag) return;
return this.foo();
};
if (flag) {
return this.bar();
}
return;
}
}
`,
],
invalid: [
{
Expand Down Expand Up @@ -155,6 +181,39 @@ ruleTester.run('consistent-return', rule, {
},
],
},
{
code: `
declare function foo(): void;
function bar(flag: boolean): undefined {
function baz(): undefined {
if (flag) return;
return undefined;
}
if (flag) return baz();
return;
}
`,
errors: [
{
messageId: 'unexpectedReturnValue',
data: { name: "Function 'baz'" },
type: AST_NODE_TYPES.ReturnStatement,
line: 6,
column: 13,
endLine: 6,
endColumn: 30,
},
{
messageId: 'missingReturnValue',
data: { name: "Function 'bar'" },
type: AST_NODE_TYPES.ReturnStatement,
line: 9,
column: 11,
endLine: 9,
endColumn: 18,
},
],
},
{
code: `
function foo(flag: boolean): Promise<void> {
Expand Down
5 changes: 5 additions & 0 deletions packages/eslint-plugin/typings/eslint-rules.d.ts
Expand Up @@ -37,6 +37,11 @@ declare module 'eslint/lib/rules/consistent-return' {
],
{
ReturnStatement(node: TSESTree.ReturnStatement): void;
'FunctionDeclaration:exit'(node: TSESTree.FunctionDeclaration): void;
'FunctionExpression:exit'(node: TSESTree.FunctionExpression): void;
'ArrowFunctionExpression:exit'(
node: TSESTree.ArrowFunctionExpression,
): void;
}
>;
export = rule;
Expand Down

0 comments on commit b176702

Please sign in to comment.