Skip to content

Commit

Permalink
prevent-abbreviations: Fix inappropriate suggestion (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Mar 11, 2020
1 parent 7126d6d commit 2e20294
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
8 changes: 5 additions & 3 deletions rules/prevent-abbreviations.js
Expand Up @@ -668,6 +668,9 @@ const create = context => {
}

const scopes = variable.references.map(reference => reference.from).concat(variable.scope);
variableReplacements.samples = variableReplacements.samples.map(
name => avoidCapture(name, scopes, ecmaVersion, isSafeName)
);

const problem = {
node: definition.name,
Expand All @@ -676,20 +679,19 @@ const create = context => {

if (variableReplacements.total === 1 && shouldFix(variable)) {
const [replacement] = variableReplacements.samples;
const captureAvoidingReplacement = avoidCapture(replacement, scopes, ecmaVersion, isSafeName);

for (const scope of scopes) {
if (!scopeToNamesGeneratedByFixer.has(scope)) {
scopeToNamesGeneratedByFixer.set(scope, new Set());
}

const generatedNames = scopeToNamesGeneratedByFixer.get(scope);
generatedNames.add(captureAvoidingReplacement);
generatedNames.add(replacement);
}

problem.fix = fixer => {
return variableIdentifiers(variable)
.map(fixIdentifier(fixer, captureAvoidingReplacement));
.map(fixIdentifier(fixer, replacement));
};
}

Expand Down
37 changes: 35 additions & 2 deletions test/prevent-abbreviations.js
Expand Up @@ -775,7 +775,7 @@ ruleTester.run('prevent-abbreviations', rule, {
{
code: 'class Err {}',
output: 'class Error_ {}',
errors: createErrors('The variable `Err` should be named `Error`. A more descriptive name will do too.')
errors: createErrors('The variable `Err` should be named `Error_`. A more descriptive name will do too.')
},
{
code: 'class Cb {}',
Expand Down Expand Up @@ -828,7 +828,40 @@ ruleTester.run('prevent-abbreviations', rule, {
"use strict";
let package_;
`,
errors: createErrors()
errors: createErrors('The variable `pkg` should be named `package_`. A more descriptive name will do too.')
},
{
code: outdent`
"use strict";
let pkg = 1;
let package_ = 2;
`,
output: outdent`
"use strict";
let package__ = 1;
let package_ = 2;
`,
errors: createErrors('The variable `pkg` should be named `package__`. A more descriptive name will do too.')
},
{
code: outdent`
"use strict";
function foo() {
const args = [...arguments];
const pkg = 1;
}
`,
output: outdent`
"use strict";
function foo() {
const arguments_ = [...arguments];
const package_ = 1;
}
`,
errors: [
...createErrors('The variable `args` should be named `arguments_`. A more descriptive name will do too.'),
...createErrors('The variable `pkg` should be named `package_`. A more descriptive name will do too.')
]
},

{
Expand Down

0 comments on commit 2e20294

Please sign in to comment.