Skip to content

Commit

Permalink
Update: camelcase rule ignoreList added (#10783)
Browse files Browse the repository at this point in the history
* New: camelcase rule now accept ignore list

* Docs: Documentation updated for camelcase rule ignore list
  • Loading branch information
Shudrum authored and ilyavolodin committed Oct 2, 2018
1 parent 70bde69 commit 066f7e0
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 8 deletions.
25 changes: 25 additions & 0 deletions docs/rules/camelcase.md
Expand Up @@ -14,6 +14,7 @@ This rule has an object option:
* `"properties": "never"` does not check property names
* `"ignoreDestructuring": false` (default) enforces camelcase style for destructured identifiers
* `"ignoreDestructuring": true` does not check destructured identifiers
* `allow` (`string[]`) list of properties to accept. Accept regex.

### properties: "always"

Expand Down Expand Up @@ -151,6 +152,30 @@ var { category_id = 1 } = query;
var { category_id: category_id } = query;
```

## allow

Examples of **correct** code for this rule with the `allow` option:

```js
/*eslint camelcase: ["error", {allow: ["UNSAFE_componentWillMount"]}]*/

function UNSAFE_componentWillMount() {
// ...
}
```

```js
/*eslint camelcase: ["error", {allow: ["^UNSAFE_"]}]*/

function UNSAFE_componentWillMount() {
// ...
}

function UNSAFE_componentWillMount() {
// ...
}
```

## When Not To Use It

If you have established coding standards using a different naming convention (separating words with underscores), turn this rule off.
44 changes: 36 additions & 8 deletions lib/rules/camelcase.js
Expand Up @@ -27,6 +27,16 @@ module.exports = {
},
properties: {
enum: ["always", "never"]
},
allow: {
type: "array",
items: [
{
type: "string"
}
],
minItems: 0,
uniqueItems: true
}
},
additionalProperties: false
Expand All @@ -40,6 +50,15 @@ module.exports = {

create(context) {

const options = context.options[0] || {};
let properties = options.properties || "";
const ignoreDestructuring = options.ignoreDestructuring || false;
const allow = options.allow || [];

if (properties !== "always" && properties !== "never") {
properties = "always";
}

//--------------------------------------------------------------------------
// Helpers
//--------------------------------------------------------------------------
Expand All @@ -60,6 +79,18 @@ module.exports = {
return name.indexOf("_") > -1 && name !== name.toUpperCase();
}

/**
* Checks if a string match the ignore list
* @param {string} name The string to check.
* @returns {boolean} if the string is ignored
* @private
*/
function isAllowed(name) {
return allow.findIndex(
entry => name === entry || name.match(new RegExp(entry))
) !== -1;
}

/**
* Checks if a parent of a node is an ObjectPattern.
* @param {ASTNode} node The node to check.
Expand Down Expand Up @@ -93,14 +124,6 @@ module.exports = {
}
}

const options = context.options[0] || {};
let properties = options.properties || "";
const ignoreDestructuring = options.ignoreDestructuring || false;

if (properties !== "always" && properties !== "never") {
properties = "always";
}

return {

Identifier(node) {
Expand All @@ -112,6 +135,11 @@ module.exports = {
const name = node.name.replace(/^_+|_+$/g, ""),
effectiveParent = (node.parent.type === "MemberExpression") ? node.parent.parent : node.parent;

// First, we ignore the node if it match the ignore list
if (isAllowed(name)) {
return;
}

// MemberExpressions get special rules
if (node.parent.type === "MemberExpression") {

Expand Down
32 changes: 32 additions & 0 deletions tests/lib/rules/camelcase.js
Expand Up @@ -181,6 +181,18 @@ ruleTester.run("camelcase", rule, {
{
code: "function foo({ trailing_ }) {}",
parserOptions: { ecmaVersion: 6 }
},
{
code: "ignored_foo = 0;",
options: [{ allow: ["ignored_foo"] }]
},
{
code: "ignored_foo = 0; ignored_bar = 1;",
options: [{ allow: ["ignored_foo", "ignored_bar"] }]
},
{
code: "user_id = 0;",
options: [{ allow: ["_id$"] }]
}
],
invalid: [
Expand Down Expand Up @@ -555,6 +567,26 @@ ruleTester.run("camelcase", rule, {
type: "Identifier"
}
]
},
{
code: "not_ignored_foo = 0;",
options: [{ allow: ["ignored_bar"] }],
errors: [
{
message: "Identifier 'not_ignored_foo' is not in camel case.",
type: "Identifier"
}
]
},
{
code: "not_ignored_foo = 0;",
options: [{ allow: ["_id$"] }],
errors: [
{
message: "Identifier 'not_ignored_foo' is not in camel case.",
type: "Identifier"
}
]
}
]
});

0 comments on commit 066f7e0

Please sign in to comment.