javascripteslinteslintrc

ESLINT: Prevent the use of double exclamation (!!)


What is the rule and configuration required to stop eslint (when using eslint --fix) from changing:

return regex.test(foo) ? true : false

into this:

return !!regex.test(foo)

While I understand what this rule is doing, I don't like it. I thought it might be one of these -- however, it's either neither, or I just fail to understand how to configure them correctly.

// eslintrc.js

module.exports = {
  rules: {
    'no-implicit-coercion': [2, { string: false, boolean: false, number: false }],
    'no-extra-boolean-cast': [2, { enforceForLogicalOperands: true }],
  }
}

Solution

  • Looks like no-unneeded-ternary is converting this, as it is ultimately superfluous and can be written more cleanly and succinctly without a ternary. That said, as Salman A points out in the comments, it is somewhat curious that the fix itself is leveraging what is considered a bad practice of the !! Boolean casting.