tailwind-ctp-intellisense/packages/tailwindcss-language-service/src/diagnostics/getInvalidScreenDiagnostics.ts

72 lines
2.1 KiB
TypeScript
Raw Normal View History

import { State, Settings } from '../util/state'
import type { TextDocument, Range, DiagnosticSeverity } from 'vscode-languageserver'
2020-06-19 10:52:38 +00:00
import { InvalidScreenDiagnostic, DiagnosticKind } from './types'
import { isCssDoc } from '../util/css'
import { getLanguageBoundaries } from '../util/getLanguageBoundaries'
import { findAll, indexToPosition } from '../util/find'
import { closest } from '../util/closest'
import { absoluteRange } from '../util/absoluteRange'
2020-06-19 10:52:38 +00:00
const dlv = require('dlv')
export function getInvalidScreenDiagnostics(
state: State,
document: TextDocument,
settings: Settings
): InvalidScreenDiagnostic[] {
2021-05-04 11:40:50 +00:00
let severity = settings.tailwindCSS.lint.invalidScreen
2020-06-19 10:52:38 +00:00
if (severity === 'ignore') return []
let diagnostics: InvalidScreenDiagnostic[] = []
let ranges: Range[] = []
if (isCssDoc(state, document)) {
ranges.push(undefined)
} else {
let boundaries = getLanguageBoundaries(state, document)
if (!boundaries) return []
ranges.push(...boundaries.css)
}
ranges.forEach((range) => {
let text = document.getText(range)
let matches = findAll(/(?:\s|^)@screen\s+(?<screen>[^\s{]+)/g, text)
matches.forEach((match) => {
if (state.screens.includes(match.groups.screen)) {
2020-06-19 10:52:38 +00:00
return null
}
let message = `The screen '${match.groups.screen}' does not exist in your theme config.`
let suggestions: string[] = []
let suggestion = closest(match.groups.screen, state.screens)
2020-06-19 10:52:38 +00:00
if (suggestion) {
suggestions.push(suggestion)
message += ` Did you mean '${suggestion}'?`
}
diagnostics.push({
code: DiagnosticKind.InvalidScreen,
range: absoluteRange(
{
start: indexToPosition(
text,
match.index + match[0].length - match.groups.screen.length
),
end: indexToPosition(text, match.index + match[0].length),
},
range
),
severity:
severity === 'error'
? 1 /* DiagnosticSeverity.Error */
: 2 /* DiagnosticSeverity.Warning */,
2020-06-19 10:52:38 +00:00
message,
suggestions,
})
})
})
return diagnostics
}