Skip to content

Commit

Permalink
Merge b5c0a12 into 054e04b
Browse files Browse the repository at this point in the history
  • Loading branch information
pull[bot] committed Mar 14, 2020
2 parents 054e04b + b5c0a12 commit c71c59f
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 34 deletions.
4 changes: 2 additions & 2 deletions rules/filename-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ function fixFilename(words, caseFunctions, {leading, extension}) {
return combinations.map(parts => `${leading}${parts.join('')}${extension}`);
}

const leadingUnserscoresRegex = /^(?<leading>_+)(?<tailing>.*)$/;
const leadingUnderscoresRegex = /^(?<leading>_+)(?<tailing>.*)$/;
function splitFilename(filename) {
const result = leadingUnserscoresRegex.exec(filename) || {groups: {}};
const result = leadingUnderscoresRegex.exec(filename) || {groups: {}};
const {leading = '', tailing = filename} = result.groups;

const words = [];
Expand Down
25 changes: 2 additions & 23 deletions rules/no-keyword-prefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,6 @@ function findKeywordPrefix(name, options) {
});
}

function isInsideObjectPattern(node) {
let current = node;

while (current) {
const {parent} = current;

if (parent && parent.type === 'Property' && parent.computed && parent.key === current) {
return false;
}

if (current.type === 'ObjectPattern') {
return true;
}

current = parent;
}

return false;
}

function checkMemberExpression(report, node, options) {
const {name} = node;
const keyword = findKeywordPrefix(name, options);
Expand Down Expand Up @@ -90,7 +70,7 @@ function checkObjectPattern(report, node, options) {
const valueIsInvalid = node.parent.value.name && Boolean(keyword);

// Ignore destructuring if the option is set, unless a new identifier is created
if (valueIsInvalid && !(assignmentKeyEqualsValue && options.ignoreDestructuring)) {
if (valueIsInvalid && !assignmentKeyEqualsValue) {
report(node, keyword);
}

Expand Down Expand Up @@ -140,8 +120,7 @@ const create = context => {
}

if (
!options.checkProperties ||
(options.ignoreDestructuring && isInsideObjectPattern(node))
!options.checkProperties
) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions rules/no-nested-ternary.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const getDocumentationUrl = require('./utils/get-documentation-url');

const isParethesized = (sourceCode, node) => {
const isParenthesized = (sourceCode, node) => {
const previousToken = sourceCode.getTokenBefore(node);
const nextToken = sourceCode.getTokenAfter(node);

Expand Down Expand Up @@ -35,7 +35,7 @@ const create = context => {
) {
context.report({node, message});
break;
} else if (!isParethesized(sourceCode, childNode)) {
} else if (!isParenthesized(sourceCode, childNode)) {
context.report({
node: childNode,
message,
Expand Down
27 changes: 25 additions & 2 deletions rules/utils/avoid-capture.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,36 @@ const nameCollidesWithArgumentsSpecial = (name, scopes, isStrict) => {
return isStrict || scopes.some(scopeHasArgumentsSpecial);
};

/*
Unresolved reference is probably from the global scope. We should avoid using that name.
For example, like `foo` and `bar` below.
```
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 Expand Up @@ -62,7 +85,7 @@ Useful when you want to rename a variable (or create a new variable) while being
@param {Scope[]} scopes - The list of scopes the new variable will be referenced in.
@param {number} ecmaVersion - The language version, get it from `context.parserOptions.ecmaVersion`.
@param {isSafe} [isSafe] - Rule-specific name check function.
@returns {string} - Either `name` as is, or a string like `${name}_` suffixed with undescores to make the name unique.
@returns {string} - Either `name` as is, or a string like `${name}_` suffixed with underscores to make the name unique.
*/
module.exports = (name, scopes, ecmaVersion, isSafe = alwaysTrue) => {
const isStrict = someScopeIsStrict(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;
};
2 changes: 1 addition & 1 deletion test/lint/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const {

const hasFixable = fixableErrorCount || fixableWarningCount;

if (fix && hasFixable) {
if (fix) {
CLIEngine.outputFixes(report);
}

Expand Down
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 c71c59f

Please sign in to comment.