-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathno-dupe-keys.js
172 lines (158 loc) · 4.48 KB
/
no-dupe-keys.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* @fileoverview Prevents duplication of field names.
* @author Armano
*/
'use strict'
const { findVariable } = require('@eslint-community/eslint-utils')
const utils = require('../utils')
/**
* @typedef {import('../utils').GroupName} GroupName
* @typedef {import('eslint').Scope.Variable} Variable
* @typedef {import('../utils').ComponentProp} ComponentProp
*/
/** @type {GroupName[]} */
const GROUP_NAMES = ['props', 'computed', 'data', 'methods', 'setup']
/**
* Gets the props pattern node from given `defineProps()` node
* @param {CallExpression} node
* @returns {Pattern|null}
*/
function getPropsPattern(node) {
let target = node
if (
target.parent &&
target.parent.type === 'CallExpression' &&
target.parent.arguments[0] === target &&
target.parent.callee.type === 'Identifier' &&
target.parent.callee.name === 'withDefaults'
) {
target = target.parent
}
if (
!target.parent ||
target.parent.type !== 'VariableDeclarator' ||
target.parent.init !== target
) {
return null
}
return target.parent.id
}
/**
* Checks whether the initialization of the given variable declarator node contains one of the references.
* @param {VariableDeclarator} node
* @param {ESNode[]} references
*/
function isInsideInitializer(node, references) {
const init = node.init
if (!init) {
return false
}
return references.some(
(id) => init.range[0] <= id.range[0] && id.range[1] <= init.range[1]
)
}
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow duplication of field names',
categories: ['vue3-essential', 'essential'],
url: 'https://eslint.vuejs.org/rules/no-dupe-keys.html'
},
fixable: null,
schema: [
{
type: 'object',
properties: {
groups: {
type: 'array'
}
},
additionalProperties: false
}
],
messages: {
duplicateKey:
"Duplicate key '{{name}}'. May cause name collision in script or template tag."
}
},
/** @param {RuleContext} context */
create(context) {
const options = context.options[0] || {}
const groups = new Set([...GROUP_NAMES, ...(options.groups || [])])
return utils.compositingVisitors(
utils.executeOnVue(context, (obj) => {
const properties = utils.iterateProperties(obj, groups)
/** @type {Set<string>} */
const usedNames = new Set()
for (const o of properties) {
if (usedNames.has(o.name)) {
context.report({
node: o.node,
messageId: 'duplicateKey',
data: {
name: o.name
}
})
}
usedNames.add(o.name)
}
}),
utils.defineScriptSetupVisitor(context, {
onDefinePropsEnter(node, props) {
const propsNode = getPropsPattern(node)
const propReferences = [
...(propsNode ? extractReferences(propsNode) : []),
node
]
for (const prop of props) {
if (!prop.propName) continue
const variable = findVariable(context.getScope(), prop.propName)
if (!variable || variable.defs.length === 0) continue
if (
variable.defs.some((def) => {
if (def.type !== 'Variable') return false
return isInsideInitializer(def.node, propReferences)
})
) {
continue
}
context.report({
node: variable.defs[0].node,
messageId: 'duplicateKey',
data: {
name: prop.propName
}
})
}
}
})
)
/**
* Extracts references from the given node.
* @param {Pattern} node
* @returns {Identifier[]} References
*/
function extractReferences(node) {
if (node.type === 'Identifier') {
const variable = findVariable(context.getScope(), node)
if (!variable) {
return []
}
return variable.references.map((ref) => ref.identifier)
}
if (node.type === 'ObjectPattern') {
return node.properties.flatMap((prop) =>
extractReferences(prop.type === 'Property' ? prop.value : prop)
)
}
if (node.type === 'AssignmentPattern') {
return extractReferences(node.left)
}
if (node.type === 'RestElement') {
return extractReferences(node.argument)
}
return []
}
}
}