Skip to content

Commit

Permalink
Merge 76b14da into 7aa2dc4
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Mar 13, 2020
2 parents 7aa2dc4 + 76b14da commit 84a0d8a
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 5 deletions.
21 changes: 20 additions & 1 deletion rules/utils/avoid-capture.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,32 @@ const nameCollidesWithArgumentsSpecial = (name, scopes, isStrict) => {
return isStrict || scopes.some(scopeHasArgumentsSpecial);
};

/*
Unresolved reference is probably from the global scope, should avoid use that name, like `foo` and `bar` bellow
function unicorn() {
return foo;
}
function unicorn() {
return function() {
return bar;
};
}
*/
const isUnresolvedName = (name, scopes) => scopes.some(scope =>
scope.references.some(reference => reference.identifier && reference.identifier.name === name && !reference.resolved) ||
isUnresolvedName(name, scope.childScopes)
);

const isSafeName = (name, scopes, ecmaVersion, isStrict) => {
ecmaVersion = Math.min(6, ecmaVersion); // 6 is the latest version understood by `reservedWords`

return (
!someScopeHasVariableName(name, scopes) &&
!reservedWords.check(name, ecmaVersion, isStrict) &&
!nameCollidesWithArgumentsSpecial(name, scopes, isStrict)
!nameCollidesWithArgumentsSpecial(name, scopes, isStrict) &&
!isUnresolvedName(name, scopes)
);
};

Expand Down
2 changes: 0 additions & 2 deletions rules/utils/resolve-variable-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ module.exports = (name, scope) => {

scope = scope.upper;
}

return undefined;
};
91 changes: 89 additions & 2 deletions test/prevent-abbreviations.js
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,94 @@ ruleTester.run('prevent-abbreviations', rule, {
`,
output: outdent`
function unicorn(unicorn) {
const {prop: property = {}} = unicorn;
const {prop: property_ = {}} = unicorn;
return property;
}
`,
errors: createErrors()
},
{
code: outdent`
const property = '';
function unicorn() {
const prop = 1;
return property;
}
`,
output: outdent`
const property = '';
function unicorn() {
const property_ = 1;
return property;
}
`,
errors: createErrors()
},
{
code: outdent`
function unicorn() {
const prop = 1;
return function () {
return property;
};
}
`,
output: outdent`
function unicorn() {
const property_ = 1;
return function () {
return property;
};
}
`,
errors: createErrors()
},
{
code: outdent`
let property;
function unicorn() {
const prop = 1;
return property;
}
`,
output: outdent`
let property;
function unicorn() {
const property_ = 1;
return property;
}
`,
errors: createErrors()
},
{
code: outdent`
/*global property:true*/
function unicorn() {
const prop = 1;
return property;
}
`,
output: outdent`
/*global property:true*/
function unicorn() {
const property_ = 1;
return property;
}
`,
errors: createErrors()
},
{
code: outdent`
/*global property:false*/
function unicorn() {
const prop = 1;
return property;
}
`,
output: outdent`
/*global property:false*/
function unicorn() {
const property_ = 1;
return property;
}
`,
Expand Down Expand Up @@ -1512,7 +1599,7 @@ babelRuleTester.run('prevent-abbreviations', rule, {
`,
output: outdent`
function unicorn(unicorn) {
const {prop: property = {}} = unicorn;
const {prop: property_ = {}} = unicorn;
return property;
}
`,
Expand Down

0 comments on commit 84a0d8a

Please sign in to comment.