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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
diff --git a/src/lsp/providers/diagnosticsProvider.ts b/src/lsp/providers/diagnostics/diagnosticsProvider.ts
rename from src/lsp/providers/diagnosticsProvider.ts
rename to src/lsp/providers/diagnostics/diagnosticsProvider.ts
index f6fa9092b7d83df2bd1647b212f32527f8c843ee..56362d5488cc8776715de6feac2096c57149f255 100644
--- a/src/lsp/providers/diagnosticsProvider.ts
+++ b/src/lsp/providers/diagnostics/diagnosticsProvider.ts
@@ -1,95 +1,103 @@
-import {
- TextDocument,
- Diagnostic,
- DiagnosticSeverity,
- Range,
-} from 'vscode-languageserver'
-import { State, Settings } from '../util/state'
-import { isCssDoc } from '../util/css'
+import { TextDocument, DiagnosticSeverity, Range } from 'vscode-languageserver'
+import { State, Settings } from '../../util/state'
+import { isCssDoc } from '../../util/css'
import {
findClassNamesInRange,
findClassListsInDocument,
getClassNamesInClassList,
findAll,
indexToPosition,
-} from '../util/find'
-import { getClassNameMeta } from '../util/getClassNameMeta'
-import { getClassNameDecls } from '../util/getClassNameDecls'
-import { equal } from '../../util/array'
-import { getDocumentSettings } from '../util/getDocumentSettings'
+} from '../../util/find'
+import { getClassNameMeta } from '../../util/getClassNameMeta'
+import { getClassNameDecls } from '../../util/getClassNameDecls'
+import { equal } from '../../../util/array'
+import { getDocumentSettings } from '../../util/getDocumentSettings'
const dlv = require('dlv')
import semver from 'semver'
-import { getLanguageBoundaries } from '../util/getLanguageBoundaries'
-import { absoluteRange } from '../util/absoluteRange'
-import { isObject } from '../../class-names/isObject'
-import { stringToPath } from '../util/stringToPath'
-import { closest } from '../util/closest'
+import { getLanguageBoundaries } from '../../util/getLanguageBoundaries'
+import { absoluteRange } from '../../util/absoluteRange'
+import { isObject } from '../../../class-names/isObject'
+import { stringToPath } from '../../util/stringToPath'
+import { closest } from '../../util/closest'
+import {
+ InvalidApplyDiagnostic,
+ DiagnosticKind,
+ UtilityConflictsDiagnostic,
+ InvalidScreenDiagnostic,
+ InvalidVariantDiagnostic,
+ InvalidConfigPathDiagnostic,
+ InvalidTailwindDirectiveDiagnostic,
+ AugmentedDiagnostic,
+} from './types'
function getInvalidApplyDiagnostics(
state: State,
document: TextDocument,
settings: Settings
-): Diagnostic[] {
+): InvalidApplyDiagnostic[] {
let severity = settings.lint.invalidApply
if (severity === 'ignore') return []
const classNames = findClassNamesInRange(document, undefined, 'css')
- let diagnostics: Diagnostic[] = classNames
- .map(({ className, range }) => {
- const meta = getClassNameMeta(state, className)
- if (!meta) return null
+ let diagnostics: InvalidApplyDiagnostic[] = classNames.map((className) => {
+ const meta = getClassNameMeta(state, className.className)
+ if (!meta) return null
- let message: string
+ let message: string
- if (Array.isArray(meta)) {
- message = `'@apply' cannot be used with '${className}' because it is included in multiple rulesets.`
- } else if (meta.source !== 'utilities') {
- message = `'@apply' cannot be used with '${className}' because it is not a utility.`
- } else if (meta.context && meta.context.length > 0) {
- if (meta.context.length === 1) {
- message = `'@apply' cannot be used with '${className}' because it is nested inside of an at-rule ('${meta.context[0]}').`
- } else {
- message = `'@apply' cannot be used with '${className}' because it is nested inside of at-rules (${meta.context
- .map((c) => `'${c}'`)
- .join(', ')}).`
- }
- } else if (meta.pseudo && meta.pseudo.length > 0) {
- if (meta.pseudo.length === 1) {
- message = `'@apply' cannot be used with '${className}' because its definition includes a pseudo-selector ('${meta.pseudo[0]}')`
- } else {
- message = `'@apply' cannot be used with '${className}' because its definition includes pseudo-selectors (${meta.pseudo
- .map((p) => `'${p}'`)
- .join(', ')}).`
- }
+ if (Array.isArray(meta)) {
+ message = `'@apply' cannot be used with '${className.className}' because it is included in multiple rulesets.`
+ } else if (meta.source !== 'utilities') {
+ message = `'@apply' cannot be used with '${className.className}' because it is not a utility.`
+ } else if (meta.context && meta.context.length > 0) {
+ if (meta.context.length === 1) {
+ message = `'@apply' cannot be used with '${className.className}' because it is nested inside of an at-rule ('${meta.context[0]}').`
+ } else {
+ message = `'@apply' cannot be used with '${
+ className.className
+ }' because it is nested inside of at-rules (${meta.context
+ .map((c) => `'${c}'`)
+ .join(', ')}).`
}
+ } else if (meta.pseudo && meta.pseudo.length > 0) {
+ if (meta.pseudo.length === 1) {
+ message = `'@apply' cannot be used with '${className.className}' because its definition includes a pseudo-selector ('${meta.pseudo[0]}')`
+ } else {
+ message = `'@apply' cannot be used with '${
+ className.className
+ }' because its definition includes pseudo-selectors (${meta.pseudo
+ .map((p) => `'${p}'`)
+ .join(', ')}).`
+ }
+ }
- if (!message) return null
+ if (!message) return null
- return {
- severity:
- severity === 'error'
- ? DiagnosticSeverity.Error
- : DiagnosticSeverity.Warning,
- range,
- message,
- code: 'invalidApply',
- }
- })
- .filter(Boolean)
+ return {
+ code: DiagnosticKind.InvalidApply,
+ severity:
+ severity === 'error'
+ ? DiagnosticSeverity.Error
+ : DiagnosticSeverity.Warning,
+ range: className.range,
+ message,
+ className,
+ }
+ })
- return diagnostics
+ return diagnostics.filter(Boolean)
}
function getUtilityConflictDiagnostics(
state: State,
document: TextDocument,
settings: Settings
-): Diagnostic[] {
+): UtilityConflictsDiagnostic[] {
let severity = settings.lint.utilityConflicts
if (severity === 'ignore') return []
- let diagnostics: Diagnostic[] = []
+ let diagnostics: UtilityConflictsDiagnostic[] = []
const classLists = findClassListsInDocument(state, document)
classLists.forEach((classList) => {
@@ -115,6 +123,9 @@ equal(meta.context, otherMeta.context) &&
equal(meta.pseudo, otherMeta.pseudo)
) {
diagnostics.push({
+ code: DiagnosticKind.UtilityConflicts,
+ className,
+ otherClassName,
range: className.range,
severity:
severity === 'error'
@@ -143,11 +154,11 @@ function getInvalidScreenDiagnostics(
state: State,
document: TextDocument,
settings: Settings
-): Diagnostic[] {
+): InvalidScreenDiagnostic[] {
let severity = settings.lint.invalidScreen
if (severity === 'ignore') return []
- let diagnostics: Diagnostic[] = []
+ let diagnostics: InvalidScreenDiagnostic[] = []
let ranges: Range[] = []
if (isCssDoc(state, document)) {
@@ -178,6 +189,7 @@ message += ` Did you mean '${suggestion}'?`
}
diagnostics.push({
+ code: DiagnosticKind.InvalidScreen,
range: absoluteRange(
{
start: indexToPosition(
@@ -204,11 +216,11 @@ function getInvalidVariantDiagnostics(
state: State,
document: TextDocument,
settings: Settings
-): Diagnostic[] {
+): InvalidVariantDiagnostic[] {
let severity = settings.lint.invalidVariant
if (severity === 'ignore') return []
- let diagnostics: Diagnostic[] = []
+ let diagnostics: InvalidVariantDiagnostic[] = []
let ranges: Range[] = []
if (isCssDoc(state, document)) {
@@ -244,6 +256,7 @@ let variantStartIndex =
listStartIndex + variants.slice(0, i).join('').length
diagnostics.push({
+ code: DiagnosticKind.InvalidVariant,
range: absoluteRange(
{
start: indexToPosition(text, variantStartIndex),
@@ -268,11 +281,11 @@ function getInvalidConfigPathDiagnostics(
state: State,
document: TextDocument,
settings: Settings
-): Diagnostic[] {
+): InvalidConfigPathDiagnostic[] {
let severity = settings.lint.invalidConfigPath
if (severity === 'ignore') return []
- let diagnostics: Diagnostic[] = []
+ let diagnostics: InvalidConfigPathDiagnostic[] = []
let ranges: Range[] = []
if (isCssDoc(state, document)) {
@@ -381,6 +394,7 @@ 1 + // open paren
match.groups.quote.length
diagnostics.push({
+ code: DiagnosticKind.InvalidConfigPath,
range: absoluteRange(
{
start: indexToPosition(text, startIndex),
@@ -404,11 +418,11 @@ function getInvalidTailwindDirectiveDiagnostics(
state: State,
document: TextDocument,
settings: Settings
-): Diagnostic[] {
+): InvalidTailwindDirectiveDiagnostic[] {
let severity = settings.lint.invalidTailwindDirective
if (severity === 'ignore') return []
- let diagnostics: Diagnostic[] = []
+ let diagnostics: InvalidTailwindDirectiveDiagnostic[] = []
let ranges: Range[] = []
if (isCssDoc(state, document)) {
@@ -446,6 +460,7 @@ }
}
diagnostics.push({
+ code: DiagnosticKind.InvalidTailwindDirective,
range: absoluteRange(
{
start: indexToPosition(
@@ -468,26 +483,48 @@
return diagnostics
}
-export async function provideDiagnostics(
+export async function getDiagnostics(
state: State,
- document: TextDocument
-): Promise<void> {
+ document: TextDocument,
+ only: DiagnosticKind[] = [
+ DiagnosticKind.UtilityConflicts,
+ DiagnosticKind.InvalidApply,
+ DiagnosticKind.InvalidScreen,
+ DiagnosticKind.InvalidVariant,
+ DiagnosticKind.InvalidConfigPath,
+ DiagnosticKind.InvalidTailwindDirective,
+ ]
+): Promise<AugmentedDiagnostic[]> {
const settings = await getDocumentSettings(state, document)
- const diagnostics: Diagnostic[] = settings.validate
+ return settings.validate
? [
- ...getUtilityConflictDiagnostics(state, document, settings),
- ...getInvalidApplyDiagnostics(state, document, settings),
- ...getInvalidScreenDiagnostics(state, document, settings),
- ...getInvalidVariantDiagnostics(state, document, settings),
- ...getInvalidConfigPathDiagnostics(state, document, settings),
- ...getInvalidTailwindDirectiveDiagnostics(state, document, settings),
+ ...(only.includes(DiagnosticKind.UtilityConflicts)
+ ? getUtilityConflictDiagnostics(state, document, settings)
+ : []),
+ ...(only.includes(DiagnosticKind.InvalidApply)
+ ? getInvalidApplyDiagnostics(state, document, settings)
+ : []),
+ ...(only.includes(DiagnosticKind.InvalidScreen)
+ ? getInvalidScreenDiagnostics(state, document, settings)
+ : []),
+ ...(only.includes(DiagnosticKind.InvalidVariant)
+ ? getInvalidVariantDiagnostics(state, document, settings)
+ : []),
+ ...(only.includes(DiagnosticKind.InvalidConfigPath)
+ ? getInvalidConfigPathDiagnostics(state, document, settings)
+ : []),
+ ...(only.includes(DiagnosticKind.InvalidTailwindDirective)
+ ? getInvalidTailwindDirectiveDiagnostics(state, document, settings)
+ : []),
]
: []
+}
+export async function provideDiagnostics(state: State, document: TextDocument) {
state.editor.connection.sendDiagnostics({
uri: document.uri,
- diagnostics,
+ diagnostics: await getDiagnostics(state, document),
})
}
|