Skip to content

Commit

Permalink
adding to docs, tests, and fixing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
erindepew committed Oct 23, 2017
1 parent ae652d5 commit a30e043
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
30 changes: 30 additions & 0 deletions docs/rules/camelcase.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,27 @@ obj.do_something = function() {
// ...
};

function foo({ no_camelcased }) {
// ...
};

function foo({ no_camelcased: my_default }) {
// ...
}

function foo({ no_camelcased = 'default value' }) {
// ...
};

var obj = {
my_pref: 1
};

var { category_id = 1 } = query;

var { foo: no_camelcased } = bar;

var { foo: bar_baz = 1 } = quz;
```

Examples of **correct** code for this rule with the default `{ "properties": "always" }` option:
Expand Down Expand Up @@ -68,6 +86,18 @@ Examples of **correct** code for this rule with the `{ "properties": "never" }`
var obj = {
my_pref: 1
};

const { no_camelcased } = bar;

const { no_camelcased = false } = bar;

function foo({ no_camelcased }) {
// ...
};

function foo({ no_camelcased = 'default value' }) {
// ...
}
```

## When Not To Use It
Expand Down
20 changes: 10 additions & 10 deletions lib/rules/camelcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,22 @@ module.exports = {
// e.g.: const { no_camelcased = false } = bar;
} else if (node.parent.type === "Property" || node.parent.type === "AssignmentPattern") {

if (node.parent.parent.type === "ObjectPattern") {
node.parent.parent.properties.forEach(property => {
if (property.value.right && property.value.right.type === "Identifier" && isUnderscored(property.value.right.name)) {
report(node);
}
});
}

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

if (node.parent.parent && node.parent.parent.type === "ObjectPattern" &&
node.parent.key === node && node.parent.value !== node) {
if (node.parent.parent && node.parent.parent.type === "ObjectPattern") {
if (node.parent.key === node && node.parent.value !== node) {
return;
}
if (node.parent.parent.type === "ObjectPattern") {
node.parent.parent.properties.forEach(property => {
if (property.value.right && property.value.right.type === "Identifier" && isUnderscored(property.value.right.name)) {
report(node);
}
});
}
}

if (isUnderscored(name) && !ALLOWED_PARENT_TYPES.has(effectiveParent.type)) {
Expand Down
20 changes: 17 additions & 3 deletions tests/lib/rules/camelcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@ ruleTester.run("camelcase", rule, {
},
{
code: " const no_camelcased = 0; function foo({ camelcased_value = no_camelcased}) {}",
options: [{ properties: "never" }],
parserOptions: { ecmaVersion: 6 },
errors: [
{
Expand All @@ -368,7 +367,11 @@ ruleTester.run("camelcase", rule, {
{
message: "Identifier 'camelcased_value' is not in camel case.",
type: "Identifier"
}
},
{
message: "Identifier 'no_camelcased' is not in camel case.",
type: "Identifier"
},
]
},
{
Expand All @@ -392,6 +395,17 @@ ruleTester.run("camelcase", rule, {
type: "Identifier"
}
]
}
},
{
code: "var { foo: bar_baz = 1 } = quz;",
options: [{ properties: "always" }],
parserOptions: { ecmaVersion: 6 },
errors: [
{
message: "Identifier 'bar_baz' is not in camel case.",
type: "Identifier"
}
]
}
]
});

0 comments on commit a30e043

Please sign in to comment.