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

no-extra-boolean-cast autofixer handles some Boolean() calls incorrectly #7977

Closed
not-an-aardvark opened this issue Jan 25, 2017 · 2 comments · Fixed by renovatebot/renovate#111 · May be fixed by iamhunter/teammates#4
Closed
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 bug ESLint is working incorrectly good first issue Good for people who haven't worked on ESLint before help wanted The team would welcome a contribution from the community for this issue rule Relates to ESLint's core rules

Comments

@not-an-aardvark
Copy link
Member

not-an-aardvark commented Jan 25, 2017

Tell us about your environment

  • ESLint Version: master
  • Node Version: 7.4.0
  • npm Version: 4.0.5

What parser (default, Babel-ESLint, etc.) are you using?

default

Please show your full configuration:

rules:
  no-extra-boolean-cast: error
parserOptions:
  ecmaVersion: 2015

What did you do? Please include the actual source code causing the issue.

I ran eslint theFile.js --fix on three different files:

// Case 1
!Boolean(...foo);
// Case 2
!Boolean(foo, bar());
// Case 3
!Boolean();

What did you expect to happen?

I expected the syntax of the files to be valid after autofixing

What actually happened? Please include the actual, raw output from ESLint.

In the first two files, the code was autofixed to

// Case 1
!...foo;
// Case 2
!foo;

An error was thrown when running the command on the third file:

TypeError: Cannot read property 'type' of undefined
    at Object.getPrecedence (/path/to/eslint/lib/ast-utils.js:620:21)
    at Object.fix (/path/to/eslint/lib/rules/no-extra-boolean-cast.js:103:42)
    at RuleContext.report (/path/to/eslint/lib/rule-context.js:127:34)
    at EventEmitter.CallExpression (/path/to/eslint/lib/rules/no-extra-boolean-cast.js:97:29)
    at emitOne (events.js:96:13)
    at EventEmitter.emit (events.js:188:7)
    at NodeEventGenerator.enterNode (/path/to/eslint/lib/util/node-event-generator.js:39:22)
    at CodePathAnalyzer.enterNode (/path/to/eslint/lib/code-path-analysis/code-path-analyzer.js:607:23)
    at CommentEventGenerator.enterNode (/path/to/eslint/lib/util/comment-event-generator.js:98:23)
    at Controller.enter (/path/to/eslint/lib/eslint.js:928:36)

The first autofixed line is a syntax error, and the second autofixed line changes the behavior of the code (it no longer calls bar()).

To fix this, I think we need to avoid doing an autofix for no-extra-boolean-cast in the following cases:

  • Boolean is called with more than one argument
  • Boolean is called with a spread element as the first argument
  • Boolean is called with no arguments. (Alternatively, we could just fix !Boolean() to true.)
@not-an-aardvark not-an-aardvark added accepted There is consensus among the team that this change meets the criteria for inclusion good first issue Good for people who haven't worked on ESLint before bug ESLint is working incorrectly rule Relates to ESLint's core rules labels Jan 25, 2017
@ljharb
Copy link
Sponsor Contributor

ljharb commented Jan 25, 2017

Why would anyone call Boolean with more than one argument?

Boolean() should be equivalent to Boolean(undefined), in other words, false.

@not-an-aardvark
Copy link
Member Author

Why would anyone call Boolean with more than one argument?

¯\_(ツ)_/¯

Boolean() should be equivalent to Boolean(undefined), in other words, false.

Yes, but the rule only activates when the expression is negated to !Boolean() (since the Boolean call is then a useless cast), which would be true.

@kaicataldo kaicataldo added the help wanted The team would welcome a contribution from the community for this issue label Feb 2, 2017
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
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 bug ESLint is working incorrectly good first issue Good for people who haven't worked on ESLint before help wanted The team would welcome a contribution from the community for this issue rule Relates to ESLint's core rules
Projects
None yet
3 participants