-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathgetDeprecatedClassDiagnostics.ts
62 lines (49 loc) · 1.94 KB
/
getDeprecatedClassDiagnostics.ts
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
import type { State, Settings } from '../util/state'
import { type DeprecatedClassDiagnostic, DiagnosticKind } from './types'
import { findClassListsInDocument, getClassNamesInClassList } from '../util/find'
import type { TextDocument } from 'vscode-languageserver-textdocument'
export async function getDeprecatedClassDiagnostics(
state: State,
document: TextDocument,
settings: Settings,
): Promise<DeprecatedClassDiagnostic[]> {
// Only v4 projects can report deprecations
if (!state.v4) return []
// This is an earlier v4 version that does not support class deprecations
if (!state.designSystem.classMetadata) return []
let severity = settings.tailwindCSS.lint.deprecatedClass
if (severity === 'ignore') return []
// Fill in the list of statically known deprecated classes
let deprecations = new Map<string, boolean>(
state.classList.map(([className, meta]) => [className, meta.deprecated ?? false]),
)
function isDeprecated(className: string) {
if (deprecations.has(className)) {
return deprecations.get(className)
}
let metadata = state.designSystem.classMetadata([className])[0]
let deprecated = metadata?.deprecated ?? false
deprecations.set(className, deprecated)
return deprecated
}
let diagnostics: DeprecatedClassDiagnostic[] = []
let classLists = await findClassListsInDocument(state, document)
for (let classList of classLists) {
let classNames = getClassNamesInClassList(classList, state.blocklist)
for (let className of classNames) {
if (!isDeprecated(className.className)) continue
diagnostics.push({
code: DiagnosticKind.DeprecatedClass,
className,
range: className.range,
severity:
severity === 'error'
? 1 /* DiagnosticSeverity.Error */
: 2 /* DiagnosticSeverity.Warning */,
message: `'${className.className}' is deprecated.`,
suggestions: [],
})
}
}
return diagnostics
}