Skip to content

Commit

Permalink
Chore: prefer smaller scope for variables in codebase (#9265)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Sep 9, 2017
1 parent 3693e4e commit f31f59d
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 27 deletions.
3 changes: 2 additions & 1 deletion lib/cli-engine.js
Expand Up @@ -668,7 +668,6 @@ class CLIEngine {
*/
getFormatter(format) {

let formatterPath;

// default is stylish
format = format || "stylish";
Expand All @@ -679,6 +678,8 @@ class CLIEngine {
// replace \ with / for Windows compatibility
format = format.replace(/\\/g, "/");

let formatterPath;

// if there's a slash, then it's a file
if (format.indexOf("/") > -1) {
const cwd = this.options ? this.options.cwd : process.cwd();
Expand Down
8 changes: 3 additions & 5 deletions lib/code-path-analysis/code-path-state.js
Expand Up @@ -843,15 +843,14 @@ class CodePathState {
* This segment will leave at the end of this finally block.
*/
const segments = forkContext.makeNext(-1, -1);
let j;

for (let i = 0; i < forkContext.count; ++i) {
const prevSegsOfLeavingSegment = [headOfLeavingSegments[i]];

for (j = 0; j < returned.segmentsList.length; ++j) {
for (let j = 0; j < returned.segmentsList.length; ++j) {
prevSegsOfLeavingSegment.push(returned.segmentsList[j][i]);
}
for (j = 0; j < thrown.segmentsList.length; ++j) {
for (let j = 0; j < thrown.segmentsList.length; ++j) {
prevSegsOfLeavingSegment.push(thrown.segmentsList[j][i]);
}

Expand Down Expand Up @@ -985,7 +984,6 @@ class CodePathState {

const forkContext = this.forkContext;
const brokenForkContext = this.popBreakContext().brokenForkContext;
let choiceContext;

// Creates a looped path.
switch (context.type) {
Expand All @@ -1000,7 +998,7 @@ class CodePathState {
break;

case "DoWhileStatement": {
choiceContext = this.popChoiceContext();
const choiceContext = this.popChoiceContext();

if (!choiceContext.processed) {
choiceContext.trueForkContext.add(forkContext.head);
Expand Down
6 changes: 2 additions & 4 deletions lib/config/autoconfig.js
Expand Up @@ -269,10 +269,8 @@ class Registry {
* @returns {Registry} New registry with errorCount populated
*/
lintSourceCode(sourceCodes, config, cb) {
let ruleSetIdx,
lintedRegistry;
let lintedRegistry = new Registry();

lintedRegistry = new Registry();
lintedRegistry.rules = Object.assign({}, this.rules);

const ruleSets = lintedRegistry.buildRuleSets();
Expand All @@ -287,7 +285,7 @@ class Registry {
filenames.forEach(filename => {
debug(`Linting file: ${filename}`);

ruleSetIdx = 0;
let ruleSetIdx = 0;

ruleSets.forEach(ruleSet => {
const lintConfig = Object.assign({}, config, { rules: ruleSet });
Expand Down
10 changes: 6 additions & 4 deletions lib/config/config-initializer.js
Expand Up @@ -382,7 +382,6 @@ function hasESLintVersionConflict(answers) {
* @returns {Promise} The promise with the result of the prompt
*/
function promptUser() {
let config;

return inquirer.prompt([
{
Expand Down Expand Up @@ -469,7 +468,8 @@ function promptUser() {
earlyAnswers.styleguide = "airbnb-base";
}

config = getConfigForStyleGuide(earlyAnswers.styleguide, earlyAnswers.installESLint);
const config = getConfigForStyleGuide(earlyAnswers.styleguide, earlyAnswers.installESLint);

writeFile(config, earlyAnswers.format);

return void 0;
Expand Down Expand Up @@ -529,7 +529,8 @@ function promptUser() {
if (earlyAnswers.source === "auto") {
const combinedAnswers = Object.assign({}, earlyAnswers, secondAnswers);

config = processAnswers(combinedAnswers);
const config = processAnswers(combinedAnswers);

installModules(config);
writeFile(config, earlyAnswers.format);

Expand Down Expand Up @@ -575,7 +576,8 @@ function promptUser() {
]).then(answers => {
const totalAnswers = Object.assign({}, earlyAnswers, secondAnswers, answers);

config = processAnswers(totalAnswers);
const config = processAnswers(totalAnswers);

installModules(config);
writeFile(config, answers.format);
});
Expand Down
6 changes: 2 additions & 4 deletions lib/linter.js
Expand Up @@ -988,9 +988,7 @@ module.exports = class Linter {
markVariableAsUsed(name) {
const hasGlobalReturn = this.currentConfig.parserOptions.ecmaFeatures && this.currentConfig.parserOptions.ecmaFeatures.globalReturn,
specialScope = hasGlobalReturn || this.currentConfig.parserOptions.sourceType === "module";
let scope = getScope(this.scopeManager, this.traverser.current(), this.currentConfig.parserOptions.ecmaVersion),
i,
len;
let scope = getScope(this.scopeManager, this.traverser.current(), this.currentConfig.parserOptions.ecmaVersion);

// Special Node.js scope means we need to start one level deeper
if (scope.type === "global" && specialScope) {
Expand All @@ -1000,7 +998,7 @@ module.exports = class Linter {
do {
const variables = scope.variables;

for (i = 0, len = variables.length; i < len; i++) {
for (let i = 0; i < variables.length; i++) {
if (variables[i].name === name) {
variables[i].eslintUsed = true;
return true;
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/key-spacing.js
Expand Up @@ -420,14 +420,15 @@ module.exports = {
isExtra = diff > 0,
diffAbs = Math.abs(diff),
spaces = Array(diffAbs + 1).join(" ");
let fix;

if ((
diff && mode === "strict" ||
diff < 0 && mode === "minimum" ||
diff > 0 && !expected && mode === "minimum") &&
!(expected && containsLineTerminator(whitespace))
) {
let fix;

if (isExtra) {
let range;

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-unmodified-loop-condition.js
Expand Up @@ -213,13 +213,13 @@ function getEncloseFunctionDeclaration(reference) {
* @returns {void}
*/
function updateModifiedFlag(conditions, modifiers) {
let funcNode, funcVar;

for (let i = 0; i < conditions.length; ++i) {
const condition = conditions[i];

for (let j = 0; !condition.modified && j < modifiers.length; ++j) {
const modifier = modifiers[j];
let funcNode, funcVar;

/*
* Besides checking for the condition being in the loop, we want to
Expand Down
6 changes: 4 additions & 2 deletions lib/rules/quote-props.js
Expand Up @@ -135,13 +135,14 @@ module.exports = {
*/
function checkUnnecessaryQuotes(node) {
const key = node.key;
let tokens;

if (node.method || node.computed || node.shorthand) {
return;
}

if (key.type === "Literal" && typeof key.value === "string") {
let tokens;

try {
tokens = espree.tokenize(key.value);
} catch (e) {
Expand Down Expand Up @@ -215,7 +216,6 @@ module.exports = {

node.properties.forEach(property => {
const key = property.key;
let tokens;

if (!key || property.method || property.computed || property.shorthand) {
return;
Expand All @@ -226,6 +226,8 @@ module.exports = {
quotedProps.push(property);

if (checkQuotesRedundancy) {
let tokens;

try {
tokens = espree.tokenize(key.value);
} catch (e) {
Expand Down
3 changes: 1 addition & 2 deletions lib/rules/quotes.js
Expand Up @@ -229,10 +229,9 @@ module.exports = {
Literal(node) {
const val = node.value,
rawVal = node.raw;
let isValid;

if (settings && typeof val === "string") {
isValid = (quoteOption === "backtick" && isAllowedAsNonBacktick(node)) ||
let isValid = (quoteOption === "backtick" && isAllowedAsNonBacktick(node)) ||
isJSXLiteral(node) ||
astUtils.isSurroundedBy(rawVal, settings.quote);

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/space-before-blocks.js
Expand Up @@ -82,11 +82,11 @@ module.exports = {
*/
function checkPrecedingSpace(node) {
const precedingToken = sourceCode.getTokenBefore(node);
let requireSpace;

if (precedingToken && !isConflicted(precedingToken) && astUtils.isTokenOnSameLine(precedingToken, node)) {
const hasSpace = sourceCode.isSpaceBetweenTokens(precedingToken, node);
const parent = context.getAncestors().pop();
let requireSpace;

if (parent.type === "FunctionExpression" || parent.type === "FunctionDeclaration") {
requireSpace = checkFunctions;
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/valid-jsdoc.js
Expand Up @@ -231,11 +231,11 @@ module.exports = {
hasConstructor = false,
isInterface = false,
isOverride = false,
isAbstract = false,
jsdoc;
isAbstract = false;

// make sure only to validate JSDoc comments
if (jsdocNode) {
let jsdoc;

try {
jsdoc = doctrine.parse(jsdocNode.value, {
Expand Down

0 comments on commit f31f59d

Please sign in to comment.