|
| 1 | +/** |
| 2 | + * @fileoverview Emit definitions should be detailed |
| 3 | + * @author Pig Fang |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const utils = require('../utils') |
| 8 | + |
| 9 | +/** |
| 10 | + * @typedef {import('../utils').ComponentArrayEmit} ComponentArrayEmit |
| 11 | + * @typedef {import('../utils').ComponentObjectEmit} ComponentObjectEmit |
| 12 | + */ |
| 13 | + |
| 14 | +// ------------------------------------------------------------------------------ |
| 15 | +// Rule Definition |
| 16 | +// ------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +module.exports = { |
| 19 | + meta: { |
| 20 | + type: 'suggestion', |
| 21 | + docs: { |
| 22 | + description: 'require type definitions in emits', |
| 23 | + categories: [], |
| 24 | + url: 'https://eslint.vuejs.org/rules/require-emit-validator.html' |
| 25 | + }, |
| 26 | + fixable: null, |
| 27 | + messages: { |
| 28 | + missing: 'Emit "{{name}}" should define at least its validator function.', |
| 29 | + skipped: |
| 30 | + 'Emit "{{name}}" should not skip validation, or you may define a validator function with no parameters.', |
| 31 | + emptyValidation: 'Replace with a validator function with no parameters.' |
| 32 | + }, |
| 33 | + schema: [] |
| 34 | + }, |
| 35 | + /** @param {RuleContext} context */ |
| 36 | + create(context) { |
| 37 | + // ---------------------------------------------------------------------- |
| 38 | + // Helpers |
| 39 | + // ---------------------------------------------------------------------- |
| 40 | + |
| 41 | + /** |
| 42 | + * @param {ComponentArrayEmit|ComponentObjectEmit} emit |
| 43 | + */ |
| 44 | + function checker({ value, node, emitName }) { |
| 45 | + const hasType = |
| 46 | + !!value && |
| 47 | + (value.type === 'ArrowFunctionExpression' || |
| 48 | + value.type === 'FunctionExpression' || |
| 49 | + // validator may from outer scope |
| 50 | + value.type === 'Identifier') |
| 51 | + |
| 52 | + if (!hasType) { |
| 53 | + const name = |
| 54 | + emitName || |
| 55 | + (node.type === 'Identifier' && node.name) || |
| 56 | + 'Unknown emit' |
| 57 | + |
| 58 | + if (value && value.type === 'Literal' && value.value === null) { |
| 59 | + context.report({ |
| 60 | + node, |
| 61 | + messageId: 'skipped', |
| 62 | + data: { name }, |
| 63 | + suggest: [ |
| 64 | + { |
| 65 | + messageId: 'emptyValidation', |
| 66 | + fix: (fixer) => fixer.replaceText(value, '() => true') |
| 67 | + } |
| 68 | + ] |
| 69 | + }) |
| 70 | + |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + context.report({ |
| 75 | + node, |
| 76 | + messageId: 'missing', |
| 77 | + data: { name } |
| 78 | + }) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // ---------------------------------------------------------------------- |
| 83 | + // Public |
| 84 | + // ---------------------------------------------------------------------- |
| 85 | + |
| 86 | + return utils.executeOnVue(context, (obj) => { |
| 87 | + utils.getComponentEmits(obj).forEach(checker) |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
0 commit comments