Skip to content

edit v-on-function-call to enforce @click="() => fn()" over @click="fn()" and @click="fn" #2001

Closed
@mesqueeb

Description

@mesqueeb

https://eslint.vuejs.org/rules/v-on-function-call.html#vue-v-on-function-call

I want to have a new option for v-on-function-call to enforce a 3rd way of writing that is not yet enforcable. My reasoning is very sound I believe bet let me elaborate:

Please describe what the rule should do:

There are 3 ways in Vue to pass an event listener in HTML:

<button @click="fn" />
<button @click="fn()" />
<button @click="() => fn()" />    

To me there are clear PROs for using the last way and only CONs for using the first two. This is why I want to enforce this style with a new rule.

Here is my reasoning:

<button @click="fn" />
  • CON: it's unclear to see what payload comes out of the event and what payload goes into the fn if any.
  • CON: sometimes fn doesn't accept a payload, but will still receive one because of this way of writing
  • CON: sometimes fn is used somewhere else with a payload, but passed here as well, but it will end up receiving PointerEvent | MouseEvent implicitly which can cause bugs that are hard to find because it's implicit
  • CON: a lot of VSCode themes color this the same color as a prop, so it becomes hard to see that it's a function:
    image
<button @click="fn()" />
  • CON: this just looks like a function is being passed and immidiately executed...
  • CON: if this were a prop, then this way of writing would immidiately execute the function. So it's really confusing
<button @click="() => fn()" />    
  • PRO: it doesn't look like a function is being immidiately executed
  • PRO: it's clear what payload comes out of the click event and what payload goes into the fn, if any

What category should the rule belong to?

[x] Enforces code style (layout)
[ ] Warns about a potential error (problem)
[ ] Suggests an alternate way of doing something (suggestion)
[ ] Other (please specify:)

Provide 2-3 code examples that this rule should warn about:

I believe the explanation above is clear enough but let me know if it doesn't

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions