question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[no-magic-numbers] - false error report on array destructuring declaration

See original GitHub issue

Tell us about your environment

Node version: v12.12.0 npm version: v6.9.0 Local ESLint version: v6.8.0 (Currently used) Global ESLint version: Not found

What parser (default, Babel-ESLint, etc.) are you using? typescript 3.8 Please show your full configuration:

Configuration
env:
  es2020: true
  browser: true
extends:
  - "eslint:recommended"
  - "plugin:@typescript-eslint/recommended"
  - "plugin:@typescript-eslint/eslint-recommended"
  - "plugin:functional/all"
  - "plugin:jest/recommended"
  - "plugin:react/recommended"
  - "plugin:no-unsanitized/DOM"
  - "prettier"
  - "prettier/@typescript-eslint"
parser: "@typescript-eslint/parser"
parserOptions:
  #  project: "./tsconfig.json"
  sourceType: module
plugins:
  - "@typescript-eslint/eslint-plugin"
  - functional
  - jest
  - no-unsanitized
  - promise
  - react
  - react-hooks
overrides:
  - files:
      - "*.test.js"
      - "*.spec.js"
globals:
  globalThis: "writable"
#  devprint: "readonly"
rules:
  "@typescript-eslint/camelcase": off
  "@typescript-eslint/class-name-casing":
    - off
  "@typescript-eslint/explicit-function-return-type":
    - error
    - allowExpressions: true
  "@typescript-eslint/interface-name-prefix":
    - warn
  "@typescript-eslint/no-empty-interface":
    - error
    - "allowSingleExtends": true
  "@typescript-eslint/no-use-before-define": off
  arrow-body-style: "off"
  arrow-parens: warn
  camelcase: off
  comma-dangle: "off"
  complexity:
    - warn
    - 3
  func-style: warn
  functional/functional-parameters:
    - error
    - enforceParameterCount: false
  functional/immutable-data:
    - error
    - ignorePattern:
        - globalThis
  functional/prefer-readonly-type: off
  functional/no-conditional-statement: off
  functional/no-expression-statement:
    - off
    - ignorePattern:
        - console
        - devprint
        - globalThis
        - render

  functional/no-return-void: off
  id-length:
    - warn
    - {min: 3, exceptions: [_, fn, id, is, BG]}
  id-match:
    - error
    - "^[a-zA-Z_0-9]*$"
    - properties: true
  indent: off
  lines-around-comment:
    - warn
    - ignorePattern: /prettier-ignore/
  max-len:
    - warn
    - code: 90
      comments: 120
      tabWidth: 2
      #      "^\\} from" part is for multi-line imports
      ignorePattern: "Spec|import|^\\} from"
      ignoreUrls: true
  max-lines:
    - warn
    - max: 50
      skipBlankLines: true
  max-params:
    - warn
    - 6
  max-statements:
    - warn
    - 10
  multiline-ternary: "off"
  new-cap: off
  no-console: warn
  no-inline-comments: "off"
  no-magic-numbers:
    - error
    - ignore:
        - 0
        - 1
        - -1
  no-multi-spaces: "off"
  no-param-reassign:
    - error
    - props: true
  no-nested-ternary: "off"
  no-return-await: warn
  no-shadow:
    - error
    - builtinGlobals: true
      allow:
        - "name"
        - "status"
  no-warning-comments: off
  no-undef: error
  no-underscore-dangle:
    - error
    - "allow": ["_id", "_text"]
  no-unneeded-ternary: "off"
  no-unused-vars: warn
  no-use-before-define: "off"
  no-useless-catch: warn
  object-curly-newline: "off"
  one-var: off
  operator-linebreak:
    - warn
    - before
    - overrides:
        +: ignore
        =: ignore
  padding-line-between-statements: "off"
  prefer-destructuring: warn
  radix: "off"
  react/no-string-refs:
    - error
    - noTemplateLiterals: true
  react-hooks/rules-of-hooks: error
  react-hooks/exhaustive-deps: warn
  react/display-name: "off"
  react/prop-types: "off"
  semi: warn
settings: {}

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

const [widht, height] = [10, 20];
I use Webstorm's built-in eslint reporting service

What did you expect to happen? No errors reported

What actually happened? Please include the actual, raw output from ESLint. No magic number: 10. (no-magic-numbers)

Are you willing to submit a pull request to fix this bug? noooo…

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
yeonjuancommented, Feb 9, 2020

Thanks for reporting!

In my opinion, It makes sense to treat it as a bug because all numbers are named.

2reactions
mdjermanoviccommented, Feb 11, 2020

Something like this might be correct:

/* eslint no-magic-numbers: ["error", { "enforceConst": false }] */

const [a, b] = [1, 2]; // ok

let [a, b] = [1, 2]; // ok

var [a, b] = [1, 2]; // ok

[a, b] = [1, 2]; // error
/* eslint no-magic-numbers: ["error", { "enforceConst": true }] */

const [a, b] = [1, 2]; // ok

let [a, b] = [1, 2]; // error

var [a, b] = [1, 2]; // error

[a, b] = [1, 2]; // error
/* eslint no-magic-numbers: ["error", { "detectObjects": false }] */

[obj.a, obj.b] = [1, 2]; // ok
/* eslint no-magic-numbers: ["error", { "detectObjects": true }] */

[obj.a, obj.b] = [1, 2]; // error

We could find the assignment target for the number, and if it’s a MemberExpression then allow/disallow based on detectObjects.

If it’s an Identifier, then allow only if the assignment is initializing variable (VariableDeclarator, which must be in a const declaration if "enforceConst": true).

Perhaps we could do this for object patterns as well, maybe even support nested destructuring.

There is also proposal #12611 to optionally allow default assignments.

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-magic-numbers - ESLint - Pluggable JavaScript Linter
A boolean to specify if numbers used in the context of array indexes (e.g., data[2] ) are considered okay. false by default. This...
Read more >
Reduce smelling code and detect JavaScript issues with ESLint
Problem statement: Smelling Code Have you ever had to fix an error in someone else's code - but you had to reformat the...
Read more >
ESLint with TypeScript and Firebase - no-undef error
I use eslint in my project with typescript-eslint to be able to lint TypeScript code. I have an issue with an import provided...
Read more >
no-unsafe-assignment | typescript-eslint
This rule disallows assigning any to a variable, and assigning any[] to an array destructuring. This rule also compares generic type argument types...
Read more >
declaration or statement expected. this '=' follows a block of ...
Today, you'll get an error on the = like. Declaration or statement expected. Destructuring assignments are super confusing on their own because object ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found