-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15361c2
commit 76202c5
Showing
4 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const message = require('./CONST').MESSAGE.PREFER_AT; | ||
|
||
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') { | ||
context.report({ | ||
node, | ||
message, | ||
fix(fixer) { | ||
const arrayName = node.object.name; | ||
const indexValue = node.property.value; | ||
return fixer.replaceText(node, `${arrayName}.at(${indexValue})`); | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const RuleTester = require('eslint').RuleTester; | ||
const rule = require('../prefer-at'); | ||
const message = require('../CONST').MESSAGE.PREFER_AT; | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
sourceType: 'module', | ||
}, | ||
}); | ||
|
||
ruleTester.run('prefer-at', rule, { | ||
valid: [ | ||
{ | ||
code: 'example.at(0)', | ||
}, | ||
{ | ||
code: 'test.at(1)', | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: 'example[0]', | ||
output: 'example.at(0)', | ||
errors: [{ | ||
message, | ||
}], | ||
}, | ||
{ | ||
code: 'test[1]', | ||
output: 'test.at(1)', | ||
errors: [{ | ||
message, | ||
}], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters