Skip to content

Commit

Permalink
prefer-at: Remove auto-fix for arguments (#1705)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 25, 2022
1 parent ab78c0d commit bba518e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 27 deletions.
69 changes: 42 additions & 27 deletions rules/prefer-at.js
Expand Up @@ -42,6 +42,7 @@ const indexAccess = [
].join('');
const sliceCall = methodCallSelector({method: 'slice', minimumArguments: 1, maximumArguments: 2});
const stringCharAt = methodCallSelector({method: 'charAt', argumentsLength: 1});
const isArguments = node => node.type === 'Identifier' && node.name === 'arguments';

const isLiteralNegativeInteger = node =>
node.type === 'UnaryExpression'
Expand Down Expand Up @@ -164,21 +165,28 @@ function create(context) {
}
}

return {
const problem = {
node: indexNode,
messageId: lengthNode ? MESSAGE_ID_NEGATIVE_INDEX : MESSAGE_ID_INDEX,
* fix(fixer) {
if (lengthNode) {
yield removeLengthNode(lengthNode, fixer, sourceCode);
}
};

if (isArguments(node.object)) {
return problem;
}

const openingBracketToken = sourceCode.getTokenBefore(indexNode, isOpeningBracketToken);
yield fixer.replaceText(openingBracketToken, '.at(');
problem.fix = function * (fixer) {
if (lengthNode) {
yield removeLengthNode(lengthNode, fixer, sourceCode);
}

const openingBracketToken = sourceCode.getTokenBefore(indexNode, isOpeningBracketToken);
yield fixer.replaceText(openingBracketToken, '.at(');

const closingBracketToken = sourceCode.getTokenAfter(indexNode, isClosingBracketToken);
yield fixer.replaceText(closingBracketToken, ')');
},
const closingBracketToken = sourceCode.getTokenAfter(indexNode, isClosingBracketToken);
yield fixer.replaceText(closingBracketToken, ')');
};

return problem;
},
[stringCharAt](node) {
const [indexNode] = node.arguments;
Expand Down Expand Up @@ -251,32 +259,39 @@ function create(context) {
return;
}

return {
const problem = {
node: node.callee,
messageId: MESSAGE_ID_GET_LAST_FUNCTION,
data: {description: matchedFunction.trim()},
fix(fixer) {
const [array] = node.arguments;
};

let fixed = getParenthesizedText(array, sourceCode);
const [array] = node.arguments;

if (
!isParenthesized(array, sourceCode)
&& shouldAddParenthesesToMemberExpressionObject(array, sourceCode)
) {
fixed = `(${fixed})`;
}
if (isArguments(array)) {
return problem;
}

fixed = `${fixed}.at(-1)`;
problem.fix = function (fixer) {
let fixed = getParenthesizedText(array, sourceCode);

if (
!isParenthesized(array, sourceCode)
&& shouldAddParenthesesToMemberExpressionObject(array, sourceCode)
) {
fixed = `(${fixed})`;
}

const tokenBefore = sourceCode.getTokenBefore(node);
if (needsSemicolon(tokenBefore, sourceCode, fixed)) {
fixed = `;${fixed}`;
}
fixed = `${fixed}.at(-1)`;

return fixer.replaceText(node, fixed);
},
const tokenBefore = sourceCode.getTokenBefore(node);
if (needsSemicolon(tokenBefore, sourceCode, fixed)) {
fixed = `;${fixed}`;
}

return fixer.replaceText(node, fixed);
};

return problem;
},
};
}
Expand Down
2 changes: 2 additions & 0 deletions test/prefer-at.mjs
Expand Up @@ -33,6 +33,7 @@ test.snapshot({
'const a = array[array.length - 1]',
'const {a = array[array.length - 1]} = {}',
'typeof array[array.length - 1]',
'function foo() {return arguments[arguments.length - 1]}',
],
});

Expand Down Expand Up @@ -170,6 +171,7 @@ test.snapshot({
code: '_.last(getLast(utils.lastOne(array)))',
options: [{getLastElementFunctions: ['getLast', ' utils.lastOne ']}],
},
'function foo() {return _.last(arguments)}',
],
});

Expand Down
20 changes: 20 additions & 0 deletions test/snapshots/prefer-at.mjs.md
Expand Up @@ -212,6 +212,16 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^ Prefer \`.at(…)\` over \`[….length - index]\`.␊
`

## Invalid #14
1 | function foo() {return arguments[arguments.length - 1]}

> Error 1/1
`␊
> 1 | function foo() {return arguments[arguments.length - 1]}␊
| ^^^^^^^^^^^^^^^^^^^^ Prefer \`.at(…)\` over \`[….length - index]\`.␊
`

## Invalid #1
1 | string.charAt(string.length - 1);

Expand Down Expand Up @@ -968,6 +978,16 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^ Prefer \`.at(-1)\` over \`utils.lastOne(…)\` to get the last element.␊
`

## Invalid #10
1 | function foo() {return _.last(arguments)}

> Error 1/1
`␊
> 1 | function foo() {return _.last(arguments)}␊
| ^^^^^^ Prefer \`.at(-1)\` over \`_.last(…)\` to get the last element.␊
`

## Invalid #1
1 | array[0]

Expand Down
Binary file modified test/snapshots/prefer-at.mjs.snap
Binary file not shown.

0 comments on commit bba518e

Please sign in to comment.