Skip to content

Commit

Permalink
Merge pull request #2046 from doberkofler/update-camelcase
Browse files Browse the repository at this point in the history
Update: rule camelcase to allow snake_case in object literals. (fixes #1919)
  • Loading branch information
nzakas committed Mar 18, 2015
2 parents 9a5d0c4 + 3719b40 commit 66b43cb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
26 changes: 26 additions & 0 deletions docs/rules/camelcase.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ When it comes to naming variables, styleguides generally fall into one of two ca

This rule looks for any underscores (`_`) located within the source code. It ignores leading and trailing underscores and only checks those in the middle of a variable name. If ESLint decides that the variable is a constant (all uppercase), then no warning will be thrown. Otherwise, a warning will be thrown. This rule only flags definitions and assignments but not function calls.

### Options

This rule accepts a single options argument with the following defaults:

```js
{
"rules": {
"camelcase": [2, {"properties": "always"}]
}
}
```

`Properties` can have the following values:

1. `always` is the default and checks all proprty names
2. `never` does not check property names at all

The following patterns are considered warnings:

```js
Expand All @@ -18,6 +35,10 @@ function do_something() {
obj.do_something = function() {
// ...
};

var obj = {
my_pref: 1
};
```

The following patterns are considered okay and do not cause warnings:
Expand All @@ -31,6 +52,11 @@ var foo = bar.baz_boom;
var foo = { qux: bar.baz_boom };

obj.do_something();

/*eslint camelcase: [2, {properties: "never"}]*/
var obj = {
my_pref: 1
};
```

## When Not To Use It
Expand Down
21 changes: 21 additions & 0 deletions lib/rules/camelcase.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @fileoverview Rule to flag non-camelcased identifiers
* @author Nicholas C. Zakas
* @copyright 2015 Dieter Oberkofler. All rights reserved.
*/

"use strict";
Expand Down Expand Up @@ -37,6 +38,13 @@ module.exports = function(context) {
context.report(node, "Identifier '{{name}}' is not in camel case.", { name: node.name });
}

var options = context.options[0] || {},
properties = options.properties || "";

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

return {

"Identifier": function(node) {
Expand All @@ -63,11 +71,24 @@ module.exports = function(context) {
report(node);
}

// Properties have their own rules
} else if (node.parent.type === "Property") {

// "never" check properties
if (properties === "never") {
return;
}

if (isUnderscored(name) && effectiveParent.type !== "CallExpression") {
report(node);
}

// Report anything that is underscored that isn't a CallExpression
} else if (isUnderscored(name) && effectiveParent.type !== "CallExpression") {
report(node);
}
}

};

};
21 changes: 20 additions & 1 deletion tests/lib/rules/camelcase.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @fileoverview Tests for camelcase rule.
* @author Nicholas C. Zakas
* @copyright 2015 Dieter Oberkofler. All rights reserved.
*/

"use strict";
Expand Down Expand Up @@ -35,7 +36,15 @@ eslintTester.addRuleTest("lib/rules/camelcase", {
"[foo.bar_baz]",
"var arr = [foo.bar_baz.qux];",
"[foo.bar_baz.nesting]",
"if (foo.bar_baz === boom.bam_pow) { [foo.baz_boom] }"
"if (foo.bar_baz === boom.bam_pow) { [foo.baz_boom] }",
{
code: "var o = {key: 1}",
args: [2, {properties: "always"}]
},
{
code: "var o = {bar_baz: 1}",
args: [2, {properties: "never"}]
}
],
invalid: [
{
Expand Down Expand Up @@ -127,6 +136,16 @@ eslintTester.addRuleTest("lib/rules/camelcase", {
type: "Identifier"
}
]
},
{
code: "var o = {bar_baz: 1}",
args: [2, {properties: "always"}],
errors: [
{
message: "Identifier 'bar_baz' is not in camel case.",
type: "Identifier"
}
]
}

]
Expand Down

0 comments on commit 66b43cb

Please sign in to comment.