Skip to content

Commit

Permalink
Update to include additional examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ShridharGoel committed Jul 8, 2024
1 parent 1db821d commit 6c9e140
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 9 deletions.
73 changes: 64 additions & 9 deletions eslint-plugin-expensify/prefer-at.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,78 @@ module.exports = {
meta: {
fixable: 'code',
},

create(context) {
return {
MemberExpression(node) {
// Check if we are accessing an array element directly
if (node.object.type === 'Identifier' && node.property.type === 'Literal' && typeof node.property.value === 'number') {
function isLiteralNumber(node) {
return node && node.type === 'Literal' && typeof node.value === 'number';
}

function isMemberExpressionWithLength(node) {
return (
node &&
node.type === 'MemberExpression' &&
node.property &&
node.property.type === 'Identifier' &&
node.property.name === 'length'
);
}

function checkNode(node) {
if (node.type === 'MemberExpression' && node.property) {
// Handle items cases like test[0]
if (isLiteralNumber(node.property)) {
context.report({
node,
message,
fix(fixer) {
return fixer.replaceText(node, `${node.object.name}.at(${node.property.value})`);
},
});
}

// Handle items like test[test.length - 1]
if (
node.property.type === 'BinaryExpression' &&
node.property.operator === '-' &&
isMemberExpressionWithLength(node.property.left) &&
isLiteralNumber(node.property.right)
) {
context.report({
node,
message,
fix(fixer) {
const arrayName = node.object.name;
const indexValue = node.property.value;
return fixer.replaceText(node, `${arrayName}.at(${indexValue})`);
const objectName = node.object.name;
const offset = node.property.right.value;
return fixer.replaceText(node, `${objectName}.at(-${offset})`);
},
});
}
},

// Handle items like test[(test.length - 1) / 2]
if (
node.property.type === 'BinaryExpression' &&
node.property.operator === '/' &&
node.property.left.type === 'BinaryExpression' &&
node.property.left.operator === '-' &&
isMemberExpressionWithLength(node.property.left.left) &&
isLiteralNumber(node.property.left.right) &&
isLiteralNumber(node.property.right)
) {
context.report({
node,
message,
fix(fixer) {
const objectName = node.object.name;
const leftValue = node.property.left.right.value;
const rightValue = node.property.right.value;
return fixer.replaceText(node, `${objectName}.at((${objectName}.length - ${leftValue}) / ${rightValue})`);
},
});
}
}
}

return {
MemberExpression: checkNode,
};
},
};
32 changes: 32 additions & 0 deletions eslint-plugin-expensify/tests/prefer-at.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ ruleTester.run('prefer-at', rule, {
{
code: 'test.at(1)',
},
{
code: 'sample.at(-1)',
},
{
code: 'testing.at(-2)',
},
{
code: 'testing.at(0).at(-1)',
},
{
code: 'test.at((test.length - 1) / 2)',
},
],
invalid: [
{
Expand All @@ -33,5 +45,25 @@ ruleTester.run('prefer-at', rule, {
message,
}],
},
{
code: 'sample[sample.length-1]',
output: 'sample.at(-1)',
errors: [{
message,
}],
},{
code: 'testing[testing.length-2]',
output: 'testing.at(-2)',
errors: [{
message,
}],
},
{
code: 'test[(test.length - 1) / 2]',
output: 'test.at((test.length - 1) / 2)',
errors: [{
message,
}],
},
],
});

0 comments on commit 6c9e140

Please sign in to comment.