Skip to content

Commit

Permalink
Fix: no-magic-numbers false negative for reassigned vars (fixes eslin…
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Aug 31, 2016
1 parent 6869c60 commit 36427f0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 4 additions & 2 deletions lib/rules/no-magic-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,10 @@ module.exports = {
message: "Number constants declarations must use 'const'."
});
}
} else if (okTypes.indexOf(parent.type) === -1 ||
(parent.type === "AssignmentExpression" && parent.operator !== "=")) {
} else if (
okTypes.indexOf(parent.type) === -1 ||
(parent.type === "AssignmentExpression" && parent.left.type === "Identifier")
) {
context.report({
node,
message: "No magic number: " + raw + "."
Expand Down
12 changes: 9 additions & 3 deletions tests/lib/rules/no-magic-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ ruleTester.run("no-magic-numbers", rule, {
ignore: [0, 1, 2, 3, 4]
}]
},
{
code: "var min, max, mean; min = 1; max = 10; mean = 4;"
},
{
code: "var foo = { bar:10 }"
},
Expand Down Expand Up @@ -233,6 +230,15 @@ ruleTester.run("no-magic-numbers", rule, {
{ message: "No magic number: 2.", line: 1 },
{ message: "No magic number: 3.", line: 1 }
]
},
{
code: "var min, max, mean; min = 1; max = 10; mean = 4;",
options: [{}],
errors: [
{message: "No magic number: 1.", line: 1},
{message: "No magic number: 10.", line: 1},
{message: "No magic number: 4.", line: 1}
]
}
]
});

0 comments on commit 36427f0

Please sign in to comment.