Skip to content

Commit

Permalink
Update: Proposed no-magic-numbers option: ignoreJSXNumbers (fixes #5348)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsbeeks committed Mar 4, 2016
1 parent a8cf60b commit 1b2c6e0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/rules/no-magic-numbers.js
Expand Up @@ -74,6 +74,15 @@ module.exports = function(context) {
parent.callee.property.name === "parseInt");
}

/**
* Returns whether the number should be ignored when used to define a JSX prop
* @param {ASTNode} parent - the non-"UnaryExpression" parent
* @returns {boolean} true if the number should be ignored
*/
function shouldIgnoreJSXNumbers(parent) {
return parent.type.indexOf("JSX") === 0;
}

/**
* Returns whether the number should be ignored when used as an array index with enabled 'ignoreArrayIndexes' option.
* @param {ASTNode} parent - the non-"UnaryExpression" parent.
Expand Down Expand Up @@ -104,7 +113,8 @@ module.exports = function(context) {

if (shouldIgnoreNumber(value) ||
shouldIgnoreParseInt(parent, node) ||
shouldIgnoreArrayIndexes(parent)) {
shouldIgnoreArrayIndexes(parent) ||
shouldIgnoreJSXNumbers(parent)) {
return;
}

Expand Down
29 changes: 29 additions & 0 deletions tests/lib/rules/no-magic-numbers.js
Expand Up @@ -73,6 +73,22 @@ ruleTester.run("no-magic-numbers", rule, {
options: [{
ignoreArrayIndexes: true
}]
},
{
code: "var a = <input maxLength={10} />;",
parserOptions: {
ecmaFeatures: {
jsx: true
}
}
},
{
code: "var a = <div objectProp={{ test: 1}}></div>;",
parserOptions: {
ecmaFeatures: {
jsx: true
}
}
}
],
invalid: [
Expand Down Expand Up @@ -193,6 +209,19 @@ ruleTester.run("no-magic-numbers", rule, {
errors: [{
message: "No magic number: 3", line: 1
}]
},
{
code: "var a = <div arrayProp={[1,2,3]}></div>;",
parserOptions: {
ecmaFeatures: {
jsx: true
}
},
errors: [
{ message: "No magic number: 1", line: 1 },
{ message: "No magic number: 2", line: 1 },
{ message: "No magic number: 3", line: 1 }
]
}
]
});

0 comments on commit 1b2c6e0

Please sign in to comment.