From 196d814232a2586f1a84f0fa3813f1ede6beb8aa Mon Sep 17 00:00:00 2001 From: fisker Date: Fri, 24 Jan 2025 18:54:51 +0800 Subject: [PATCH 1/2] Add internal rule `prefer-fixer-remove-range` --- scripts/internal-rules/index.js | 2 + .../prefer-fixer-remove-range.js | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 scripts/internal-rules/prefer-fixer-remove-range.js diff --git a/scripts/internal-rules/index.js b/scripts/internal-rules/index.js index fdaf8de5e6..64d70b1248 100644 --- a/scripts/internal-rules/index.js +++ b/scripts/internal-rules/index.js @@ -3,6 +3,7 @@ import packageJson from './package.json' with {type: 'json'}; import fixSnapshotTest from './fix-snapshot-test.js'; import noTestOnly from './no-test-only.js'; import preferNegativeBooleanAttribute from './prefer-negative-boolean-attribute.js'; +import preferFixerRemoveRange from './prefer-fixer-remove-range.js'; const pluginName = 'internal'; @@ -18,6 +19,7 @@ const rules = [ {id: 'fix-snapshot-test', directories: TEST_DIRECTORIES, rule: fixSnapshotTest}, {id: 'prefer-negative-boolean-attribute', directories: RULES_DIRECTORIES, rule: preferNegativeBooleanAttribute}, {id: 'no-test-only', directories: TEST_DIRECTORIES, rule: noTestOnly}, + {id: 'prefer-fixer-remove-range', directories: RULES_DIRECTORIES, rule: preferFixerRemoveRange}, ]; const isFileInsideDirectory = (filename, directory) => filename.startsWith(directory); diff --git a/scripts/internal-rules/prefer-fixer-remove-range.js b/scripts/internal-rules/prefer-fixer-remove-range.js new file mode 100644 index 0000000000..3d43824cd9 --- /dev/null +++ b/scripts/internal-rules/prefer-fixer-remove-range.js @@ -0,0 +1,47 @@ +import path from 'node:path'; +import {fileURLToPath} from 'node:url'; +import {isMethodCall, isLiteral} from '../../rules/ast/index.js'; +import {removeArgument} from '../../rules/fix/index.js'; + +const messageId = path.basename(fileURLToPath(import.meta.url), '.js'); + +const config = { + create(context) { + return { + CallExpression(callExpression) { + const [, emptyString] = callExpression.arguments; + + if (!( + isMethodCall(callExpression, { + object: 'fixer', + method: 'replaceTextRange', + argumentsLength: 2, + optionalCall: false, + optionalMember: false, + }) + && isLiteral(emptyString, '') + )) { + return; + } + + const {property} = callExpression.callee; + context.report({ + node: property, + messageId, + * fix(fixer) { + yield removeArgument(fixer, emptyString, context.sourceCode); + yield fixer.replaceText(property, 'removeRange'); + }, + }); + }, + }; + }, + meta: { + fixable: 'code', + messages: { + [messageId]: 'Prefer `fixer.removeRange(…)` over `fixer.replaceTextRange(…, \'\')`.', + }, + }, +}; + +export default config; From 59603d9b0d714f0fbfccd00f63f29448ea7e39e6 Mon Sep 17 00:00:00 2001 From: fisker Date: Fri, 24 Jan 2025 18:55:51 +0800 Subject: [PATCH 2/2] Apply fix --- rules/fix/remove-argument.js | 2 +- rules/prefer-array-some.js | 2 +- rules/prefer-object-from-entries.js | 2 +- rules/prefer-spread.js | 2 +- rules/prefer-switch.js | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/rules/fix/remove-argument.js b/rules/fix/remove-argument.js index 6f0a00d611..b333352e29 100644 --- a/rules/fix/remove-argument.js +++ b/rules/fix/remove-argument.js @@ -25,5 +25,5 @@ export default function removeArgument(fixer, node, sourceCode) { } /* c8 ignore end */ - return fixer.replaceTextRange([start, end], ''); + return fixer.removeRange([start, end]); } diff --git a/rules/prefer-array-some.js b/rules/prefer-array-some.js index 3025abbb3e..1863582ee5 100644 --- a/rules/prefer-array-some.js +++ b/rules/prefer-array-some.js @@ -79,7 +79,7 @@ const create = context => { } const parenthesizedRange = getParenthesizedRange(callExpression, context.sourceCode); - yield fixer.replaceTextRange([parenthesizedRange[1], callExpression.parent.range[1]], ''); + yield fixer.removeRange([parenthesizedRange[1], callExpression.parent.range[1]]); if (callExpression.parent.operator === '!=' || callExpression.parent.operator === '!==') { return; diff --git a/rules/prefer-object-from-entries.js b/rules/prefer-object-from-entries.js index d31aef6960..28893738e4 100644 --- a/rules/prefer-object-from-entries.js +++ b/rules/prefer-object-from-entries.js @@ -102,7 +102,7 @@ function fixReduceAssignOrSpread({sourceCode, callExpression, property}) { const startToken = sourceCode.getTokenBefore(firstToken); const [start] = startToken.range; const [, end] = lastToken.range; - return fixer.replaceTextRange([start, end], ''); + return fixer.removeRange([start, end]); }; function * removeFirstParameter(fixer) { diff --git a/rules/prefer-spread.js b/rules/prefer-spread.js index 360d79cc2b..57e885ce51 100644 --- a/rules/prefer-spread.js +++ b/rules/prefer-spread.js @@ -132,7 +132,7 @@ function fixConcat(node, sourceCode, fixableArguments) { const [leadingSpaces] = textAfter.match(/^\s*/); end += leadingSpaces.length; - return fixer.replaceTextRange([start, end], ''); + return fixer.removeRange([start, end]); } return function * (fixer) { diff --git a/rules/prefer-switch.js b/rules/prefer-switch.js index 398735f75d..138c1211b8 100644 --- a/rules/prefer-switch.js +++ b/rules/prefer-switch.js @@ -229,10 +229,10 @@ function fix({discriminant, ifStatements}, sourceCode, options) { if (alternate) { const [, start] = consequent.range; const [end] = alternate.range; - yield fixer.replaceTextRange([start, end], ''); + yield fixer.removeRange([start, end]); } - yield fixer.replaceTextRange(headRange, ''); + yield fixer.removeRange(headRange); for (const {left, right} of compareExpressions) { const node = isSame(left, discriminant) ? right : left; const text = sourceCode.getText(node);