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.

Boolean props should be false by default always. (require-default-prop)

See original GitHub issue

Tell us about your environment

  • ESLint Version: 4.9.0
  • eslint-plugin-vue Version: 3.13.1
  • Node Version: 8.2.1

Please show your full configuration:

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

<script>
export default {
  props: {
    showText: {
      type: Boolean,
      default: true
    },
    autoPlay: Boolean
  }
}
</script>

What did you expect to happen? An error should be shown for the showText prop indicating that it should not have a default of true. The autoPlay prop should considered correct even with the require-default-prop rule turned on.

What actually happened? Please include the actual, raw output from ESLint. With the require-default-prop rule enabled, autoPlay will be flagged. showText will be considered correct.

To make Vue components work like idiomatic HTML elements Boolean attributes/props should always default to false. If a Boolean prop defaults to true then a user who wants to turn that prop off has to bind the prop to a constant false (e.g., :show-text="false").

The require-default-prop rule would need to be modified to either enforce this, or ignore Boolean props. If the rule ignores Boolean props, then a new rule could be added to enforce proper Boolean props.

Boolean props should also never be required.

someBooleanProp: Boolean is how all Boolean props should be declared. There’s no need for any other prop options.

At the very least, something should be added to the rules to allow enforcing this. I also believe this rule should be added to the Style Guide.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:10
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

4reactions
shroudedcodecommented, May 27, 2018

But sometimes, you want notifications that aren’t dismissible, so you make the behavior optional with a new dismissible prop that defaults to true.

@chrisvfritz This prop could be named persistent making it false by default.

<alert dismissible="false">You have to accept our new terms to continue using XY.</alert>
<!-- vs -->
<alert persistent>You have to accept our new terms to continue using XY.</alert>

I can’t think of a single situation where this problem cannot be solved by renaming the prop. I think that with the right error message this rule could really benefit developers, especially if it includes a tip like this:

Try to inverse the name of the prop: If the component is not '{{ currentName }}', it's ...?

If I find the time, I might have a shot at implementing this rule.

3reactions
mysticateacommented, Nov 28, 2017

I feel that this is a good idea 👍, but it should be separated from require-default-prop rule.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can a Vue Boolean prop be true by its presence and false by ...
1 Answer 1. Sorted by: Reset to default. Highest score (default) ...
Read more >
vue/no-boolean-default
The rule prevents Boolean props from having a default value. ... enforce the HTML standard of always defaulting boolean attributes to false.
Read more >
JS.VUE.REQUIRE.DEFAULT.PROP
This rule requires default value to be set for each props that are not marked as required (except Boolean props). <script> export default...
Read more >
@vincent0119/eslint-config-vue - npm
<script> export default { props: { /* ✓ GOOD */ a: { type: Number, ... default: 0, required: false }, d: { type:...
Read more >
Codiga Analysis TypeScript Rules
If you are using React, NextJS, Vue or Angular: the engine will flag any issue in your ... By default this rule prevents...
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