From d8b4073536aa59b84f54970f94a66559f753e374 Mon Sep 17 00:00:00 2001 From: Mauro Bringolf Date: Fri, 25 Aug 2017 03:19:02 +0200 Subject: [PATCH] Consistent const violations (#6100) * Changed updateExpression to report itself as violation instead of its argument * Update getBindingIdentifiers to work with forXStatement and return proper node as violation * Updated unaryExpression violation to be consistent with changes. --- .../babel-plugin-check-es2015-constants/src/index.js | 12 ++++++------ packages/babel-traverse/src/scope/index.js | 6 +++--- packages/babel-types/src/retrievers.js | 3 +++ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/packages/babel-plugin-check-es2015-constants/src/index.js b/packages/babel-plugin-check-es2015-constants/src/index.js index 7a983364a5..ccbe33aea1 100644 --- a/packages/babel-plugin-check-es2015-constants/src/index.js +++ b/packages/babel-plugin-check-es2015-constants/src/index.js @@ -36,13 +36,13 @@ export default function({ messages, types: t }) { violation.get("right").node, ), ); - } else if (violation.parentPath.isUpdateExpression()) { - violation.parentPath.replaceWith( - statementBeforeExpression(throwNode, violation.parent), + } else if (violation.isUpdateExpression()) { + violation.replaceWith( + statementBeforeExpression(throwNode, violation.node), ); - } else if (violation.parentPath.isForXStatement()) { - violation.parentPath.ensureBlock(); - violation.parentPath.node.body.body.unshift(throwNode); + } else if (violation.isForXStatement()) { + violation.ensureBlock(); + violation.node.body.body.unshift(throwNode); } } } diff --git a/packages/babel-traverse/src/scope/index.js b/packages/babel-traverse/src/scope/index.js index 356b063980..b2cdc4238e 100644 --- a/packages/babel-traverse/src/scope/index.js +++ b/packages/babel-traverse/src/scope/index.js @@ -104,7 +104,7 @@ const collectorVisitor = { ForXStatement(path, state) { const left = path.get("left"); if (left.isPattern() || left.isIdentifier()) { - state.constantViolations.push(left); + state.constantViolations.push(path); } }, @@ -140,12 +140,12 @@ const collectorVisitor = { }, UpdateExpression(path, state) { - state.constantViolations.push(path.get("argument")); + state.constantViolations.push(path); }, UnaryExpression(path, state) { if (path.node.operator === "delete") { - state.constantViolations.push(path.get("argument")); + state.constantViolations.push(path); } }, diff --git a/packages/babel-types/src/retrievers.js b/packages/babel-types/src/retrievers.js index 8deb4053bf..7abdd1f333 100644 --- a/packages/babel-types/src/retrievers.js +++ b/packages/babel-types/src/retrievers.js @@ -89,6 +89,9 @@ getBindingIdentifiers.keys = { FunctionDeclaration: ["id", "params"], FunctionExpression: ["id", "params"], + ForInStatement: ["left"], + ForOfStatement: ["left"], + ClassDeclaration: ["id"], ClassExpression: ["id"],