Skip to content

Commit

Permalink
Changes to help in excluding objects
Browse files Browse the repository at this point in the history
  • Loading branch information
ShridharGoel committed Jul 17, 2024
1 parent 6c9e140 commit baf7d27
Show file tree
Hide file tree
Showing 6 changed files with 362 additions and 21 deletions.
15 changes: 15 additions & 0 deletions eslint-plugin-expensify/prefer-at.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
const {ESLintUtils} = require('@typescript-eslint/utils');
const message = require('./CONST').MESSAGE.PREFER_AT;

module.exports = {
meta: {
fixable: 'code',
},
create(context) {
const parserServices = ESLintUtils.getParserServices(context);
const typeChecker = parserServices.program.getTypeChecker();

function isArrayType(node) {
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
const type = typeChecker.getTypeAtLocation(tsNode);
return typeChecker.isArrayType(type) || typeChecker.isTupleType(type);
}

function isLiteralNumber(node) {
return node && node.type === 'Literal' && typeof node.value === 'number';
}
Expand All @@ -21,6 +31,11 @@ module.exports = {

function checkNode(node) {
if (node.type === 'MemberExpression' && node.property) {
// Check if the object being accessed is an array
if (!isArrayType(node.object)) {
return;
}

// Handle items cases like test[0]
if (isLiteralNumber(node.property)) {
context.report({
Expand Down
Empty file.
34 changes: 25 additions & 9 deletions eslint-plugin-expensify/tests/prefer-at.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const RuleTester = require('eslint').RuleTester;
const RuleTester = require('@typescript-eslint/rule-tester').RuleTester;
const rule = require('../prefer-at');
const message = require('../CONST').MESSAGE.PREFER_AT;

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 6,
project: './tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
ecmaVersion: 2020,
},
});

Expand All @@ -29,6 +32,15 @@ ruleTester.run('prefer-at', rule, {
{
code: 'test.at((test.length - 1) / 2)',
},
{
code: `
const object = {0: 'v0', 1: 'v1', 2: 'v2'};
object[0]
`,
},
{
code: '[0, 1, 2].at(1)',
},
],
invalid: [
{
Expand All @@ -46,21 +58,25 @@ ruleTester.run('prefer-at', rule, {
}],
},
{
code: 'sample[sample.length-1]',
output: 'sample.at(-1)',
code: 'test[(test.length - 1) / 2]',
output: 'test.at((test.length - 1) / 2)',
errors: [{
message,
}],
},{
code: 'testing[testing.length-2]',
output: 'testing.at(-2)',
},
{
code: `
const sample = [];
sample[sample.length-1]
`,
output: 'sample.at(sample.length-1)',
errors: [{
message,
}],
},
{
code: 'test[(test.length - 1) / 2]',
output: 'test.at((test.length - 1) / 2)',
code: '[0, 1, 2][1]',
output: '[0, 1, 2].at(1)',
errors: [{
message,
}],
Expand Down
10 changes: 10 additions & 0 deletions eslint-plugin-expensify/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"sourceMap": true
},
"exclude": [
"node_modules"
]
}
Loading

0 comments on commit baf7d27

Please sign in to comment.