diff --git a/lib/rules/camelcase.js b/lib/rules/camelcase.js index 2b1d0043f1f..c3966917571 100644 --- a/lib/rules/camelcase.js +++ b/lib/rules/camelcase.js @@ -213,6 +213,11 @@ module.exports = { return; } + // Check if it's a global variable + if (ignoreGlobals && isReferenceToGlobalVariable(node)) { + return; + } + // MemberExpressions get special rules if (node.parent.type === "MemberExpression") { @@ -290,11 +295,6 @@ module.exports = { report(node); } - // Check if it's a global variable - } else if (ignoreGlobals && isReferenceToGlobalVariable(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)) { report(node); diff --git a/tests/lib/rules/camelcase.js b/tests/lib/rules/camelcase.js index b67f787d44f..4ce424dfd28 100644 --- a/tests/lib/rules/camelcase.js +++ b/tests/lib/rules/camelcase.js @@ -170,14 +170,29 @@ ruleTester.run("camelcase", rule, { parserOptions: { ecmaVersion: 6, sourceType: "module" } }, { - code: "var _camelCased = camelCased", + code: "var _camelCased = aGlobalVariable", options: [{ ignoreGlobals: false }], - globals: { camelCased: false } + globals: { aGlobalVariable: false } }, { - code: "var camelCased = snake_cased", + code: "var camelCased = _aGlobalVariable", + options: [{ ignoreGlobals: false }], + globals: { _aGlobalVariable: false } + }, + { + code: "var camelCased = a_global_variable", options: [{ ignoreGlobals: true }], - globals: { snake_cased: false } // eslint-disable-line camelcase + globals: { a_global_variable: false } // eslint-disable-line camelcase + }, + { + code: "a_global_variable.foo()", + options: [{ ignoreGlobals: true }], + globals: { a_global_variable: false } // eslint-disable-line camelcase + }, + { + code: "a_global_variable[undefined]", + options: [{ ignoreGlobals: true }], + globals: { a_global_variable: "readonly" } // eslint-disable-line camelcase }, { code: "function foo({ no_camelcased: camelCased }) {};", @@ -674,6 +689,31 @@ ruleTester.run("camelcase", rule, { } ] }, + { + code: "a_global_variable.foo()", + options: [{ ignoreGlobals: false }], + globals: { a_global_variable: false }, // eslint-disable-line camelcase + errors: [ + { + messageId: "notCamelCase", + data: { name: "a_global_variable" }, + type: "Identifier" + } + ] + }, + { + code: "a_global_variable[undefined]", + options: [{ ignoreGlobals: false }], + globals: { a_global_variable: false }, // eslint-disable-line camelcase + errors: [ + { + messageId: "notCamelCase", + data: { name: "a_global_variable" }, + type: "Identifier" + } + ] + }, + { code: "export * as snake_cased from 'mod'", parserOptions: { ecmaVersion: 2020, sourceType: "module" },