|
| 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 `defineProps` 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-props.html' |
| 19 | + }, |
| 20 | + fixable: null, |
| 21 | + schema: [], |
| 22 | + messages: { |
| 23 | + hasTypeAndArg: |
| 24 | + '`defineProps` has both a type-only props and an argument.', |
| 25 | + referencingLocally: |
| 26 | + '`defineProps` are referencing locally declared variables.', |
| 27 | + multiple: '`defineProps` has been called multiple times.', |
| 28 | + notDefined: 'Props are not defined.', |
| 29 | + definedInBoth: |
| 30 | + 'Props are defined in both `defineProps` and `export default {}`.' |
| 31 | + } |
| 32 | + }, |
| 33 | + /** @param {RuleContext} context */ |
| 34 | + create(context) { |
| 35 | + const scriptSetup = utils.getScriptSetupElement(context) |
| 36 | + if (!scriptSetup) { |
| 37 | + return {} |
| 38 | + } |
| 39 | + |
| 40 | + /** @type {Set<Expression | SpreadElement>} */ |
| 41 | + const propsDefExpressions = new Set() |
| 42 | + let hasDefaultExport = false |
| 43 | + /** @type {CallExpression[]} */ |
| 44 | + const definePropsNodes = [] |
| 45 | + /** @type {CallExpression | null} */ |
| 46 | + let emptyDefineProps = null |
| 47 | + |
| 48 | + return utils.compositingVisitors( |
| 49 | + utils.defineScriptSetupVisitor(context, { |
| 50 | + onDefinePropsEnter(node) { |
| 51 | + definePropsNodes.push(node) |
| 52 | + |
| 53 | + if (node.arguments.length >= 1) { |
| 54 | + if (node.typeParameters && node.typeParameters.params.length >= 1) { |
| 55 | + // `defineProps` has both a literal type and an argument. |
| 56 | + context.report({ |
| 57 | + node, |
| 58 | + messageId: 'hasTypeAndArg' |
| 59 | + }) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + propsDefExpressions.add(node.arguments[0]) |
| 64 | + } else { |
| 65 | + if ( |
| 66 | + !node.typeParameters || |
| 67 | + node.typeParameters.params.length === 0 |
| 68 | + ) { |
| 69 | + emptyDefineProps = node |
| 70 | + } |
| 71 | + } |
| 72 | + }, |
| 73 | + Identifier(node) { |
| 74 | + for (const def of propsDefExpressions) { |
| 75 | + if (utils.inRange(def.range, node)) { |
| 76 | + const variable = findVariable(context.getScope(), node) |
| 77 | + if ( |
| 78 | + variable && |
| 79 | + variable.references.some((ref) => ref.identifier === node) |
| 80 | + ) { |
| 81 | + if ( |
| 82 | + variable.defs.length && |
| 83 | + variable.defs.every((def) => |
| 84 | + utils.inRange(scriptSetup.range, def.name) |
| 85 | + ) |
| 86 | + ) { |
| 87 | + //`defineProps` are referencing locally declared variables. |
| 88 | + context.report({ |
| 89 | + node, |
| 90 | + messageId: 'referencingLocally' |
| 91 | + }) |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + }), |
| 98 | + utils.defineVueVisitor(context, { |
| 99 | + onVueObjectEnter(node, { type }) { |
| 100 | + if (type !== 'export' || utils.inRange(scriptSetup.range, node)) { |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + hasDefaultExport = Boolean(utils.findProperty(node, 'props')) |
| 105 | + } |
| 106 | + }), |
| 107 | + { |
| 108 | + 'Program:exit'() { |
| 109 | + if (!definePropsNodes.length) { |
| 110 | + return |
| 111 | + } |
| 112 | + if (definePropsNodes.length > 1) { |
| 113 | + // `defineProps` has been called multiple times. |
| 114 | + for (const node of definePropsNodes) { |
| 115 | + context.report({ |
| 116 | + node, |
| 117 | + messageId: 'multiple' |
| 118 | + }) |
| 119 | + } |
| 120 | + return |
| 121 | + } |
| 122 | + if (emptyDefineProps) { |
| 123 | + if (!hasDefaultExport) { |
| 124 | + // Props are not defined. |
| 125 | + context.report({ |
| 126 | + node: emptyDefineProps, |
| 127 | + messageId: 'notDefined' |
| 128 | + }) |
| 129 | + } |
| 130 | + } else { |
| 131 | + if (hasDefaultExport) { |
| 132 | + // Props are defined in both `defineProps` and `export default {}`. |
| 133 | + for (const node of definePropsNodes) { |
| 134 | + context.report({ |
| 135 | + node, |
| 136 | + messageId: 'definedInBoth' |
| 137 | + }) |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + ) |
| 144 | + } |
| 145 | +} |
0 commit comments