2020-10-08 15:20:54 +00:00
|
|
|
import { joinWithAnd } from '../util/joinWithAnd'
|
|
|
|
import { State, Settings } from '../util/state'
|
|
|
|
import type { TextDocument, DiagnosticSeverity } from 'vscode-languageserver'
|
2020-06-23 11:22:31 +00:00
|
|
|
import { CssConflictDiagnostic, DiagnosticKind } from './types'
|
2020-06-19 10:52:38 +00:00
|
|
|
import {
|
|
|
|
findClassListsInDocument,
|
|
|
|
getClassNamesInClassList,
|
2020-10-08 15:20:54 +00:00
|
|
|
} from '../util/find'
|
|
|
|
import { getClassNameDecls } from '../util/getClassNameDecls'
|
|
|
|
import { getClassNameMeta } from '../util/getClassNameMeta'
|
|
|
|
import { equal } from '../util/array'
|
2020-06-19 10:52:38 +00:00
|
|
|
|
2020-12-07 15:39:44 +00:00
|
|
|
export async function getCssConflictDiagnostics(
|
2020-06-19 10:52:38 +00:00
|
|
|
state: State,
|
|
|
|
document: TextDocument,
|
|
|
|
settings: Settings
|
2020-12-07 15:39:44 +00:00
|
|
|
): Promise<CssConflictDiagnostic[]> {
|
2020-06-23 11:22:31 +00:00
|
|
|
let severity = settings.lint.cssConflict
|
2020-06-19 10:52:38 +00:00
|
|
|
if (severity === 'ignore') return []
|
|
|
|
|
2020-06-23 11:22:31 +00:00
|
|
|
let diagnostics: CssConflictDiagnostic[] = []
|
2020-12-07 15:39:44 +00:00
|
|
|
const classLists = await findClassListsInDocument(state, document)
|
2020-06-19 10:52:38 +00:00
|
|
|
|
|
|
|
classLists.forEach((classList) => {
|
|
|
|
const classNames = getClassNamesInClassList(classList)
|
|
|
|
|
|
|
|
classNames.forEach((className, index) => {
|
|
|
|
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) &&
|
2020-06-23 20:14:47 +00:00
|
|
|
equal(meta.pseudo, otherMeta.pseudo) &&
|
|
|
|
meta.scope === otherMeta.scope
|
2020-06-19 10:52:38 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
if (conflictingClassNames.length === 0) return
|
|
|
|
|
|
|
|
diagnostics.push({
|
2020-06-23 11:22:31 +00:00
|
|
|
code: DiagnosticKind.CssConflict,
|
2020-06-19 10:52:38 +00:00
|
|
|
className,
|
|
|
|
otherClassNames: conflictingClassNames,
|
|
|
|
range: className.range,
|
|
|
|
severity:
|
|
|
|
severity === 'error'
|
2020-10-08 15:20:54 +00:00
|
|
|
? 1 /* DiagnosticSeverity.Error */
|
|
|
|
: 2 /* DiagnosticSeverity.Warning */,
|
2020-06-19 10:52:38 +00:00
|
|
|
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
|
|
|
|
}
|