The enforceConst option of the no-magic-numbers rule incorrectly reports a simple for loop
See original GitHub issueTell us about your environment
- ESLint Version:
v4.19.1
- Node Version:
v8.9.4
- npm Version:
6.0.0
What parser (default, Babel-ESLint, etc.) are you using? default
Please show your full configuration:
Configuration
{
"env": {
"browser": true,
"es6": true,
"greasemonkey": true
},
"extends": "eslint:recommended",
"globals": {
"min": false
},
"rules": {
"array-callback-return": "error",
"block-scoped-var": "error",
"curly": "error",
"dot-notation": "error",
"eol-last": [
"error",
"always"
],
"eqeqeq": "error",
"guard-for-in": "error",
"indent": [
"error",
4, {
"SwitchCase": 1
}
],
"linebreak-style": [
"error",
"unix"
],
"no-case-declarations": "error",
"no-else-return": "error",
"no-empty-function": "error",
"no-eq-null": "error",
"no-eval": "error",
"no-extra-bind": "error",
"no-floating-decimal": "error",
"no-implicit-coercion": "error",
"no-implicit-globals": "error",
"no-implied-eval": "error",
"no-invalid-this": "error",
"no-loop-func": "error",
"no-lone-blocks": "error",
"no-magic-numbers": [
"error", {
"enforceConst": true,
"ignoreArrayIndexes": true
}
],
"no-multiple-empty-lines": [
"error", {
"max": 1,
"maxBOF": 0,
"maxEOF": 1
}
],
"no-trailing-spaces": "error",
"quotes": [
"error",
"double"
],
"semi": [
"error",
"always"
],
"valid-jsdoc": [
"error", {
"requireReturn": false
}
]
}
}
What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.
(function () {
var array = [];
for (let i = 0; i < array.length; i++) {
array[i] = i;
}
})();
./node_modules/.bin/eslint index.js
What did you expect to happen?
Not throw an error.
What actually happened? Please include the actual, raw output from ESLint.
/Users/revolt/Development/index.js
4:18 error Number constants declarations must use 'const' no-magic-numbers
✖ 1 problem (1 error, 0 warnings)
Issue Analytics
- State:
- Created 5 years ago
- Comments:13 (12 by maintainers)
Top Results From Across the Web
no-magic-numbers - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >no-magic-numbers - TypeScript ESLint
This rule extends the base eslint/no-magic-numbers rule. It adds support for: ... Note: you must disable the base rule as it can report...
Read more >Disallow magic numbers ( no-magic-numbers ) - GitLab
This rule extends the base eslint/no-magic-numbers rule. ... the base rule as it can report incorrect errors "no-magic-numbers": "off", ...
Read more >What is a magic number, and why is it bad? - Stack Overflow
First, magic numbers are not just numbers. Any basic value can be "magic". Basic values are manifest entities such as integers, reals, doubles,...
Read more >no-magic-numbers - Rules - ESLint中文文档
The no-magic-numbers rule aims to make code more readable and refactoring easier by ... Examples of incorrect code for the { "enforceConst": true...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think we’re straying from the point of the
enforceConst
option. Imagine the code looked like this:I would argue that the use of
4124
is a code smell, because it’s a “magic number” and provides no readable info about what that particular value is for.The correct fix for the issue would be to do something like this:
The
enforceConst
option on theno-magic-numbers
rule controls whether the declaration ofINITIAL_NUM_WIDGETS
must be declared withconst
, or whethervar
is also acceptable. It’s not related to the initialization ofi
.If the purpose of the rule is to give a name to special constants, then I don’t think it makes sense to accept a loop iteration variable as the “name” of the constant, because loop iteration variables change their value, so they would not consistently refer to that constant.
For example, the following correction to the original code would not make sense:
I would argue that the above code still has the same code smell as the original code, because it’s still not clear where the value of
4124
comes from. The nameINITIAL_NUM_WIDGETS
doesn’t clarify the situation, because it refers to a value that changes within the loop, rather than always referring to the value4124
.@not-an-aardvark, Does my last comment make sense?