From 04dcf163413e476372850895880dc45c4eef065a Mon Sep 17 00:00:00 2001 From: Sergey Sova Date: Fri, 13 Oct 2017 19:45:44 +0300 Subject: [PATCH] Add rule no-magic-numbers --- .eslintrc.js | 7 +++++++ example/index.js | 16 +++++++++++----- readme.md | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 361a25e..7cd73a6 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -17,6 +17,13 @@ module.exports = { 'semi': ['error', 'never'], 'no-confusing-arrow': 'off', + 'no-magic-numbers': ['warn', { + ignore: [1], + ignoreArrayIndexes: true, + enforceConst: true, + detectObjects: false, + }], + 'comma-dangle': ['error', { arrays: 'always-multiline', objects: 'always-multiline', diff --git a/example/index.js b/example/index.js index a5df01c..818be9e 100644 --- a/example/index.js +++ b/example/index.js @@ -9,18 +9,21 @@ const example = { first: 1, second: 2, 'third-e': [ - 1, - 2, - 3, + '1', + '2', + '3', '4', ], five, four, } +const PART_NUM = 12 + const inside = resolve( example, `report${example['third-e'].join(',')}`, + `foo${PART_NUM}`, reinstall.path ) @@ -49,11 +52,14 @@ catch (error) { example.second = 0 } +const OFFSET = 12 +const INCR = 0.3 + let target = global.meet ? 'overrided' : 'misleaded' -target = example.first + 12 +target = example.first + OFFSET -export const demo = target * 0.3 +export const demo = target * INCR export default example diff --git a/readme.md b/readme.md index d7d0198..ff25f9c 100644 --- a/readme.md +++ b/readme.md @@ -80,6 +80,39 @@ const x = a => 1 ? 2 : 3 ``` +## no-magic-numbers: warn + +```js +{ + ignore: [1], + ignoreArrayIndexes: true, + enforceConst: true, + detectObjects: false, +} +``` + +https://eslint.org/docs/rules/no-magic-numbers + +Magic numbers is not descriptive. Please name it. + +Exclude: 1, array indexes, object of numbers. + +```js +// Good + +const TAX = 1.28 +const Coeff = { + A: 0.924, + B: 0.759, + C: 0.552, + V: 0.308, + Z: 0.116, +} +const val = myList[5] +const prev = myList[val.next - 1] +``` + + ## comma-dangle: error ```js @@ -348,7 +381,7 @@ import FooBar from './foo-bar' // Correct import. But not solved by rule ## unicorn/catch-error-name: error ```js -{ name: 'error } +{ name: 'error' } ``` https://github.com/sindresorhus/eslint-plugin-unicorn/blob/master/docs/rules/catch-error-name.md