Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: Add no-magic-numbers 'ignoreDefaultValues' option #12611

Merged
merged 14 commits into from Aug 20, 2020

Conversation

moeriki
Copy link
Contributor

@moeriki moeriki commented Nov 27, 2019

This was briefly discussed in #10751 and #11271. As far as I could tell people thought it was a good idea.

What is the purpose of this pull request?

[X] Changes an existing rule

What rule do you want to change?

no-magic-numbers

Does this change cause the rule to produce more or fewer warnings?

Fewer.

How will the change be implemented?

I added an option called ignoreDefaultValues.

Please provide some example code that this change will affect:

const { WEB = 3000 } = process.env;
const mapPromises = ({ concurrency = 2 }) => {};

What does the rule currently do for this code?

Error: No magic number: 3000
Error: No magic number: 2

What will the rule do after it's changed?

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Nov 27, 2019
@moeriki
Copy link
Contributor Author

moeriki commented Nov 27, 2019

I'd consider ignoring nullish coalescing assignment through the same option.

const port = process.env.PORT ?? 3000;

This can be done with the following additional check.

parent.type === 'LogicalExpression' && parent.operator === '??'

What do you think?

@kaicataldo kaicataldo added enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Nov 27, 2019
@moeriki moeriki force-pushed the no-magic-number-ignore-default-values branch from 40a92dc to ee1ef95 Compare November 29, 2019 09:48
@kaicataldo
Copy link
Member

I'd consider ignoring nullish coalescing assignment through the same option.

I think this makes sense, though I don't know if it makes sense to put it behind the ignoreDefaultValues flag.

I support this change. We'll need a champion and two more supporters from the team before we can mark this as accepted.

@mdjermanovic
Copy link
Member

Should the option allow all default values regardless of the context? For example, the following are also default assignments (AssignmentPattern):

let WEB;
({ WEB = 3000 } = process.env);

const [foo = 89] = bar;

function doSomething(foo = 33) {}

for (const { value = 42 } of values) {}

@moeriki
Copy link
Contributor Author

moeriki commented Feb 7, 2020

Should the option allow all default values regardless of the context?

I think yes.

For me the purpose is to allow numbers where the variable is providing the necessary description.

@mdjermanovic
Copy link
Member

Thanks for the clarification!

If the proposal gets accepted we should certainly add more examples in the documentation and more tests to specify the intention. Someone might expect that the option applies only to declarations, while it also allows magic numbers in assignments, e.g.,:

[foo = 35, bar = 20] = getSomething();

@moeriki
Copy link
Contributor Author

moeriki commented Feb 10, 2020

we should certainly add more examples in the documentation and more tests to specify the intention.

Agreed. I'm awaiting maintainer approval to update the PR.

@moeriki
Copy link
Contributor Author

moeriki commented Apr 27, 2020

Hi all! I would still love this option. Is there anything I can do to move this forward?

Besides rebase and resolve conflicts 😁

@kaicataldo
Copy link
Member

@eslint/eslint-team It looks like we need a champion and one more 👍 to accept this.

@nzakas nzakas self-assigned this Jun 19, 2020
@nzakas
Copy link
Member

nzakas commented Jun 19, 2020

I will champion this. @moeriki if you're still up to update this PR, we can move forward. Sorry for the delay.

@btmills
Copy link
Member

btmills commented Jun 19, 2020

This makes a lot of sense. Added my 👍 and marking as accepted!

@btmills btmills added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Jun 19, 2020
@moeriki moeriki force-pushed the no-magic-number-ignore-default-values branch from ee1ef95 to d07b0b0 Compare June 20, 2020 10:07
Copy link
Member

@kaicataldo kaicataldo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM, but it looks like we're missing tests for array destructuring with default values. e.g.

const [ el = 123] = arr;

docs/rules/no-magic-numbers.md Show resolved Hide resolved
lib/rules/no-magic-numbers.js Outdated Show resolved Hide resolved
tests/lib/rules/no-magic-numbers.js Show resolved Hide resolved
lib/rules/no-magic-numbers.js Outdated Show resolved Hide resolved
tests/lib/rules/no-magic-numbers.js Show resolved Hide resolved
@moeriki moeriki force-pushed the no-magic-number-ignore-default-values branch from 7276334 to 4cda76d Compare June 30, 2020 07:45
@mdjermanovic mdjermanovic changed the title Fix: Add no-magic-numbers 'ignoreDefaultValues' option Update: Add no-magic-numbers 'ignoreDefaultValues' option Jun 30, 2020
Copy link
Member

@btmills btmills left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two minor alterations, then LGTM - thanks for working on this @moeriki!

lib/rules/no-magic-numbers.js Outdated Show resolved Hide resolved
tests/lib/rules/no-magic-numbers.js Show resolved Hide resolved
docs/rules/no-magic-numbers.md Outdated Show resolved Hide resolved
lib/rules/no-magic-numbers.js Outdated Show resolved Hide resolved
lib/rules/no-magic-numbers.js Outdated Show resolved Hide resolved
lib/rules/no-magic-numbers.js Outdated Show resolved Hide resolved
Copy link
Member

@btmills btmills left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the two JSDoc comment updates requested by @mdjermanovic, LGTM - thank you!

Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@btmills
Copy link
Member

btmills commented Jul 13, 2020

@kaicataldo if you're happy with the array destructuring default tests in 7a942b6, then this can be merged!

@btmills btmills requested a review from kaicataldo July 20, 2020 15:28
@kaicataldo
Copy link
Member

@moeriki Do you mind fixing up the merge conflict? Otherwise LGTM.

…gnore-default-values

* upstream/master: (66 commits)
  Sponsors: Sync README with website
  Sponsors: Sync README with website
  Sponsors: Sync README with website
  Sponsors: Sync README with website
  Sponsors: Sync README with website
  Chore: remove leche (fixes eslint#13287) (eslint#13533)
  Sponsors: Sync README with website
  Sponsors: Sync README with website
  7.6.0
  Build: changelog update for 7.6.0
  Update: require `meta` for fixable rules in RuleTester (refs eslint#13349) (eslint#13489)
  Docs: fix broken links in developer guide (eslint#13518)
  Fix: Do not output `undefined` as line and column when it's unavailable (eslint#13519)
  Sponsors: Sync README with website
  Sponsors: Sync README with website
  Fix: Update the chatroom link to go directly to help channel (eslint#13536)
  Sponsors: Sync README with website
  Update: Change no-duplicate-case to comparing tokens (fixes eslint#13485) (eslint#13494)
  Docs: add ECMAScript 2020 to README (eslint#13510)
  7.5.0
  ...
@jsf-clabot
Copy link

jsf-clabot commented Aug 9, 2020

CLA assistant check
All committers have signed the CLA.

@moeriki
Copy link
Contributor Author

moeriki commented Aug 9, 2020

Merged master back in.

I mistakenly removed my committer email from my GitHub account. I did add it again. The CLA should be fine.

@btmills btmills dismissed kaicataldo’s stale review August 20, 2020 04:20

The most recent review was "Otherwise LGTM" once the merge conflict was resolved, which it has been.

@btmills btmills merged commit 66442a9 into eslint:master Aug 20, 2020
@btmills
Copy link
Member

btmills commented Aug 20, 2020

Thanks @moeriki! I'm looking forward to having this in the next release.

@moeriki moeriki deleted the no-magic-number-ignore-default-values branch August 20, 2020 07:21
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Mar 18, 2021
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Mar 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants