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
|
import { joinWithAnd } from '../util/joinWithAnd'
import { State, Settings } from '../util/state'
import { CssConflictDiagnostic, DiagnosticKind } from './types'
import { findClassListsInDocument, getClassNamesInClassList } from '../util/find'
import { getClassNameDecls } from '../util/getClassNameDecls'
import { getClassNameMeta } from '../util/getClassNameMeta'
import { equal } from '../util/array'
import * as jit from '../util/jit'
import type { AtRule, Node, Rule } from 'postcss'
import type { TextDocument } from 'vscode-languageserver-textdocument'
function isCustomProperty(property: string): boolean {
return property.startsWith('--')
}
function isAtRule(node: Node): node is AtRule {
return node.type === 'atrule'
}
function isKeyframes(rule: Rule): boolean {
let parent = rule.parent
if (!parent) {
return false
}
if (isAtRule(parent) && parent.name === 'keyframes') {
return true
}
return false
}
function getRuleProperties(rule: Rule): string[] {
let properties: string[] = []
rule.walkDecls(({ prop }) => {
properties.push(prop)
})
if (properties.findIndex((p) => !isCustomProperty(p)) > -1) {
properties = properties.filter((p) => !isCustomProperty(p))
}
return properties
}
export async function getCssConflictDiagnostics(
state: State,
document: TextDocument,
settings: Settings
): Promise<CssConflictDiagnostic[]> {
let severity = settings.tailwindCSS.lint.cssConflict
if (severity === 'ignore') return []
let diagnostics: CssConflictDiagnostic[] = []
const classLists = await findClassListsInDocument(state, document)
classLists.forEach((classList) => {
const classNames = getClassNamesInClassList(classList, state.blocklist)
classNames.forEach((className, index) => {
if (state.jit) {
let { rules } = jit.generateRules(
state,
[className.className],
(rule) => !isKeyframes(rule)
)
if (rules.length === 0) {
return
}
let info: Array<{ context: string[]; properties: string[] }> = rules.map((rule) => {
let properties = getRuleProperties(rule)
let context = jit.getRuleContext(state, rule, className.className)
return { context, properties }
})
let otherClassNames = classNames.filter((_className, i) => i !== index)
let conflictingClassNames = otherClassNames.filter((otherClassName) => {
let { rules: otherRules } = jit.generateRules(
state,
[otherClassName.className],
(rule) => !isKeyframes(rule)
)
if (otherRules.length !== rules.length) {
return false
}
let propertiesAreComparable = false
for (let i = 0; i < otherRules.length; i++) {
let otherRule = otherRules[i]
let properties = getRuleProperties(otherRule)
if (info[i].properties.length > 0 && properties.length > 0) {
propertiesAreComparable = true
}
if (!equal(info[i].properties, properties)) {
return false
}
let context = jit.getRuleContext(state, otherRule, otherClassName.className)
if (!equal(info[i].context, context)) {
return false
}
}
if (!propertiesAreComparable) {
return false
}
return true
})
if (conflictingClassNames.length === 0) return
diagnostics.push({
code: DiagnosticKind.CssConflict,
className,
otherClassNames: conflictingClassNames,
range: className.range,
severity:
severity === 'error'
? 1 /* DiagnosticSeverity.Error */
: 2 /* DiagnosticSeverity.Warning */,
message: `'${className.className}' applies the same CSS properties as ${joinWithAnd(
conflictingClassNames.map(
(conflictingClassName) => `'${conflictingClassName.className}'`
)
)}.`,
relatedInformation: conflictingClassNames.map((conflictingClassName) => {
return {
message: conflictingClassName.className,
location: {
uri: document.uri,
range: conflictingClassName.range,
},
}
}),
})
return
}
let decls = getClassNameDecls(state, className.className)
if (!decls) return
let properties = Object.keys(decls)
let meta = getClassNameMeta(state, className.className)
let otherClassNames = classNames.filter((_className, i) => i !== index)
let conflictingClassNames = otherClassNames.filter((otherClassName) => {
let otherDecls = getClassNameDecls(state, otherClassName.className)
if (!otherDecls) return false
let otherMeta = getClassNameMeta(state, otherClassName.className)
return (
equal(properties, Object.keys(otherDecls)) &&
!Array.isArray(meta) &&
!Array.isArray(otherMeta) &&
equal(meta.context, otherMeta.context) &&
equal(meta.pseudo, otherMeta.pseudo) &&
meta.scope === otherMeta.scope
)
})
if (conflictingClassNames.length === 0) return
diagnostics.push({
code: DiagnosticKind.CssConflict,
className,
otherClassNames: conflictingClassNames,
range: className.range,
severity:
severity === 'error'
? 1 /* DiagnosticSeverity.Error */
: 2 /* DiagnosticSeverity.Warning */,
message: `'${className.className}' applies the same CSS ${
properties.length === 1 ? 'property' : 'properties'
} as ${joinWithAnd(
conflictingClassNames.map((conflictingClassName) => `'${conflictingClassName.className}'`)
)}.`,
relatedInformation: conflictingClassNames.map((conflictingClassName) => {
return {
message: conflictingClassName.className,
location: {
uri: document.uri,
range: conflictingClassName.range,
},
}
}),
})
})
})
return diagnostics
}
|