Skip to content

Commit

Permalink
Fix behavior for global references and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdado committed Jun 21, 2020
1 parent 8984e18 commit 4e61c59
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
10 changes: 5 additions & 5 deletions lib/rules/camelcase.js
Expand Up @@ -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") {

Expand Down Expand Up @@ -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);
Expand Down
48 changes: 44 additions & 4 deletions tests/lib/rules/camelcase.js
Expand Up @@ -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 }) {};",
Expand Down Expand Up @@ -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" },
Expand Down

0 comments on commit 4e61c59

Please sign in to comment.