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

feat(no-shadow-native-events): initial implementation #2558

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
7b640fe
initial version
JoCa96 Sep 16, 2024
8ad0f4b
added invalid tests
JoCa96 Sep 16, 2024
fe3615d
added valid tests cases
JoCa96 Sep 16, 2024
892f277
clean up
JoCa96 Sep 16, 2024
391a4bb
add docs
JoCa96 Sep 16, 2024
654332a
update related docs
JoCa96 Sep 16, 2024
300c138
add missing test fixture
JoCa96 Sep 16, 2024
c3e68ca
fix link
JoCa96 Sep 16, 2024
218a577
fix missing typedef for NameWithLoc
JoCa96 Sep 16, 2024
4e55123
lint issue: remove todo
JoCa96 Sep 16, 2024
ddfb904
fix lint issue: "space inside link text"
JoCa96 Sep 16, 2024
81ddf6a
remove unused options
JoCa96 Sep 16, 2024
7d47fac
replace array with Set
JoCa96 Sep 16, 2024
82123a3
add explanatory comments
JoCa96 Sep 16, 2024
c72133f
initial version
JoCa96 Sep 16, 2024
1c40d3b
added invalid tests
JoCa96 Sep 16, 2024
8e2cfd7
added valid tests cases
JoCa96 Sep 16, 2024
9d0e1c2
clean up
JoCa96 Sep 16, 2024
84b7b78
add docs
JoCa96 Sep 16, 2024
7bdf176
update related docs
JoCa96 Sep 16, 2024
b10bae2
add missing test fixture
JoCa96 Sep 16, 2024
2dc6949
fix link
JoCa96 Sep 16, 2024
9a8ccd7
fix missing typedef for NameWithLoc
JoCa96 Sep 16, 2024
dc011e0
lint issue: remove todo
JoCa96 Sep 16, 2024
8c71413
fix lint issue: "space inside link text"
JoCa96 Sep 16, 2024
f3ffb7d
remove unused options
JoCa96 Sep 16, 2024
066689b
replace array with Set
JoCa96 Sep 16, 2024
7941f9c
add explanatory comments
JoCa96 Sep 16, 2024
3901c99
review: execute "npm run update"
JoCa96 Sep 19, 2024
44f5eb4
Merge branch 'joca96/feat-no-shadow-native' of https://github.com/JoC…
JoCa96 Sep 19, 2024
b62c161
Update docs/rules/no-shadow-native-events.md
JoCa96 Sep 19, 2024
4feb834
Update docs/rules/no-shadow-native-events.md
JoCa96 Sep 19, 2024
3e9e020
review: revert accidental packageManager field in package.json
JoCa96 Sep 20, 2024
df3e87e
Merge branch 'joca96/feat-no-shadow-native' of https://github.com/JoC…
JoCa96 Sep 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ For example:
| [vue/no-restricted-v-on](./no-restricted-v-on.md) | disallow specific argument in `v-on` | | :hammer: |
| [vue/no-root-v-if](./no-root-v-if.md) | disallow `v-if` directives on root element | | :hammer: |
| [vue/no-setup-props-reactivity-loss](./no-setup-props-reactivity-loss.md) | disallow usages that lose the reactivity of `props` passed to `setup` | | :hammer: |
| [vue/no-shadow-native-events](./no-shadow-native-events.md) | disallow the use of event names that collide with native web event names | | :warning: |
| [vue/no-static-inline-styles](./no-static-inline-styles.md) | disallow static inline `style` attributes | | :hammer: |
| [vue/no-template-target-blank](./no-template-target-blank.md) | disallow target="_blank" attribute without rel="noopener noreferrer" | :bulb: | :warning: |
| [vue/no-this-in-before-route-enter](./no-this-in-before-route-enter.md) | disallow `this` usage in a `beforeRouteEnter` method | | :warning: |
Expand Down
57 changes: 57 additions & 0 deletions docs/rules/no-shadow-native-events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/no-shadow-native-events
description: disallow the use of event names that collide with native web event names
---

# vue/no-shadow-native-events

> disallow the use of event names that collide with native web event names

- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> _**This rule has not been released yet.**_ </badge>

## :book: Rule Details

This rule reports emits that shadow native HTML events.

Using native event names for emits can lead to incorrect assumptions about an emit and cause confusion. This is caused by Vue emits behaving differently from native events. E.g. :

- The payload of an emit can be chosen arbitrarily
- Vue emits do not bubble, while most native events do
- [Event modifiers](https://vuejs.org/guide/essentials/event-handling.html#event-modifiers) only work on HTML events or when the original event is re-emitted as emit payload.
- When the native event is re-emitted, the `event.target` might not match the actual event-listeners location.

The rule is mostly aimed at developers of component libraries.

<eslint-code-block :rules="{'vue/no-shadow-native-events': ['error']}">

```vue
<template>
<!-- ✓ GOOD -->
<button @click="$emit('close')">Close</button>
<!-- ✗ BAD -->
<button @click="$emit('click')">Close</button>
</template>
```

</eslint-code-block>

## :wrench: Options

Nothing.

## :couple: Related Rules

- [vue/no-unused-emit-declarations](./no-unused-emit-declarations.md)
- [vue/require-explicit-emits](./require-explicit-emits.md)

## :books: Further Reading

- [Components In-Depth - Events / Component Events](https://vuejs.org/guide/components/events.html#event-arguments)
- [Vue RFCs - 0030-emits-option](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0030-emits-option.md)

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-shadow-native-events.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-shadow-native-events.js)
3 changes: 2 additions & 1 deletion docs/rules/require-explicit-emits.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,11 @@ export default {

- [vue/no-unused-emit-declarations](./no-unused-emit-declarations.md)
- [vue/require-explicit-slots](./require-explicit-slots.md)
- [vue/no-shadow-native-events](./no-shadow-native-events.md)

## :books: Further Reading

- [Guide - Custom Events / Defining Custom Events](https://v3.vuejs.org/guide/component-custom-events.html#defining-custom-events)
- [Components In-Depth - Events / Component Events](https://vuejs.org/guide/components/events.html#event-arguments)
- [Vue RFCs - 0030-emits-option](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0030-emits-option.md)

## :rocket: Version
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ const plugin = {
'no-root-v-if': require('./rules/no-root-v-if'),
'no-setup-props-destructure': require('./rules/no-setup-props-destructure'),
'no-setup-props-reactivity-loss': require('./rules/no-setup-props-reactivity-loss'),
'no-shadow-native-events': require('./rules/no-shadow-native-events'),
'no-shared-component-data': require('./rules/no-shared-component-data'),
'no-side-effects-in-computed-properties': require('./rules/no-side-effects-in-computed-properties'),
'no-spaces-around-equal-signs-in-attribute': require('./rules/no-spaces-around-equal-signs-in-attribute'),
Expand Down
Loading