Skip to content

Commit

Permalink
Add internal rule prefer-fixer-remove-range (#2545)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Jan 24, 2025
1 parent 41548c4 commit 35fdb1c
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 6 deletions.
2 changes: 1 addition & 1 deletion rules/fix/remove-argument.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ export default function removeArgument(fixer, node, sourceCode) {
}
/* c8 ignore end */

return fixer.replaceTextRange([start, end], '');
return fixer.removeRange([start, end]);
}
2 changes: 1 addition & 1 deletion rules/prefer-array-some.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion rules/prefer-object-from-entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion rules/prefer-spread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions rules/prefer-switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions scripts/internal-rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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);
Expand Down
47 changes: 47 additions & 0 deletions scripts/internal-rules/prefer-fixer-remove-range.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 35fdb1c

Please sign in to comment.