-
-
Notifications
You must be signed in to change notification settings - Fork 679
Add vue/v-on-handler-style
rule and deprecate vue/v-on-function-call
rule
#2009
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f099cbc
Add `vue/v-on-handler-style` rule and deprecated `vue/v-on-function-c…
ota-meshi 56fd2e9
Update docs/rules/v-on-handler-style.md
ota-meshi f76cfad
Update docs/rules/v-on-handler-style.md
ota-meshi bc1f8c8
simplify element in doc
ota-meshi 5bd54e9
add related rules to doc
ota-meshi e316a69
add ignored examples to doc
ota-meshi e9e9516
change to report morev
ota-meshi 194fe21
fix doc
ota-meshi cf2c89a
Merge branch 'master' into v-on-handler-style
ota-meshi 3395673
change v-on-handler-style
ota-meshi 7296bc9
refactor
ota-meshi c0e2fe1
Apply suggestions from code review
ota-meshi b471fd3
update docs
ota-meshi 7134914
fix error message
ota-meshi b5f4479
Merge branch 'master' into v-on-handler-style
ota-meshi 1b21be6
refactor
ota-meshi 6fd394d
Update docs/rules/v-on-handler-style.md
ota-meshi fb42bb5
Merge branch 'master' into v-on-handler-style
FloEdelmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/v-on-handler-style | ||
description: enforce writing style for handlers in `v-on` directives | ||
--- | ||
# vue/v-on-handler-style | ||
|
||
> enforce writing style for handlers in `v-on` directives | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. | ||
|
||
## :book: Rule Details | ||
|
||
This rule aims to enforce a consistent style in `v-on` event handlers: | ||
|
||
```vue | ||
<!-- Method handler: --> | ||
<button v-on:click="handler" /> | ||
|
||
<!-- Inline handler: --> | ||
<button v-on:click="handler()" /> | ||
|
||
<!-- Inline function: --> | ||
<button v-on:click="() => handler()" /> | ||
``` | ||
|
||
<eslint-code-block fix :rules="{'vue/v-on-handler-style': ['error']}"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<button v-on:click="handler" /> | ||
|
||
<!-- ✗ BAD --> | ||
<button v-on:click="handler()" /> | ||
<button v-on:click="() => handler()" /> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
```json | ||
{ | ||
"vue/v-on-handler-style": ["error", | ||
["method", "inline-function"], // ["method", "inline-function"] | ["method", "inline"] | "inline-function" | "inline" | ||
{ | ||
"ignoreIncludesComment": false | ||
} | ||
] | ||
} | ||
``` | ||
|
||
- First option ... Specifies the name of an allowed style. Default is `["method", "inline-function"]`. | ||
- `["method", "inline-function"]` ... Allow handlers by method binding. e.g. `v-on:click="handler"`. Allow inline functions where method handlers cannot be used. e.g. `v-on:click="() => handler(listItem)"`. | ||
- `["method", "inline"]` ... Allow handlers by method binding. e.g. `v-on:click="handler"`. Allow inline handlers where method handlers cannot be used. e.g. `v-on:click="handler(listItem)"`. | ||
- `"inline-function"` ... Allow inline functions. e.g. `v-on:click="() => handler()"` | ||
- `"inline"` ... Allow inline handlers. e.g. `v-on:click="handler()"` | ||
- Second option | ||
- `ignoreIncludesComment` ... If `true`, do not report inline handlers or inline functions containing comments, even if the preferred style is `"method"`. Default is `false`. | ||
|
||
### `["method", "inline-function"]` (Default) | ||
|
||
<eslint-code-block fix :rules="{'vue/v-on-handler-style': ['error', ['method', 'inline-function']]}"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<button v-on:click="handler" /> | ||
<template v-for="e in list"> | ||
<button v-on:click="e" /> | ||
<button v-on:click="() => handler(e)" /> | ||
</template> | ||
|
||
<!-- ✗ BAD --> | ||
<button v-on:click="handler()" /> | ||
<button v-on:click="count++" /> | ||
<button v-on:click="() => handler()" /> | ||
<button v-on:click="() => count++" /> | ||
<button v-on:click="(a, b) => handler(a, b)" /> | ||
<template v-for="e in list"> | ||
<button v-on:click="e()" /> | ||
<button v-on:click="() => e()" /> | ||
<button v-on:click="handler(e)" /> | ||
</template> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
### `["method", "inline"]` | ||
|
||
<eslint-code-block fix :rules="{'vue/v-on-handler-style': ['error', ['method', 'inline']]}"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<button v-on:click="handler" /> | ||
<template v-for="e in list"> | ||
<button v-on:click="e" /> | ||
<button v-on:click="handler(e)" /> | ||
</template> | ||
|
||
<!-- ✗ BAD --> | ||
<button v-on:click="handler()" /> | ||
<button v-on:click="count++" /> | ||
<button v-on:click="() => handler()" /> | ||
<button v-on:click="() => count++" /> | ||
<button v-on:click="(a, b) => handler(a, b)" /> | ||
<template v-for="e in list"> | ||
<button v-on:click="e()" /> | ||
<button v-on:click="() => e()" /> | ||
<button v-on:click="() => handler(e)" /> | ||
</template> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
### `["inline-function"]` | ||
|
||
<eslint-code-block fix :rules="{'vue/v-on-handler-style': ['error', ['inline-function']]}"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<button v-on:click="() => handler()" /> | ||
<button v-on:click="() => count++" /> | ||
|
||
<!-- ✗ BAD --> | ||
<button v-on:click="handler" /> | ||
<button v-on:click="handler()" /> | ||
<button v-on:click="handler($event)" /> | ||
<button v-on:click="count++" /> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
### `["inline"]` | ||
|
||
<eslint-code-block fix :rules="{'vue/v-on-handler-style': ['error', ['inline']]}"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<button v-on:click="handler()" /> | ||
<button v-on:click="handler($event)" /> | ||
<button v-on:click="count++" /> | ||
|
||
<!-- ✗ BAD --> | ||
<button v-on:click="handler" /> | ||
<button v-on:click="() => handler()" /> | ||
<button v-on:click="(foo) => handler(foo)" /> | ||
<button v-on:click="(foo, bar) => handler(foo, bar)" /> | ||
<button v-on:click="() => count++" /> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
### `["method", "inline-function"], { "ignoreIncludesComment": true }` | ||
|
||
<eslint-code-block fix :rules="{'vue/v-on-handler-style': ['error', ['method', 'inline-function'], {ignoreIncludesComment: true}]}"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<button v-on:click="handler" /> | ||
<button v-on:click="() => handler() /* comment */" /> | ||
|
||
<!-- ✗ BAD --> | ||
<button v-on:click="handler()" /> | ||
<button v-on:click="() => handler()" /> | ||
<button v-on:click="handler() /* comment */" /> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
### `["method", "inline"], { "ignoreIncludesComment": true }` | ||
|
||
<eslint-code-block fix :rules="{'vue/v-on-handler-style': ['error', ['method', 'inline'], {ignoreIncludesComment: true}]}"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<button v-on:click="handler" /> | ||
<button v-on:click="handler() /* comment */" /> | ||
|
||
<!-- ✗ BAD --> | ||
<button v-on:click="handler()" /> | ||
<button v-on:click="() => handler()" /> | ||
<button v-on:click="() => handler() /* comment */" /> | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
ota-meshi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
## :couple: Related Rules | ||
|
||
- [vue/v-on-style](./v-on-style.md) | ||
- [vue/v-on-event-hyphenation](./v-on-event-hyphenation.md) | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## :books: Further Reading | ||
|
||
- [Guide - Inline Handlers] | ||
- [Guide - Method Handlers] | ||
|
||
[Guide - Inline Handlers]: https://vuejs.org/guide/essentials/event-handling.html#inline-handlers | ||
[Guide - Method Handlers]: https://vuejs.org/guide/essentials/event-handling.html#method-handlers | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/v-on-handler-style.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/v-on-handler-style.js) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.