Skip to content

Commit

Permalink
Update: Add option "ignoreGlobals" to camelcase rule (fixes 11716)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdado committed Jan 14, 2020
1 parent a1d999c commit feeebee
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docs/rules/camelcase.md
Expand Up @@ -16,6 +16,8 @@ This rule has an object option:
* `"ignoreDestructuring": true` does not check destructured identifiers (but still checks any use of those identifiers later in the code)
* `"ignoreImports": false` (default) enforces camelcase style for ES2015 imports
* `"ignoreImports": true` does not check ES2015 imports (but still checks any use of the imports later in the code except function arguments)
* `"ignoreGlobals": false` (default) enforces camelcase style for global variables
* `"ignoreGlobals": true` does not enforce camelcase style for global variables
* `allow` (`string[]`) list of properties to accept. Accept regex.

### properties: "always"
Expand Down Expand Up @@ -217,6 +219,28 @@ Examples of **correct** code for this rule with the `{ "ignoreImports": true }`
import { snake_cased } from 'mod';
```

### ignoreGlobals: false

Examples of **incorrect** code for this rule with the default `{ "ignoreGlobals": false }` option:

```js
/*eslint camelcase: ["error", {ignoreGlobals: false}]*/
/* global some_property */

const property = some_property;
```

### ignoreGlobals: true

Examples of **correct** code for this rule with the `{ "ignoreGlobals": true }` option:

```js
/*eslint camelcase: ["error", {ignoreGlobals: true}]*/
/* global some_property */

const property = some_property;
```

## allow

Examples of **correct** code for this rule with the `allow` option:
Expand Down
40 changes: 40 additions & 0 deletions lib/rules/camelcase.js
Expand Up @@ -5,6 +5,28 @@

"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 @@ -32,6 +54,10 @@ module.exports = {
type: "boolean",
default: false
},
ignoreGlobals: {
type: "boolean",
default: false
},
properties: {
enum: ["always", "never"]
},
Expand Down Expand Up @@ -61,7 +87,9 @@ module.exports = {
let properties = options.properties || "";
const ignoreDestructuring = options.ignoreDestructuring;
const ignoreImports = options.ignoreImports;
const ignoreGlobals = options.ignoreGlobals;
const allow = options.allow || [];
const currentScope = context.getScope();

if (properties !== "always" && properties !== "never") {
properties = "always";
Expand Down Expand Up @@ -229,6 +257,18 @@ module.exports = {
report(node);
}

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

if (ignoreGlobals) {
return;
}

// Report only if the local imported identifier is underscored
if (nameIsUnderscored) {
report(node);
}

// Report anything that is underscored that isn't a CallExpression
} else if (nameIsUnderscored && !ALLOWED_PARENT_TYPES.has(effectiveParent.type)) {
report(node);
Expand Down
22 changes: 22 additions & 0 deletions tests/lib/rules/camelcase.js
Expand Up @@ -161,6 +161,16 @@ ruleTester.run("camelcase", rule, {
options: [{ ignoreImports: false }],
parserOptions: { ecmaVersion: 6, sourceType: "module" }
},
{
code: "var _camelCased = camelCased",
options: [{ ignoreGlobals: false }],
globals: { camelCased: false }
},
{
code: "var camelCased = snake_cased",
options: [{ ignoreGlobals: true }],
globals: { snake_cased: false } // eslint-disable-line camelcase
},
{
code: "function foo({ no_camelcased: camelCased }) {};",
parserOptions: { ecmaVersion: 6 }
Expand Down Expand Up @@ -590,6 +600,18 @@ ruleTester.run("camelcase", rule, {
}
]
},
{
code: "var camelCased = snake_cased",
options: [{ ignoreGlobals: false }],
globals: { snake_cased: false }, // eslint-disable-line camelcase
errors: [
{
messageId: "notCamelCase",
data: { name: "snake_cased" },
type: "Identifier"
}
]
},
{
code: "function foo({ no_camelcased }) {};",
parserOptions: { ecmaVersion: 6 },
Expand Down

0 comments on commit feeebee

Please sign in to comment.