Skip to content

Commit

Permalink
Add option "ignoreGlobals" to camelcase rule
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdado committed Jan 13, 2020
1 parent a1d999c commit b08d262
Show file tree
Hide file tree
Showing 2 changed files with 41 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
17 changes: 17 additions & 0 deletions lib/rules/camelcase.js
Expand Up @@ -32,6 +32,10 @@ module.exports = {
type: "boolean",
default: false
},
ignoreGlobals: {
type: "boolean",
default: false
},
properties: {
enum: ["always", "never"]
},
Expand Down Expand Up @@ -61,6 +65,7 @@ module.exports = {
let properties = options.properties || "";
const ignoreDestructuring = options.ignoreDestructuring;
const ignoreImports = options.ignoreImports;
const ignoreGlobals = options.ignoreGlobals;
const allow = options.allow || [];

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

// Check if it's a global variable
} else if (Boolean(node.scope) && node.scope.type === "global") {

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

0 comments on commit b08d262

Please sign in to comment.