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