Skip to content

Commit

Permalink
Fix: Add no-magic-numbers 'ignoreDefaultValues' option
Browse files Browse the repository at this point in the history
  • Loading branch information
moeriki committed Nov 27, 2019
1 parent ea16de4 commit 40a92dc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
12 changes: 12 additions & 0 deletions docs/rules/no-magic-numbers.md
Expand Up @@ -78,6 +78,18 @@ var data = ['foo', 'bar', 'baz'];
var dataLast = data[2];
```

### ignoreDefaultValues

A boolean to specify if numbers used in default value assignmength are considered okay. `false` by default.

Examples of **correct** code for the `{ "ignoreDefaultValues": true }` option:

```js
/*eslint no-magic-numbers: ["error", { "ignoreDefaultValues": true }]*/

const { tax = 0.25 } = accountancy;
```

### enforceConst

A boolean to specify if we should check for the const keyword in variable declaration of numbers. `false` by default.
Expand Down
18 changes: 17 additions & 1 deletion lib/rules/no-magic-numbers.js
Expand Up @@ -41,6 +41,10 @@ module.exports = {
ignoreArrayIndexes: {
type: "boolean",
default: false
},
ignoreDefaultValues: {
type: "boolean",
default: false
}
},
additionalProperties: false
Expand All @@ -57,7 +61,8 @@ module.exports = {
detectObjects = !!config.detectObjects,
enforceConst = !!config.enforceConst,
ignore = config.ignore || [],
ignoreArrayIndexes = !!config.ignoreArrayIndexes;
ignoreArrayIndexes = !!config.ignoreArrayIndexes,
ignoreDefaultValues = !!config.ignoreDefaultValues;

/**
* Returns whether the node is number literal
Expand Down Expand Up @@ -109,6 +114,16 @@ module.exports = {
return parent.type === "MemberExpression" && ignoreArrayIndexes;
}

/**
* Returns whether the number should be ignore when used as a default
* value assignment.
* @param {ASTNode} parent the non-"UnaryExpression" parent.
* @returns {boolean} true if the number should be ignored
*/
function shouldIgnoreDefaultValues(parent) {
return parent.type === "AssignmentPattern" && ignoreDefaultValues;
}

return {
Literal(node) {
const okTypes = detectObjects ? [] : ["ObjectExpression", "Property", "AssignmentExpression"];
Expand Down Expand Up @@ -138,6 +153,7 @@ module.exports = {
if (shouldIgnoreNumber(value) ||
shouldIgnoreParseInt(parent, fullNumberNode) ||
shouldIgnoreArrayIndexes(parent) ||
shouldIgnoreDefaultValues(parent) ||
shouldIgnoreJSXNumbers(parent)) {
return;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/lib/rules/no-magic-numbers.js
Expand Up @@ -75,6 +75,20 @@ ruleTester.run("no-magic-numbers", rule, {
jsx: true
}
}
},
{
code: "const { param = 123 } = sourceObject;",
options: [{
ignoreDefaultValues: true
}],
env: { es6: true }
},
{
code: "const func = ({ param = 123 }) => {}",
options: [{
ignoreDefaultValues: true
}],
env: { es6: true }
}
],
invalid: [
Expand Down Expand Up @@ -227,6 +241,16 @@ ruleTester.run("no-magic-numbers", rule, {
{ messageId: "noMagic", data: { raw: "10" }, line: 1 },
{ messageId: "noMagic", data: { raw: "4" }, line: 1 }
]
},
{
code: "const { param = 123 } = sourceObject;",
options: [{
ignoreDefaultValues: false
}],
env: { es6: true },
errors: [
{ messageId: "noMagic", data: { raw: "123" }, line: 1 }
]
}
]
});

0 comments on commit 40a92dc

Please sign in to comment.