Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: camelcase rule ignoreList added #10783

Merged
merged 2 commits into from Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -544,6 +556,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"
}
]
}
]
});