|
| 1 | +/** |
| 2 | + * @author Yosuke Ota <https://github.com/ota-meshi> |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const { findVariable } = require('eslint-utils') |
| 8 | +const utils = require('../utils') |
| 9 | + |
| 10 | +module.exports = { |
| 11 | + meta: { |
| 12 | + type: 'problem', |
| 13 | + docs: { |
| 14 | + description: 'enforce valid `defineEmits` compiler macro', |
| 15 | + // TODO Switch in the major version. |
| 16 | + // categories: ['vue3-essential'], |
| 17 | + categories: undefined, |
| 18 | + url: 'https://eslint.vuejs.org/rules/valid-define-emits.html' |
| 19 | + }, |
| 20 | + fixable: null, |
| 21 | + schema: [], |
| 22 | + messages: { |
| 23 | + hasTypeAndArg: '`defineEmits` has both a type-only emit and an argument.', |
| 24 | + referencingLocally: |
| 25 | + '`defineEmits` are referencing locally declared variables.', |
| 26 | + multiple: '`defineEmits` has been called multiple times.', |
| 27 | + notDefined: 'Custom events are not defined.', |
| 28 | + definedInBoth: |
| 29 | + 'Custom events are defined in both `defineEmits` and `export default {}`.' |
| 30 | + } |
| 31 | + }, |
| 32 | + /** @param {RuleContext} context */ |
| 33 | + create(context) { |
| 34 | + const scriptSetup = utils.getScriptSetupElement(context) |
| 35 | + if (!scriptSetup) { |
| 36 | + return {} |
| 37 | + } |
| 38 | + |
| 39 | + /** @type {Set<Expression | SpreadElement>} */ |
| 40 | + const emitsDefExpressions = new Set() |
| 41 | + let hasDefaultExport = false |
| 42 | + /** @type {CallExpression[]} */ |
| 43 | + const defineEmitsNodes = [] |
| 44 | + /** @type {CallExpression | null} */ |
| 45 | + let emptyDefineEmits = null |
| 46 | + |
| 47 | + return utils.compositingVisitors( |
| 48 | + utils.defineScriptSetupVisitor(context, { |
| 49 | + onDefineEmitsEnter(node) { |
| 50 | + defineEmitsNodes.push(node) |
| 51 | + |
| 52 | + if (node.arguments.length >= 1) { |
| 53 | + if (node.typeParameters && node.typeParameters.params.length >= 1) { |
| 54 | + // `defineEmits` has both a literal type and an argument. |
| 55 | + context.report({ |
| 56 | + node, |
| 57 | + messageId: 'hasTypeAndArg' |
| 58 | + }) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + emitsDefExpressions.add(node.arguments[0]) |
| 63 | + } else { |
| 64 | + if ( |
| 65 | + !node.typeParameters || |
| 66 | + node.typeParameters.params.length === 0 |
| 67 | + ) { |
| 68 | + emptyDefineEmits = node |
| 69 | + } |
| 70 | + } |
| 71 | + }, |
| 72 | + Identifier(node) { |
| 73 | + for (const def of emitsDefExpressions) { |
| 74 | + if (utils.inRange(def.range, node)) { |
| 75 | + const variable = findVariable(context.getScope(), node) |
| 76 | + if ( |
| 77 | + variable && |
| 78 | + variable.references.some((ref) => ref.identifier === node) |
| 79 | + ) { |
| 80 | + if ( |
| 81 | + variable.defs.length && |
| 82 | + variable.defs.every((def) => |
| 83 | + utils.inRange(scriptSetup.range, def.name) |
| 84 | + ) |
| 85 | + ) { |
| 86 | + //`defineEmits` are referencing locally declared variables. |
| 87 | + context.report({ |
| 88 | + node, |
| 89 | + messageId: 'referencingLocally' |
| 90 | + }) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + }), |
| 97 | + utils.defineVueVisitor(context, { |
| 98 | + onVueObjectEnter(node, { type }) { |
| 99 | + if (type !== 'export' || utils.inRange(scriptSetup.range, node)) { |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + hasDefaultExport = Boolean(utils.findProperty(node, 'emits')) |
| 104 | + } |
| 105 | + }), |
| 106 | + { |
| 107 | + 'Program:exit'() { |
| 108 | + if (!defineEmitsNodes.length) { |
| 109 | + return |
| 110 | + } |
| 111 | + if (defineEmitsNodes.length > 1) { |
| 112 | + // `defineEmits` has been called multiple times. |
| 113 | + for (const node of defineEmitsNodes) { |
| 114 | + context.report({ |
| 115 | + node, |
| 116 | + messageId: 'multiple' |
| 117 | + }) |
| 118 | + } |
| 119 | + return |
| 120 | + } |
| 121 | + if (emptyDefineEmits) { |
| 122 | + if (!hasDefaultExport) { |
| 123 | + // Custom events are not defined. |
| 124 | + context.report({ |
| 125 | + node: emptyDefineEmits, |
| 126 | + messageId: 'notDefined' |
| 127 | + }) |
| 128 | + } |
| 129 | + } else { |
| 130 | + if (hasDefaultExport) { |
| 131 | + // Custom events are defined in both `defineEmits` and `export default {}`. |
| 132 | + for (const node of defineEmitsNodes) { |
| 133 | + context.report({ |
| 134 | + node, |
| 135 | + messageId: 'definedInBoth' |
| 136 | + }) |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + ) |
| 143 | + } |
| 144 | +} |
0 commit comments