/
valid-define-props.js
147 lines (140 loc) · 4.38 KB
/
valid-define-props.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* @author Yosuke Ota <https://github.com/ota-meshi>
* See LICENSE file in root directory for full license.
*/
'use strict'
const { findVariable } = require('eslint-utils')
const utils = require('../utils')
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'enforce valid `defineProps` compiler macro',
// TODO Switch in the major version.
// categories: ['vue3-essential'],
categories: undefined,
url: 'https://eslint.vuejs.org/rules/valid-define-props.html'
},
fixable: null,
schema: [],
messages: {
hasTypeAndArg:
'`defineProps` has both a type-only props and an argument.',
referencingLocally:
'`defineProps` are referencing locally declared variables.',
multiple: '`defineProps` has been called multiple times.',
notDefined: 'Props are not defined.',
definedInBoth:
'Props are defined in both `defineProps` and `export default {}`.'
}
},
/** @param {RuleContext} context */
create(context) {
const scriptSetup = utils.getScriptSetupElement(context)
if (!scriptSetup) {
return {}
}
/** @type {Set<Expression | SpreadElement>} */
const propsDefExpressions = new Set()
let hasDefaultExport = false
/** @type {CallExpression[]} */
const definePropsNodes = []
/** @type {CallExpression | null} */
let emptyDefineProps = null
return utils.compositingVisitors(
utils.defineScriptSetupVisitor(context, {
onDefinePropsEnter(node) {
definePropsNodes.push(node)
if (node.arguments.length >= 1) {
if (node.typeParameters && node.typeParameters.params.length >= 1) {
// `defineProps` has both a literal type and an argument.
context.report({
node,
messageId: 'hasTypeAndArg'
})
return
}
propsDefExpressions.add(node.arguments[0])
} else {
if (
!node.typeParameters ||
node.typeParameters.params.length === 0
) {
emptyDefineProps = node
}
}
},
Identifier(node) {
for (const defineProps of propsDefExpressions) {
if (utils.inRange(defineProps.range, node)) {
const variable = findVariable(context.getScope(), node)
if (
variable &&
variable.references.some((ref) => ref.identifier === node)
) {
if (
variable.defs.length &&
variable.defs.every(
(def) =>
utils.inRange(scriptSetup.range, def.name) &&
!utils.inRange(defineProps.range, def.name)
)
) {
//`defineProps` are referencing locally declared variables.
context.report({
node,
messageId: 'referencingLocally'
})
}
}
}
}
}
}),
utils.defineVueVisitor(context, {
onVueObjectEnter(node, { type }) {
if (type !== 'export' || utils.inRange(scriptSetup.range, node)) {
return
}
hasDefaultExport = Boolean(utils.findProperty(node, 'props'))
}
}),
{
'Program:exit'() {
if (!definePropsNodes.length) {
return
}
if (definePropsNodes.length > 1) {
// `defineProps` has been called multiple times.
for (const node of definePropsNodes) {
context.report({
node,
messageId: 'multiple'
})
}
return
}
if (emptyDefineProps) {
if (!hasDefaultExport) {
// Props are not defined.
context.report({
node: emptyDefineProps,
messageId: 'notDefined'
})
}
} else {
if (hasDefaultExport) {
// Props are defined in both `defineProps` and `export default {}`.
for (const node of definePropsNodes) {
context.report({
node,
messageId: 'definedInBoth'
})
}
}
}
}
}
)
}
}