Skip to content

Commit

Permalink
Fix: camelcase destructure leading/trailing underscore (fixes #9700) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
platinumazure authored and ilyavolodin committed Dec 11, 2017
1 parent d49d9d0 commit d80aa7c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rules/camelcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ module.exports = {

if (node.parent.parent && node.parent.parent.type === "ObjectPattern") {

if (node.parent.shorthand && node.parent.value.left && isUnderscored(node.parent.value.left.name)) {
if (node.parent.shorthand && node.parent.value.left && isUnderscored(name)) {

report(node);
}
Expand All @@ -119,7 +119,7 @@ module.exports = {
return;
}

if (node.parent.value.name && isUnderscored(node.parent.value.name)) {
if (node.parent.value.name && isUnderscored(name)) {
report(node);
}
}
Expand Down
80 changes: 80 additions & 0 deletions tests/lib/rules/camelcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,46 @@ ruleTester.run("camelcase", rule, {
code: "var o = {key: 1}",
options: [{ properties: "always" }]
},
{
code: "var o = {_leading: 1}",
options: [{ properties: "always" }]
},
{
code: "var o = {trailing_: 1}",
options: [{ properties: "always" }]
},
{
code: "var o = {bar_baz: 1}",
options: [{ properties: "never" }]
},
{
code: "var o = {_leading: 1}",
options: [{ properties: "never" }]
},
{
code: "var o = {trailing_: 1}",
options: [{ properties: "never" }]
},
{
code: "obj.a_b = 2;",
options: [{ properties: "never" }]
},
{
code: "obj._a = 2;",
options: [{ properties: "always" }]
},
{
code: "obj.a_ = 2;",
options: [{ properties: "always" }]
},
{
code: "obj._a = 2;",
options: [{ properties: "never" }]
},
{
code: "obj.a_ = 2;",
options: [{ properties: "never" }]
},
{
code: "var obj = {\n a_a: 1 \n};\n obj.a_b = 2;",
options: [{ properties: "never" }]
Expand All @@ -63,14 +95,38 @@ ruleTester.run("camelcase", rule, {
code: "var { category_id: category } = query;",
parserOptions: { ecmaVersion: 6 }
},
{
code: "var { _leading } = query;",
parserOptions: { ecmaVersion: 6 }
},
{
code: "var { trailing_ } = query;",
parserOptions: { ecmaVersion: 6 }
},
{
code: "import { camelCased } from \"external module\";",
parserOptions: { ecmaVersion: 6, sourceType: "module" }
},
{
code: "import { _leading } from \"external module\";",
parserOptions: { ecmaVersion: 6, sourceType: "module" }
},
{
code: "import { trailing_ } from \"external module\";",
parserOptions: { ecmaVersion: 6, sourceType: "module" }
},
{
code: "import { no_camelcased as camelCased } from \"external-module\";",
parserOptions: { ecmaVersion: 6, sourceType: "module" }
},
{
code: "import { no_camelcased as _leading } from \"external-module\";",
parserOptions: { ecmaVersion: 6, sourceType: "module" }
},
{
code: "import { no_camelcased as trailing_ } from \"external-module\";",
parserOptions: { ecmaVersion: 6, sourceType: "module" }
},
{
code: "import { no_camelcased as camelCased, anoterCamelCased } from \"external-module\";",
parserOptions: { ecmaVersion: 6, sourceType: "module" }
Expand All @@ -79,13 +135,37 @@ ruleTester.run("camelcase", rule, {
code: "function foo({ no_camelcased: camelCased }) {};",
parserOptions: { ecmaVersion: 6 }
},
{
code: "function foo({ no_camelcased: _leading }) {};",
parserOptions: { ecmaVersion: 6 }
},
{
code: "function foo({ no_camelcased: trailing_ }) {};",
parserOptions: { ecmaVersion: 6 }
},
{
code: "function foo({ camelCased = 'default value' }) {};",
parserOptions: { ecmaVersion: 6 }
},
{
code: "function foo({ _leading = 'default value' }) {};",
parserOptions: { ecmaVersion: 6 }
},
{
code: "function foo({ trailing_ = 'default value' }) {};",
parserOptions: { ecmaVersion: 6 }
},
{
code: "function foo({ camelCased }) {};",
parserOptions: { ecmaVersion: 6 }
},
{
code: "function foo({ _leading }) {}",
parserOptions: { ecmaVersion: 6 }
},
{
code: "function foo({ trailing_ }) {}",
parserOptions: { ecmaVersion: 6 }
}
],
invalid: [
Expand Down

0 comments on commit d80aa7c

Please sign in to comment.