Skip to content

Commit

Permalink
fix: remove arrow function expression parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
u3u committed Aug 8, 2023
1 parent 10c6388 commit 2e12fa4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/rules/arrow-return-style.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@ ruleTester.run(RULE_NAME, arrowReturnStyleRule, {
);
`,
},

{
code: 'const render = () => (<div />)',

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

output: 'const render = () => { return <div /> }',
},
],

valid: [
Expand Down
19 changes: 11 additions & 8 deletions src/rules/arrow-return-style.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { AST_NODE_TYPES, ASTUtils } from '@typescript-eslint/utils';
import { createRule } from '../utils/create-rule';

type Options = [
Expand Down Expand Up @@ -78,13 +78,16 @@ export const arrowReturnStyleRule = createRule<Options, MessageIds>({
context.report({
fix: (fixer) => {
const fixes = [];

if (isObjectLiteral()) {
fixes.push(
fixer.remove(sourceCode.getTokenBefore(arrowBody)!),
fixer.remove(sourceCode.getTokenAfter(arrowBody)!)
);
}
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)} }`));

Expand Down

0 comments on commit 2e12fa4

Please sign in to comment.