From dcb6993d6c377c5013051e4c894e90a49cda5e5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Kol=C3=A1=C5=99?= Date: Wed, 12 May 2021 20:52:13 +0200 Subject: [PATCH] Fix: no-unused-vars 1 arg arrow fns (#74) --- packages/autofix/lib/rules/no-unused-vars.js | 18 ++++++++++++-- .../autofix/tests/lib/rules/no-unused-vars.js | 24 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/packages/autofix/lib/rules/no-unused-vars.js b/packages/autofix/lib/rules/no-unused-vars.js index 1103470..55e6130 100644 --- a/packages/autofix/lib/rules/no-unused-vars.js +++ b/packages/autofix/lib/rules/no-unused-vars.js @@ -116,18 +116,32 @@ module.exports = ruleComposer.mapReports( return fixer.insertTextBefore(node, prefix); } case "after-used": { + const tokenBefore = sourceCode.getTokenBefore(node) + const tokenAfter = sourceCode.getTokenAfter(node) + + if ( + parent.params.length === 1 && + !(tokenBefore && tokenBefore.value === '(') && + !(tokenAfter && tokenAfter.value === ')') + ) { + return [ + fixer.insertTextBefore(node, '('), + fixer.insertTextAfter(node, ')'), + fixer.remove(node) + ] + } + const comma = sourceCode.getTokenBefore(node, commaFilter); if (comma && comma.range && grand.range && comma.range[0] >= grand.range[0]) { return [fixer.remove(comma), fixer.remove(node)]; } - const commaAfter = sourceCode.getTokenAfter(node, commaFilter); + const commaAfter = sourceCode.getTokenAfter(node, commaFilter); if (commaAfter && commaAfter.range && grand.range && commaAfter.range[1] <= grand.range[1]) { return [fixer.remove(node), fixer.remove(commaAfter)]; } - return [fixer.remove(node)]; } default: diff --git a/packages/autofix/tests/lib/rules/no-unused-vars.js b/packages/autofix/tests/lib/rules/no-unused-vars.js index 01ac803..25c793b 100644 --- a/packages/autofix/tests/lib/rules/no-unused-vars.js +++ b/packages/autofix/tests/lib/rules/no-unused-vars.js @@ -237,6 +237,30 @@ ruleTester.run("no-unused-vars", rule, { argsIgnorePattern: "^_" }] }, + { + code: "const foo = (a) => {}; foo();", + output: "const foo = () => {}; foo();", + parserOptions: { ecmaVersion: 2018 }, + errors: [ + { type: "Identifier" } + ], + options: [{ + args: "after-used", + argsIgnorePattern: "^_", + }] + }, + { + code: "const foo = a => {}; foo();", + output: "const foo = () => {}; foo();", + parserOptions: { ecmaVersion: 2018 }, + errors: [ + { type: "Identifier" } + ], + options: [{ + args: "after-used", + argsIgnorePattern: "^_", + }] + }, { code: "function foo(a, b, c){console.log(b);}; foo(1, 2, 3);", output: "function foo(_a, b, _c){console.log(b);}; foo(1, 2, 3);",