Skip to content

Commit

Permalink
Change reference check to look for global variable
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdado committed Jun 16, 2020
1 parent 9b685d4 commit 3381d1a
Showing 1 changed file with 21 additions and 32 deletions.
53 changes: 21 additions & 32 deletions lib/rules/camelcase.js
Expand Up @@ -5,28 +5,6 @@

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const { findVariable } = require("eslint-utils");

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

/**
* Determines whether the given identifier node is a reference to a global variable.
* @param {ASTNode} node `Identifier` node to check.
* @param {Scope} scope Scope to which the node belongs.
* @returns {boolean} True if the identifier is a reference to a global variable.
*/
function isGlobalReference(node, scope) {
const variable = findVariable(scope, node);

return variable !== null && variable.scope.type === "global" && variable.defs.length === 0;
}

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -89,7 +67,8 @@ module.exports = {
const ignoreImports = options.ignoreImports;
const ignoreGlobals = options.ignoreGlobals;
const allow = options.allow || [];
const currentScope = context.getScope();

let globalScope;

if (properties !== "always" && properties !== "never") {
properties = "always";
Expand Down Expand Up @@ -187,6 +166,19 @@ module.exports = {
return false;
}

/**
* Checks whether the given node represents a reference to a global variable that is not declared in the source code.
* These identifiers will be allowed, as it is assumed that user has no control over the names of external global variables.
* @param {ASTNode} node `Identifier` node to check.
* @returns {boolean} `true` if the node is a reference to a global variable.
*/
function isReferenceToGlobalVariable(node) {
const variable = globalScope.set.get(node.name);

return variable && variable.defs.length === 0 &&
variable.references.some(ref => ref.identifier === node);
}

/**
* Reports an AST node as a rule violation.
* @param {ASTNode} node The node to report.
Expand All @@ -202,6 +194,10 @@ module.exports = {

return {

Program() {
globalScope = context.getScope();
},

Identifier(node) {

/*
Expand Down Expand Up @@ -295,16 +291,9 @@ module.exports = {
}

// Check if it's a global variable
} else if (isGlobalReference(node, currentScope)) {
} else if (ignoreGlobals && isReferenceToGlobalVariable(node)) {

if (ignoreGlobals) {
return;
}

// Report only if the local imported identifier is underscored
if (nameIsUnderscored) {
report(node);
}
// no-op (no-useless-return)

// Report anything that is underscored that isn't a CallExpression
} else if (nameIsUnderscored && !ALLOWED_PARENT_TYPES.has(effectiveParent.type)) {
Expand Down

0 comments on commit 3381d1a

Please sign in to comment.