Skip to content

Commit

Permalink
Fix: handle destructuring with defaults in camelcase rule (fixes #8511)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorbal authored and erindepew committed Oct 17, 2017
1 parent da77eb4 commit c905181
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/rules/camelcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,17 @@ module.exports = {
report(node);
}

// Properties have their own rules
} else if (node.parent.type === "Property") {
// Properties have their own rules, and
// AssignmentPattern nodes can be treated like Properties:
// 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") {
Expand Down
91 changes: 91 additions & 0 deletions tests/lib/rules/camelcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,30 @@ ruleTester.run("camelcase", rule, {
{
code: "import { no_camelcased as camelCased, anoterCamelCased } from \"external-module\";",
parserOptions: { ecmaVersion: 6, sourceType: "module" }
},
{
code: "const { no_camelcased } = bar;",
parserOptions: { ecmaVersion: 6 },
options: [{ properties: "never" }]
},
{
code: "const { no_camelcased = false } = bar;",
parserOptions: { ecmaVersion: 6 },
options: [{ properties: "never" }]
},
{
code: "function foo({ no_camelcased }) {};",
parserOptions: { ecmaVersion: 6 },
options: [{ properties: "never" }]
},
{
code: "function foo({ no_camelcased = 'default value' }) {};",
parserOptions: { ecmaVersion: 6 },
options: [{ properties: "never" }]
},
{
code: "function foo({ no_camelcased: camelCased }) {};",
parserOptions: { ecmaVersion: 6 }
}
],
invalid: [
Expand Down Expand Up @@ -212,6 +236,16 @@ ruleTester.run("camelcase", rule, {
}
]
},
{
code: "var { category_id = 1 } = query;",
parserOptions: { ecmaVersion: 6 },
errors: [
{
message: "Identifier 'category_id' is not in camel case.",
type: "Identifier"
}
]
},
{
code: "import no_camelcased from \"external-module\";",
parserOptions: { ecmaVersion: 6, sourceType: "module" },
Expand Down Expand Up @@ -301,6 +335,63 @@ ruleTester.run("camelcase", rule, {
type: "Identifier"
}
]
},
{
code: "function foo({ no_camelcased }) {};",
parserOptions: { ecmaVersion: 6 },
errors: [
{
message: "Identifier 'no_camelcased' is not in camel case.",
type: "Identifier"
}
]
},
{
code: "function foo({ no_camelcased = 'default value' }) {};",
parserOptions: { ecmaVersion: 6 },
errors: [
{
message: "Identifier 'no_camelcased' is not in camel case.",
type: "Identifier"
}
]
},
{
code: " const no_camelcased = 0; function foo({ camelcased_value = no_camelcased}) {}",
parserOptions: { ecmaVersion: 6 },
options: [{ properties: "never" }],
errors: [
{
message: "Identifier 'no_camelcased' is not in camel case.",
type: "Identifier"
},
{
message: "Identifier 'camelcased_value' is not in camel case.",
type: "Identifier"
}
]
},
{
code: "const { bar: no_camelcased } = foo;",
parserOptions: { ecmaVersion: 6 },
options: [{ properties: "always" }],
errors: [
{
message: "Identifier 'no_camelcased' is not in camel case.",
type: "Identifier"
}
]
},
{
code: "function foo({ value_1: my_default }) {}",
parserOptions: { ecmaVersion: 6 },
options: [{ properties: "always" }],
errors: [
{
message: "Identifier 'my_default' is not in camel case.",
type: "Identifier"
}
]
}
]
});

0 comments on commit c905181

Please sign in to comment.