Skip to content

Add vue/no-restricted-custom-event rule #1377

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 1 commit into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ For example:
| [vue/no-potential-component-option-typo](./no-potential-component-option-typo.md) | disallow a potential typo in your component property | |
| [vue/no-reserved-component-names](./no-reserved-component-names.md) | disallow the use of reserved names in component definitions | |
| [vue/no-restricted-component-options](./no-restricted-component-options.md) | disallow specific component option | |
| [vue/no-restricted-custom-event](./no-restricted-custom-event.md) | disallow specific custom event | |
| [vue/no-restricted-props](./no-restricted-props.md) | disallow specific props | |
| [vue/no-restricted-static-attribute](./no-restricted-static-attribute.md) | disallow specific attribute | |
| [vue/no-restricted-v-bind](./no-restricted-v-bind.md) | disallow specific argument in `v-bind` | |
Expand Down
97 changes: 97 additions & 0 deletions docs/rules/no-restricted-custom-event.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/no-restricted-custom-event
description: disallow specific custom event
---
# vue/no-restricted-custom-event
> disallow specific custom event

## :book: Rule Details

This rule allows you to specify custom event that you don't want to use in your application.

## :wrench: Options

This rule takes a list of strings, where each string is a custom event name or pattern to be restricted:

```json
{
"vue/no-restricted-custom-event": ["error", "input", "/^forbidden/"]
}
```

<eslint-code-block :rules="{'vue/no-restricted-custom-event': ['error', 'input', '/^forbidden/']}">

```vue
<template>
<!-- ✗ BAD -->
<input @input="$emit('input', $event.target.value)">
<!-- ✗ GOOD -->
<input @input="$emit('update:value', $event.target.value)">
</template>
<script>
export default {
methods: {
handleChangeValue(newValue) {
/* ✗ BAD */
this.$emit('input', newValue)
this.$emit('forbiddenEvent')

/* ✓ GOOD */
this.$emit('foo')
this.$emit('bar')
}
}
}
</script>
```

</eslint-code-block>


Alternatively, the rule also accepts objects.

```json
{
"vue/no-restricted-custom-event": ["error",
{
"event": "input",
"message": "If you intend a prop for v-model, it should be 'update:modelValue' in Vue 3.",
"suggest": "update:modelValue"
},
]
}
```

The following properties can be specified for the object.

- `event` ... Specify the event name or pattern.
- `message` ... Specify an optional custom message.
- `suggest` ... Specify an optional name to suggest changes.

<eslint-code-block :rules="{'vue/no-restricted-custom-event': ['error', { event: 'input', message: 'If you intend a prop for v-model, it should be \'update:modelValue\' in Vue 3.', suggest: 'update:modelValue'}]}">

```vue
<template>
<!-- ✗ BAD -->
<input @input="$emit('input', $event.target.value)">
</template>
<script>
export default {
methods: {
handleChangeValue(newValue) {
/* ✗ BAD */
this.$emit('input', newValue)
}
}
}
</script>
```

</eslint-code-block>

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-restricted-custom-event.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-restricted-custom-event.js)
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ module.exports = {
'no-reserved-component-names': require('./rules/no-reserved-component-names'),
'no-reserved-keys': require('./rules/no-reserved-keys'),
'no-restricted-component-options': require('./rules/no-restricted-component-options'),
'no-restricted-custom-event': require('./rules/no-restricted-custom-event'),
'no-restricted-props': require('./rules/no-restricted-props'),
'no-restricted-static-attribute': require('./rules/no-restricted-static-attribute'),
'no-restricted-syntax': require('./rules/no-restricted-syntax'),
Expand Down
Loading