Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: no-magic-numbers false negative on reassigned vars (fixes #4616
…) (#7028)
  • Loading branch information
not-an-aardvark authored and nzakas committed Sep 3, 2016
1 parent be29599 commit 22c7e09
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
8 changes: 8 additions & 0 deletions docs/rules/no-magic-numbers.md
Expand Up @@ -30,6 +30,14 @@ var data = ['foo', 'bar', 'baz'];
var dataLast = data[2];
```

```js
/*eslint no-magic-numbers: "error"*/

var SECONDS;

SECONDS = 60;
```

Examples of **correct** code for this rule:

```js
Expand Down
6 changes: 4 additions & 2 deletions lib/rules/no-magic-numbers.js
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
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 22c7e09

Please sign in to comment.