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

prevent-abbreviations: Add fn/func -> function, i/idx/j -> index replacements #836

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
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,15 @@
],
"rules": {
"strict": "error",
"unicorn/no-null": "error"
"unicorn/no-null": "error",
"unicorn/prevent-abbreviations": [
"error",
{
"replacements": {
"fn": false
}
}
]
}
}
}
4 changes: 2 additions & 2 deletions rules/prefer-reflect-apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const isApplySignature = (argument1, argument2) => (
)
);

const getReflectApplyCall = (sourceCode, func, receiver, arguments_) => (
`Reflect.apply(${sourceCode.getText(func)}, ${sourceCode.getText(receiver)}, ${sourceCode.getText(arguments_)})`
const getReflectApplyCall = (sourceCode, target, receiver, argumentsList) => (
`Reflect.apply(${sourceCode.getText(target)}, ${sourceCode.getText(receiver)}, ${sourceCode.getText(argumentsList)})`
);

const fixDirectApplyCall = (node, sourceCode) => {
Expand Down
15 changes: 15 additions & 0 deletions rules/prevent-abbreviations.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ const defaultReplacements = {
exts: {
extensions: true
},
fn: {
function: true
},
func: {
function: true
},
i: {
index: true
},
idx: {
index: true
},
j: {
index: true
},
len: {
length: true
},
Expand Down
4 changes: 2 additions & 2 deletions rules/utils/cartesian-product-samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ module.exports = (combinations, length = Infinity) => {
const samples = Array.from({length: Math.min(total, length)}, (_, sampleIndex) => {
let indexRemaining = sampleIndex;
const combination = [];
for (let i = combinations.length - 1; i >= 0; i--) {
const items = combinations[i];
for (let combinationIndex = combinations.length - 1; combinationIndex >= 0; combinationIndex--) {
const items = combinations[combinationIndex];
const {length} = items;
const index = indexRemaining % length;
indexRemaining = (indexRemaining - index) / length;
Expand Down
12 changes: 11 additions & 1 deletion test/lint/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@ const eslint = new ESLint({
ignorePatterns: [
'coverage',
'test/integration/fixtures'
]
],
rules: {
'unicorn/prevent-abbreviations': [
'error',
{
replacements: {
fn: false
}
}
]
}
}
});

Expand Down
80 changes: 80 additions & 0 deletions test/prevent-abbreviations.js
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,86 @@ ruleTester.run('prevent-abbreviations', rule, {
errors: createErrors()
},

// This test need run eslint 3 times to get the correct result
{
code: outdent`
function fn() {
for (let i = 0; i<10; i++) {
for (let j = 0; j<10; j++) {
console.log(i, j)
}
}
}
const func = fn;
`,
output: outdent`
function function_() {
for (let i = 0; i<10; i++) {
for (let j = 0; j<10; j++) {
console.log(i, j)
}
}
}
const func = function_;
`,
errors: [
...createErrors('The variable `fn` should be named `function_`. A more descriptive name will do too.'),
...createErrors('The variable `i` should be named `index`. A more descriptive name will do too.'),
...createErrors('The variable `j` should be named `index_`. A more descriptive name will do too.'),
...createErrors('The variable `func` should be named `function__`. A more descriptive name will do too.')
]
},
{
code: outdent`
function function_() {
for (let i = 0; i<10; i++) {
for (let j = 0; j<10; j++) {
console.log(i, j)
}
}
}
const func = function_;
`,
output: outdent`
function function_() {
for (let index = 0; index<10; index++) {
for (let j = 0; j<10; j++) {
console.log(index, j)
}
}
}
const function__ = function_;
`,
errors: [
...createErrors('The variable `i` should be named `index`. A more descriptive name will do too.'),
...createErrors('The variable `j` should be named `index_`. A more descriptive name will do too.'),
...createErrors('The variable `func` should be named `function__`. A more descriptive name will do too.')
]
},
{
code: outdent`
function function_() {
for (let index = 0; index<10; index++) {
for (let j = 0; j<10; j++) {
console.log(index, j)
}
}
}
const function__ = function_;
`,
output: outdent`
function function_() {
for (let index = 0; index<10; index++) {
for (let index_ = 0; index_<10; index_++) {
console.log(index, index_)
}
}
}
const function__ = function_;
`,
errors: createErrors('The variable `j` should be named `index_`. A more descriptive name will do too.')
},

// `package` is a reserved word in strict mode
{
code: 'let pkg',
Expand Down