From 4242314215a6f35e432860433906f47af1a29724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20D=C3=A1nyi?= Date: Sun, 23 Dec 2018 00:10:17 +0100 Subject: [PATCH] Update: handle computed properties in camelcase (fixes #11084) (#11113) --- lib/rules/camelcase.js | 19 ++++++++++++++----- tests/lib/rules/camelcase.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/lib/rules/camelcase.js b/lib/rules/camelcase.js index 6fb8760f105..a8341c84b67 100644 --- a/lib/rules/camelcase.js +++ b/lib/rules/camelcase.js @@ -100,14 +100,20 @@ module.exports = { * @private */ function isInsideObjectPattern(node) { - let { parent } = node; + let current = node; - while (parent) { - if (parent.type === "ObjectPattern") { + while (current) { + const parent = current.parent; + + if (parent && parent.type === "Property" && parent.computed && parent.key === current) { + return false; + } + + if (current.type === "ObjectPattern") { return true; } - parent = parent.parent; + current = parent; } return false; @@ -169,12 +175,15 @@ module.exports = { if (node.parent.parent && node.parent.parent.type === "ObjectPattern") { if (node.parent.shorthand && node.parent.value.left && nameIsUnderscored) { - report(node); } const assignmentKeyEqualsValue = node.parent.key.name === node.parent.value.name; + if (isUnderscored(name) && node.parent.computed) { + report(node); + } + // prevent checking righthand side of destructured object if (node.parent.key === node && node.parent.value !== node) { return; diff --git a/tests/lib/rules/camelcase.js b/tests/lib/rules/camelcase.js index d1ce3b3fd7b..eb80b4ef4d2 100644 --- a/tests/lib/rules/camelcase.js +++ b/tests/lib/rules/camelcase.js @@ -106,6 +106,11 @@ ruleTester.run("camelcase", rule, { options: [{ ignoreDestructuring: true }], parserOptions: { ecmaVersion: 6 } }, + { + code: "var { [{category_id} = query]: categoryId } = query;", + options: [{ ignoreDestructuring: true }], + parserOptions: { ecmaVersion: 6 } + }, { code: "var { category_id: category } = query;", parserOptions: { ecmaVersion: 6 } @@ -345,6 +350,29 @@ ruleTester.run("camelcase", rule, { } ] }, + { + code: "var { [category_id]: categoryId } = query;", + options: [{ ignoreDestructuring: true }], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "notCamelCase", + data: { name: "category_id" }, + type: "Identifier" + } + ] + }, + { + code: "var { [category_id]: categoryId } = query;", + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "notCamelCase", + data: { name: "category_id" }, + type: "Identifier" + } + ] + }, { code: "var { category_id: categoryId, ...other_props } = query;", options: [{ ignoreDestructuring: true }],