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: add allow underscores options to camelcase rule. #10474

Closed
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
40 changes: 40 additions & 0 deletions docs/rules/camelcase.md
Expand Up @@ -14,6 +14,10 @@ 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
* `"allowLeadingUnderscores": true` (default) allow leading underscores, which is commonly used to flag private/protected identifiers
* `"allowLeadingUnderscores": false` does not allow leading underscores
* `"allowTrailingUnderscores": true` (default) allow trailing underscores, which is commonly used to flag private/protected identifiers
* `"allowTrailingUnderscores": false` does not allow trailing underscores

### properties: "always"

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

### allowLeadingUnderscores: false

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

```js
/*eslint camelcase: ["error", {allowLeadingUnderscores: false}]*/

var __leadingUnderscore = query;
```

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

```js
/*eslint camelcase: ["error", {allowDestructuring: false}]*/

var noLeadingUnderscore = query;
```

### allowTrailingUnderscores: false

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

```js
/*eslint camelcase: ["error", {allowTrailingUnderscores: false}]*/

var trailingUnderscore__ = query;
```

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

```js
/*eslint camelcase: ["error", {allowDestructuring: false}]*/

var noTrailingUnderscore = query;
```

## When Not To Use It

If you have established coding standards using a different naming convention (separating words with underscores), turn this rule off.
24 changes: 18 additions & 6 deletions lib/rules/camelcase.js
Expand Up @@ -25,6 +25,12 @@ module.exports = {
ignoreDestructuring: {
type: "boolean"
},
allowLeadingUnderscores: {
type: "boolean"
},
allowTrailingUnderscores: {
type: "boolean"
},
properties: {
enum: ["always", "never"]
}
Expand Down Expand Up @@ -96,6 +102,8 @@ module.exports = {
const options = context.options[0] || {};
let properties = options.properties || "";
const ignoreDestructuring = options.ignoreDestructuring || false;
const allowLeadingUnderscores = options.allowLeadingUnderscores !== false;
const allowTrailingUnderscores = options.allowTrailingUnderscores !== false;

if (properties !== "always" && properties !== "never") {
properties = "always";
Expand All @@ -105,12 +113,16 @@ module.exports = {

Identifier(node) {

/*
* Leading and trailing underscores are commonly used to flag
* private/protected identifiers, strip them
*/
const name = node.name.replace(/^_+|_+$/g, ""),
effectiveParent = (node.parent.type === "MemberExpression") ? node.parent.parent : node.parent;
let name = node.name;
const effectiveParent = (node.parent.type === "MemberExpression") ? node.parent.parent : node.parent;

// Strip leading and trailing underscores
if (allowLeadingUnderscores) {
name = name.replace(/^_+/g, "");
}
if (allowTrailingUnderscores) {
name = name.replace(/_+$/g, "");
}

// MemberExpressions get special rules
if (node.parent.type === "MemberExpression") {
Expand Down
22 changes: 22 additions & 0 deletions tests/lib/rules/camelcase.js
Expand Up @@ -184,6 +184,28 @@ ruleTester.run("camelcase", rule, {
}
],
invalid: [
{
code: "__leadingUnderscores = null",
options: [{ allowLeadingUnderscores: false }],
errors: [
{
messageId: "notCamelCase",
data: { name: "__leadingUnderscores" },
type: "Identifier"
}
]
},
{
code: "trailingUnderscores__ = null",
options: [{ allowTrailingUnderscores: false }],
errors: [
{
messageId: "notCamelCase",
data: { name: "trailingUnderscores__" },
type: "Identifier"
}
]
},
{
code: "first_name = \"Nicholas\"",
errors: [
Expand Down