Skip to content

Commit

Permalink
feat: if has comments also force explicit return
Browse files Browse the repository at this point in the history
  • Loading branch information
u3u committed Aug 9, 2023
1 parent 4d127ba commit c1a5a70
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 31 deletions.
38 changes: 36 additions & 2 deletions src/rules/arrow-return-style.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ ruleTester.run(RULE_NAME, arrowReturnStyleRule, {
code: dedent`
export const createRule = ESLintUtils.RuleCreator(
(rule) => \`https://github.com/u3u/eslint-plugin-arrow-return-style/tree/v\${version}/docs/rules/\${rule}.md\`
);
)
`,

errors: [{ messageId: 'useExplicitReturn' }],

output: dedent`
export const createRule = ESLintUtils.RuleCreator(
(rule) => { return \`https://github.com/u3u/eslint-plugin-arrow-return-style/tree/v\${version}/docs/rules/\${rule}.md\` }
);
)
`,
},

Expand All @@ -158,6 +158,40 @@ ruleTester.run(RULE_NAME, arrowReturnStyleRule, {
options: [{ jsxAlwaysUseExplicitReturn: true }],
output: 'const render = () => { return <div /> }',
},

{
code: dedent`
const fn = () =>
/* block comment */
1
`,

errors: [{ messageId: 'useExplicitReturn' }],

output: dedent`
const fn = () => {
/* block comment */
return 1
}
`,
},

{
code: dedent`
const test = () =>
// line comment
({ name: 'test' })
`,

errors: [{ messageId: 'useExplicitReturn' }],

output: dedent`
const test = () => {
// line comment
return { name: 'test' }
}
`,
},
],

valid: [
Expand Down
86 changes: 57 additions & 29 deletions src/rules/arrow-return-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ export const arrowReturnStyleRule = createRule<Options, MessageIds>({
return arrowFunctionParent.parent?.parent?.type === AST_NODE_TYPES.ExportNamedDeclaration;
};

const getArrowToken = () => {
const tokens = sourceCode.getTokens(arrowFunction);
const arrowToken = tokens.find(ASTUtils.isArrowToken);

return arrowToken;
};

const commentsExistBetweenArrowTokenAndArrowBody = () => {
const arrowToken = getArrowToken();

return arrowToken && sourceCode.commentsExistBetween(arrowToken, arrowBody);
};

if (arrowBody.type === AST_NODE_TYPES.BlockStatement) {
const blockBody = arrowBody.body;

Expand Down Expand Up @@ -75,35 +88,50 @@ export const arrowReturnStyleRule = createRule<Options, MessageIds>({
node: arrowFunction,
});
}
} else if (
//
isMaxLen() ||
isMultiline() ||
isNamedExport() ||
(jsxAlwaysUseExplicitReturn && isJsxElement())
) {
context.report({
fix: (fixer) => {
const fixes = [];
const firstToken = sourceCode.getTokenBefore(arrowBody);
const lastToken = sourceCode.getTokenAfter(arrowBody);

if (
firstToken &&
lastToken &&
ASTUtils.isOpeningParenToken(firstToken) &&
ASTUtils.isClosingParenToken(lastToken)
)
fixes.push(fixer.remove(firstToken), fixer.remove(lastToken));

fixes.push(fixer.replaceText(arrowBody, `{ return ${sourceCode.getText(arrowBody)} }`));

return fixes;
},

messageId: 'useExplicitReturn',
node: arrowFunction,
});
} else {
const commentsExist = commentsExistBetweenArrowTokenAndArrowBody();

if (
//
commentsExist ||
isMaxLen() ||
isMultiline() ||
isNamedExport() ||
(jsxAlwaysUseExplicitReturn && isJsxElement())
) {
context.report({
fix: (fixer) => {
const fixes = [];
const firstToken = sourceCode.getTokenBefore(arrowBody);
const lastToken = sourceCode.getTokenAfter(arrowBody);

if (
firstToken &&
lastToken &&
ASTUtils.isOpeningParenToken(firstToken) &&
ASTUtils.isClosingParenToken(lastToken)
)
fixes.push(fixer.remove(firstToken), fixer.remove(lastToken));

if (commentsExist) {
const arrowToken = getArrowToken()!;

fixes.push(
fixer.insertTextAfter(arrowToken, ' {'),
fixer.insertTextBefore(arrowBody, 'return '),
fixer.insertTextAfter(arrowBody, '\n}')
);
} else {
fixes.push(fixer.replaceText(arrowBody, `{ return ${sourceCode.getText(arrowBody)} }`));
}

return fixes;
},

messageId: 'useExplicitReturn',
node: arrowFunction,
});
}
}
},
};
Expand Down

0 comments on commit c1a5a70

Please sign in to comment.