Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer-at: Remove auto-fix for arguments #1705

Merged
merged 2 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 42 additions & 27 deletions rules/prefer-at.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.