-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathprefer-separate-static-class.js
213 lines (189 loc) · 6.45 KB
/
prefer-separate-static-class.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* @author Flo Edelmann
* See LICENSE file in root directory for full license.
*/
'use strict'
const {
defineTemplateBodyVisitor,
isStringLiteral,
getStringLiteralValue
} = require('../utils')
/**
* @param {Expression | VForExpression | VOnExpression | VSlotScopeExpression | VFilterSequenceExpression} expressionNode
* @returns {(Literal | TemplateLiteral | Identifier)[]}
*/
function findStaticClasses(expressionNode) {
if (isStringLiteral(expressionNode)) {
return [expressionNode]
}
if (expressionNode.type === 'ArrayExpression') {
return expressionNode.elements.flatMap((element) => {
if (element === null || element.type === 'SpreadElement') {
return []
}
return findStaticClasses(element)
})
}
if (expressionNode.type === 'ObjectExpression') {
return expressionNode.properties.flatMap((property) => {
if (
property.type === 'Property' &&
property.value.type === 'Literal' &&
property.value.value === true &&
(isStringLiteral(property.key) ||
(property.key.type === 'Identifier' && !property.computed))
) {
return [property.key]
}
return []
})
}
return []
}
/**
* @param {VAttribute | VDirective} attributeNode
* @returns {attributeNode is VAttribute & { value: VLiteral }}
*/
function isStaticClassAttribute(attributeNode) {
return (
!attributeNode.directive &&
attributeNode.key.name === 'class' &&
attributeNode.value !== null
)
}
/**
* Removes the node together with the comma before or after the node.
* @param {RuleFixer} fixer
* @param {ParserServices.TokenStore} tokenStore
* @param {ASTNode} node
*/
function* removeNodeWithComma(fixer, tokenStore, node) {
const prevToken = tokenStore.getTokenBefore(node)
if (prevToken.type === 'Punctuator' && prevToken.value === ',') {
yield fixer.removeRange([prevToken.range[0], node.range[1]])
return
}
const [nextToken, nextNextToken] = tokenStore.getTokensAfter(node, {
count: 2
})
if (
nextToken.type === 'Punctuator' &&
nextToken.value === ',' &&
(nextNextToken.type !== 'Punctuator' ||
(nextNextToken.value !== ']' && nextNextToken.value !== '}'))
) {
yield fixer.removeRange([node.range[0], nextNextToken.range[0]])
return
}
yield fixer.remove(node)
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
description:
'require static class names in template to be in a separate `class` attribute',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/prefer-separate-static-class.html'
},
fixable: 'code',
schema: [],
messages: {
preferSeparateStaticClass:
'Static class "{{className}}" should be in a static `class` attribute.'
}
},
/** @param {RuleContext} context */
create(context) {
return defineTemplateBodyVisitor(context, {
/** @param {VDirectiveKey} directiveKeyNode */
"VAttribute[directive=true] > VDirectiveKey[name.name='bind'][argument.name='class']"(
directiveKeyNode
) {
const attributeNode = directiveKeyNode.parent
if (!attributeNode.value || !attributeNode.value.expression) {
return
}
const expressionNode = attributeNode.value.expression
const staticClassNameNodes = findStaticClasses(expressionNode)
for (const staticClassNameNode of staticClassNameNodes) {
const className =
staticClassNameNode.type === 'Identifier'
? staticClassNameNode.name
: getStringLiteralValue(staticClassNameNode, true)
if (className === null) {
continue
}
context.report({
node: staticClassNameNode,
messageId: 'preferSeparateStaticClass',
data: { className },
*fix(fixer) {
let dynamicClassDirectiveRemoved = false
yield* removeFromClassDirective()
yield* addToClassAttribute()
/**
* Remove class from dynamic `:class` directive.
*/
function* removeFromClassDirective() {
if (isStringLiteral(expressionNode)) {
yield fixer.remove(attributeNode)
dynamicClassDirectiveRemoved = true
return
}
const listElement =
staticClassNameNode.parent.type === 'Property'
? staticClassNameNode.parent
: staticClassNameNode
const listNode = listElement.parent
if (
listNode.type === 'ArrayExpression' ||
listNode.type === 'ObjectExpression'
) {
const elements =
listNode.type === 'ObjectExpression'
? listNode.properties
: listNode.elements
if (elements.length === 1 && listNode === expressionNode) {
yield fixer.remove(attributeNode)
dynamicClassDirectiveRemoved = true
return
}
const sourceCode = context.getSourceCode()
const tokenStore =
sourceCode.parserServices.getTemplateBodyTokenStore()
if (elements.length === 1) {
yield* removeNodeWithComma(fixer, tokenStore, listNode)
return
}
yield* removeNodeWithComma(fixer, tokenStore, listElement)
}
}
/**
* Add class to static `class` attribute.
*/
function* addToClassAttribute() {
const existingStaticClassAttribute =
attributeNode.parent.attributes.find(isStaticClassAttribute)
if (existingStaticClassAttribute) {
const literalNode = existingStaticClassAttribute.value
yield fixer.replaceText(
literalNode,
`"${literalNode.value} ${className}"`
)
return
}
// new static `class` attribute
const separator = dynamicClassDirectiveRemoved ? '' : ' '
yield fixer.insertTextBefore(
attributeNode,
`class="${className}"${separator}`
)
}
}
})
}
}
})
}
}