Skip to content
Permalink
Browse files

Update: Ignore camelcase in object destructuring (fixes #3185)

  • Loading branch information...
alberto committed Jan 7, 2016
1 parent 0a01bcc commit 186e8f0a555701f02ba0207e8e71b3d941de665a
Showing with 43 additions and 4 deletions.
  1. +2 −0 docs/rules/camelcase.md
  2. +12 −3 lib/rules/camelcase.js
  3. +29 −1 tests/lib/rules/camelcase.js
@@ -54,6 +54,8 @@ var foo = bar.baz_boom;
var foo = { qux: bar.baz_boom };
obj.do_something();
var { category_id: category } = query;
```


@@ -16,6 +16,9 @@ module.exports = function(context) {
// Helpers
//--------------------------------------------------------------------------

// contains reported nodes to avoid reporting twice on destructuring with shorthand notation
var reported = [];

/**
* Checks if a string contains an underscore and isn't all upper-case
* @param {String} name The string to check.
@@ -35,7 +38,10 @@ module.exports = function(context) {
* @private
*/
function report(node) {
context.report(node, "Identifier '{{name}}' is not in camel case.", { name: node.name });
if (reported.indexOf(node) < 0) {
reported.push(node);
context.report(node, "Identifier '{{name}}' is not in camel case.", { name: node.name });
}
}

var options = context.options[0] || {},
@@ -48,7 +54,6 @@ module.exports = function(context) {
return {

"Identifier": function(node) {

// Leading and trailing underscores are commonly used to flag private/protected identifiers, strip them
var name = node.name.replace(/^_+|_+$/g, ""),
effectiveParent = (node.parent.type === "MemberExpression") ? node.parent.parent : node.parent;
@@ -78,12 +83,16 @@ module.exports = function(context) {

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

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

if (node.parent.parent && node.parent.parent.type === "ObjectPattern" &&
node.parent.key === node && node.parent.value !== node) {
return;
}

if (isUnderscored(name) && effectiveParent.type !== "CallExpression") {
report(node);
}
@@ -56,6 +56,15 @@ ruleTester.run("camelcase", rule, {
{
code: "obj.foo_bar = function(){};",
options: [{properties: "never"}]
},
{
code: "var { category_id: category } = query;",
parserOptions: { ecmaVersion: 6 }
},
{
code: "var { category_id: category } = query;",
parserOptions: { ecmaVersion: 6 },
options: [{properties: "never"}]
}
],
invalid: [
@@ -178,7 +187,26 @@ ruleTester.run("camelcase", rule, {
type: "Identifier"
}
]
},
{
code: "var { category_id: category_id } = query;",
parserOptions: { ecmaVersion: 6 },
errors: [
{
message: "Identifier 'category_id' is not in camel case.",
type: "Identifier"
}
]
},
{
code: "var { category_id } = query;",
parserOptions: { ecmaVersion: 6 },
errors: [
{
message: "Identifier 'category_id' is not in camel case.",
type: "Identifier"
}
]
}

]
});

0 comments on commit 186e8f0

Please sign in to comment.
You can’t perform that action at this time.