|
| 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-community/eslint-utils') |
| 8 | +const utils = require('../utils') |
| 9 | + |
| 10 | +module.exports = { |
| 11 | + meta: { |
| 12 | + type: 'problem', |
| 13 | + docs: { |
| 14 | + description: 'enforce valid `defineOptions` compiler macro', |
| 15 | + // TODO Switch in the next major version |
| 16 | + // categories: ['vue3-essential', 'essential'], |
| 17 | + categories: undefined, |
| 18 | + url: 'https://eslint.vuejs.org/rules/valid-define-options.html' |
| 19 | + }, |
| 20 | + fixable: null, |
| 21 | + schema: [], |
| 22 | + messages: { |
| 23 | + referencingLocally: |
| 24 | + '`defineOptions` is referencing locally declared variables.', |
| 25 | + multiple: '`defineOptions` has been called multiple times.', |
| 26 | + notDefined: 'Options are not defined.', |
| 27 | + disallowProp: |
| 28 | + '`defineOptions()` cannot be used to declare `{{propName}}`. Use `{{insteadMacro}}()` instead.', |
| 29 | + typeArgs: '`defineOptions()` cannot accept type arguments.' |
| 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 optionsDefExpressions = new Set() |
| 41 | + /** @type {CallExpression[]} */ |
| 42 | + const defineOptionsNodes = [] |
| 43 | + |
| 44 | + return utils.compositingVisitors( |
| 45 | + utils.defineScriptSetupVisitor(context, { |
| 46 | + onDefineOptionsEnter(node) { |
| 47 | + defineOptionsNodes.push(node) |
| 48 | + |
| 49 | + if (node.arguments.length > 0) { |
| 50 | + const define = node.arguments[0] |
| 51 | + if (define.type === 'ObjectExpression') { |
| 52 | + for (const [propName, insteadMacro] of [ |
| 53 | + ['props', 'defineProps'], |
| 54 | + ['emits', 'defineEmits'], |
| 55 | + ['expose', 'defineExpose'], |
| 56 | + ['slots', 'defineSlots'] |
| 57 | + ]) { |
| 58 | + const prop = utils.findProperty(define, propName) |
| 59 | + if (prop) { |
| 60 | + context.report({ |
| 61 | + node, |
| 62 | + messageId: 'disallowProp', |
| 63 | + data: { propName, insteadMacro } |
| 64 | + }) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + optionsDefExpressions.add(node.arguments[0]) |
| 70 | + } else { |
| 71 | + context.report({ |
| 72 | + node, |
| 73 | + messageId: 'notDefined' |
| 74 | + }) |
| 75 | + } |
| 76 | + |
| 77 | + if (node.typeParameters) { |
| 78 | + context.report({ |
| 79 | + node: node.typeParameters, |
| 80 | + messageId: 'typeArgs' |
| 81 | + }) |
| 82 | + } |
| 83 | + }, |
| 84 | + Identifier(node) { |
| 85 | + for (const defineOptions of optionsDefExpressions) { |
| 86 | + if (utils.inRange(defineOptions.range, node)) { |
| 87 | + const variable = findVariable(context.getScope(), node) |
| 88 | + if ( |
| 89 | + variable && |
| 90 | + variable.references.some((ref) => ref.identifier === node) && |
| 91 | + variable.defs.length > 0 && |
| 92 | + variable.defs.every( |
| 93 | + (def) => |
| 94 | + def.type !== 'ImportBinding' && |
| 95 | + utils.inRange(scriptSetup.range, def.name) && |
| 96 | + !utils.inRange(defineOptions.range, def.name) |
| 97 | + ) |
| 98 | + ) { |
| 99 | + if (utils.withinTypeNode(node)) { |
| 100 | + continue |
| 101 | + } |
| 102 | + //`defineOptions` is referencing locally declared variables. |
| 103 | + context.report({ |
| 104 | + node, |
| 105 | + messageId: 'referencingLocally' |
| 106 | + }) |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + }), |
| 112 | + { |
| 113 | + 'Program:exit'() { |
| 114 | + if (defineOptionsNodes.length > 1) { |
| 115 | + // `defineOptions` has been called multiple times. |
| 116 | + for (const node of defineOptionsNodes) { |
| 117 | + context.report({ |
| 118 | + node, |
| 119 | + messageId: 'multiple' |
| 120 | + }) |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + ) |
| 126 | + } |
| 127 | +} |
0 commit comments