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

added options for events that ignore custom-event-name-casing #1321

Merged
merged 7 commits into from Oct 18, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 41 additions & 1 deletion docs/rules/custom-event-name-casing.md
Expand Up @@ -49,7 +49,47 @@ export default {

## :wrench: Options

Nothing.

```json
{
"vue/custom-event-name-casing": ["error", {
"ignores": []
}]
}
```
- `ignores` (`string[]`) ... The event names to ignore. Sets the event name to allow. For example, custom event names, Vue components event with special name, or Vue library component event name. You can set the regexp by writing it like `"/^name/"` or `click:row` or `fooBar`.

### `"ignores": ["fooBar", "/^[a-z]+(?:-[a-z]+)*:[a-z]+(?:-[a-z]+)*$/u"]`

<eslint-code-block :rules="{'vue/custom-event-name-casing': ['error', {ignores:'/^[a-z]+(?:-[a-z]+)*:[a-z]+(?:-[a-z]+)*$/u'}]}">

```vue
<template>
<!-- ✓ GOOD -->
<button @click="$emit('click:row')" />
<button @click="$emit('fooBar')" />

<!-- ✗ BAD -->
<button @click="$emit('myEvent')" />
</template>
<script>
export default {
methods: {
onClick () {
/* ✓ GOOD */
this.$emit('click:row')
this.$emit('fooBar')

/* ✗ BAD */
this.$emit('myEvent')
}
}
}
</script>
```

</eslint-code-block>

devTeaa marked this conversation as resolved.
Show resolved Hide resolved

## :books: Further Reading

Expand Down
21 changes: 19 additions & 2 deletions lib/rules/custom-event-name-casing.js
Expand Up @@ -11,6 +11,7 @@
const { findVariable } = require('eslint-utils')
const utils = require('../utils')
const { isKebabCase } = require('../utils/casing')
const { toRegExp } = require('../utils/regexp')

// ------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -72,21 +73,37 @@ module.exports = {
url: 'https://eslint.vuejs.org/rules/custom-event-name-casing.html'
},
fixable: null,
schema: [],
schema: [
{
type: 'object',
properties: {
ignores: {
type: 'array',
items: { type: 'string' },
uniqueItems: true,
additionalItems: false
}
},
additionalProperties: false
}
],
messages: {
unexpected: "Custom event name '{{name}}' must be kebab-case."
}
},
/** @param {RuleContext} context */
create(context) {
const setupContexts = new Map()
const options = context.options[0] || {}
/** @type {RegExp[]} */
const ignores = (options.ignores || []).map(toRegExp)

/**
* @param { Literal & { value: string } } nameLiteralNode
*/
function verify(nameLiteralNode) {
const name = nameLiteralNode.value
if (isValidEventName(name)) {
if (ignores.some((re) => re.test(name)) || isValidEventName(name)) {
return
}
context.report({
Expand Down
122 changes: 122 additions & 0 deletions tests/lib/rules/custom-event-name-casing.js
Expand Up @@ -166,6 +166,66 @@ tester.run('custom-event-name-casing', rule, {
}
</script>
`
},
{
filename: 'test.vue',
code: `
<template>
<input
@click="$emit('fooBar')">
</template>
<script>
export default {
setup(props, context) {
return {
onInput(value) {
context.emit('barBaz')
}
}
},
methods: {
onClick() {
this.$emit('bazQux')
}
}
}
</script>
`,
options: [{ ignores: ['fooBar', 'barBaz', 'bazQux'] }]
},
{
filename: 'test.vue',
code: `
<template>
<input
@click="$emit('input:update')">
<input
@click="$emit('search:update')">
<input
@click="$emit('click:row')">
</template>
<script>
export default {
setup(props, context) {
return {
onInput(value) {
context.emit('input:update')
context.emit('search:update')
context.emit('click:row')
}
}
},
methods: {
onClick() {
this.$emit('input:update')
this.$emit('search:update')
this.$emit('click:row')
}
}
}
</script>
`,
options: [{ ignores: ['/^[a-z]+(?:-[a-z]+)*:[a-z]+(?:-[a-z]+)*$/u'] }]
}
],
invalid: [
Expand Down Expand Up @@ -276,6 +336,68 @@ tester.run('custom-event-name-casing', rule, {
"Custom event name 'barBaz' must be kebab-case.",
"Custom event name 'bazQux' must be kebab-case."
]
},
{
filename: 'test.vue',
code: `
<template>
<input
@click="$emit('input/update')">
</template>
<script>
export default {
setup(props, context) {
return {
onInput(value) {
context.emit('search/update')
}
}
},
methods: {
onClick() {
this.$emit('click/row')
}
}
}
</script>
`,
options: [{ ignores: ['/^[a-z]+(?:-[a-z]+)*:[a-z]+(?:-[a-z]+)*$/u'] }],
errors: [
"Custom event name 'input/update' must be kebab-case.",
"Custom event name 'search/update' must be kebab-case.",
"Custom event name 'click/row' must be kebab-case."
]
},
{
filename: 'test.vue',
code: `
<template>
<input
@click="$emit('input/update')">
</template>
<script>
export default {
setup(props, context) {
return {
onInput(value) {
context.emit('search/update')
}
}
},
methods: {
onClick() {
this.$emit('click/row')
}
}
}
</script>
`,
options: [{ ignores: ['input:update', 'search:update', 'click:row'] }],
errors: [
"Custom event name 'input/update' must be kebab-case.",
"Custom event name 'search/update' must be kebab-case.",
"Custom event name 'click/row' must be kebab-case."
]
}
]
})