-
-
Notifications
You must be signed in to change notification settings - Fork 669
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
vue/require-emit-validator
rule (#1487)
* Add `vue/require-emit-types` rule * remove from categories * allow identifiers * add suggestion for skipped validation * rename rule
- Loading branch information
Showing
4 changed files
with
482 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,61 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/require-emit-validator | ||
description: require type definitions in emits | ||
--- | ||
# vue/require-emit-validator | ||
|
||
> require type definitions in emits | ||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
- :gear: This rule is included in . | ||
|
||
## :book: Rule Details | ||
|
||
This rule enforces that a `emits` statement contains type definition. | ||
|
||
Declaring `emits` with types can bring better maintenance. | ||
Even if using with TypeScript, this can provide better type inference when annotating parameters with types. | ||
|
||
<eslint-code-block :rules="{'vue/require-emit-validator': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
/* ✓ GOOD */ | ||
Vue.component('foo', { | ||
emits: { | ||
// Emit with arguments | ||
foo: (payload) => { /* validate payload */ }, | ||
// Emit without parameters | ||
bar: () => true, | ||
} | ||
}) | ||
/* ✗ BAD */ | ||
Vue.component('bar', { | ||
emits: ['foo'] | ||
}) | ||
Vue.component('baz', { | ||
emits: { | ||
foo: null, | ||
} | ||
}) | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :books: Further Reading | ||
|
||
- [API Reference](https://v3.vuejs.org/api/options-data.html#emits) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/require-emit-validator.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/require-emit-validator.js) |
This file contains 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 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,90 @@ | ||
/** | ||
* @fileoverview Emit definitions should be detailed | ||
* @author Pig Fang | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
/** | ||
* @typedef {import('../utils').ComponentArrayEmit} ComponentArrayEmit | ||
* @typedef {import('../utils').ComponentObjectEmit} ComponentObjectEmit | ||
*/ | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'require type definitions in emits', | ||
categories: [], | ||
url: 'https://eslint.vuejs.org/rules/require-emit-validator.html' | ||
}, | ||
fixable: null, | ||
messages: { | ||
missing: 'Emit "{{name}}" should define at least its validator function.', | ||
skipped: | ||
'Emit "{{name}}" should not skip validation, or you may define a validator function with no parameters.', | ||
emptyValidation: 'Replace with a validator function with no parameters.' | ||
}, | ||
schema: [] | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
// ---------------------------------------------------------------------- | ||
// Helpers | ||
// ---------------------------------------------------------------------- | ||
|
||
/** | ||
* @param {ComponentArrayEmit|ComponentObjectEmit} emit | ||
*/ | ||
function checker({ value, node, emitName }) { | ||
const hasType = | ||
!!value && | ||
(value.type === 'ArrowFunctionExpression' || | ||
value.type === 'FunctionExpression' || | ||
// validator may from outer scope | ||
value.type === 'Identifier') | ||
|
||
if (!hasType) { | ||
const name = | ||
emitName || | ||
(node.type === 'Identifier' && node.name) || | ||
'Unknown emit' | ||
|
||
if (value && value.type === 'Literal' && value.value === null) { | ||
context.report({ | ||
node, | ||
messageId: 'skipped', | ||
data: { name }, | ||
suggest: [ | ||
{ | ||
messageId: 'emptyValidation', | ||
fix: (fixer) => fixer.replaceText(value, '() => true') | ||
} | ||
] | ||
}) | ||
|
||
return | ||
} | ||
|
||
context.report({ | ||
node, | ||
messageId: 'missing', | ||
data: { name } | ||
}) | ||
} | ||
} | ||
|
||
// ---------------------------------------------------------------------- | ||
// Public | ||
// ---------------------------------------------------------------------- | ||
|
||
return utils.executeOnVue(context, (obj) => { | ||
utils.getComponentEmits(obj).forEach(checker) | ||
}) | ||
} | ||
} |
Oops, something went wrong.