]> git.proxmox.com Git - pve-eslint.git/blobdiff - eslint/lib/rules/camelcase.js
import 7.12.1 upstream release
[pve-eslint.git] / eslint / lib / rules / camelcase.js
index 04360837294a127b2ba6f2b2031b7dd2f0af3c3b..d34656cfabe731b7516475be66531f3fa904c954 100644 (file)
@@ -32,6 +32,10 @@ module.exports = {
                         type: "boolean",
                         default: false
                     },
+                    ignoreGlobals: {
+                        type: "boolean",
+                        default: false
+                    },
                     properties: {
                         enum: ["always", "never"]
                     },
@@ -61,8 +65,11 @@ module.exports = {
         let properties = options.properties || "";
         const ignoreDestructuring = options.ignoreDestructuring;
         const ignoreImports = options.ignoreImports;
+        const ignoreGlobals = options.ignoreGlobals;
         const allow = options.allow || [];
 
+        let globalScope;
+
         if (properties !== "always" && properties !== "never") {
             properties = "always";
         }
@@ -159,6 +166,37 @@ 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);
+        }
+
+        /**
+         * Checks whether the given node represents a reference to a property of an object in an object literal expression.
+         * This allows to differentiate between a global variable that is allowed to be used as a reference, and the key
+         * of the expressed object (which shouldn't be allowed).
+         * @param {ASTNode} node `Identifier` node to check.
+         * @returns {boolean} `true` if the node is a property name of an object literal expression
+         */
+        function isPropertyNameInObjectLiteral(node) {
+            const parent = node.parent;
+
+            return (
+                parent.type === "Property" &&
+                parent.parent.type === "ObjectExpression" &&
+                !parent.computed &&
+                parent.key === node
+            );
+        }
+
         /**
          * Reports an AST node as a rule violation.
          * @param {ASTNode} node The node to report.
@@ -174,6 +212,10 @@ module.exports = {
 
         return {
 
+            Program() {
+                globalScope = context.getScope();
+            },
+
             Identifier(node) {
 
                 /*
@@ -189,6 +231,11 @@ module.exports = {
                     return;
                 }
 
+                // Check if it's a global variable
+                if (ignoreGlobals && isReferenceToGlobalVariable(node) && !isPropertyNameInObjectLiteral(node)) {
+                    return;
+                }
+
                 // MemberExpressions get special rules
                 if (node.parent.type === "MemberExpression") {