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.
This commit is contained in:
Mauro Bringolf
2017-08-25 03:19:02 +02:00
committed by Justin Ridgewell
parent 3c4f19a28d
commit d8b4073536
3 changed files with 12 additions and 9 deletions

View File

@@ -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);
}
}
}

View File

@@ -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);
}
},

View File

@@ -89,6 +89,9 @@ getBindingIdentifiers.keys = {
FunctionDeclaration: ["id", "params"],
FunctionExpression: ["id", "params"],
ForInStatement: ["left"],
ForOfStatement: ["left"],
ClassDeclaration: ["id"],
ClassExpression: ["id"],