Skip to content
This repository has been archived by the owner on Jan 11, 2018. It is now read-only.

Commit

Permalink
Add rule no-magic-numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Sova committed Oct 13, 2017
1 parent bc33476 commit 04dcf16
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
16 changes: 11 additions & 5 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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
35 changes: 34 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 04dcf16

Please sign in to comment.