add initial "unknown screen" diagnostics

master
Brad Cornes 2020-06-12 16:41:49 +01:00
parent 652052c1f4
commit 7c0247fb11
5 changed files with 65 additions and 3 deletions

View File

@ -104,6 +104,21 @@
"",
""
]
},
"tailwindCSS.lint.unknownScreen": {
"type": "string",
"enum": [
"ignore",
"warning",
"error"
],
"default": "error",
"markdownDescription": "",
"markdownEnumDescriptions": [
"",
"",
""
]
}
}
}

View File

@ -9,13 +9,16 @@ 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'
const dlv = require('dlv')
function getCssDiagnostics(
function getUnsupportedApplyDiagnostics(
state: State,
document: TextDocument,
settings: Settings
@ -128,6 +131,45 @@ function getUtilityConflictDiagnostics(
return diagnostics
}
function getScreenDirectiveDiagnostics(
state: State,
document: TextDocument,
settings: Settings
): Diagnostic[] {
let severity = settings.lint.unknownScreen
if (severity === 'ignore') return []
let text = document.getText()
let matches = findAll(/(?:\s|^)@screen\s+(?<screen>[^\s{]+)/g, text)
let screens = Object.keys(
dlv(state.config, 'theme.screens', dlv(state.config, 'screens', {}))
)
return matches
.map((match) => {
if (screens.includes(match.groups.screen)) {
return null
}
return {
range: {
start: indexToPosition(
text,
match.index + match[0].length - match.groups.screen.length
),
end: indexToPosition(text, match.index + match[0].length),
},
severity:
severity === 'error'
? DiagnosticSeverity.Error
: DiagnosticSeverity.Warning,
message: 'Unknown screen',
}
})
.filter(Boolean)
}
export async function provideDiagnostics(
state: State,
document: TextDocument
@ -138,7 +180,10 @@ export async function provideDiagnostics(
? [
...getUtilityConflictDiagnostics(state, document, settings),
...(isCssDoc(state, document)
? getCssDiagnostics(state, document, settings)
? [
...getUnsupportedApplyDiagnostics(state, document, settings),
...getScreenDirectiveDiagnostics(state, document, settings),
]
: []),
]
: []

View File

@ -40,6 +40,7 @@ const defaultSettings: Settings = {
lint: {
utilityConflicts: 'warning',
unsupportedApply: 'error',
unknownScreen: 'error',
},
}
let globalSettings: Settings = defaultSettings

View File

@ -296,7 +296,7 @@ export function findClassListsInDocument(
return []
}
function indexToPosition(str: string, index: number): Position {
export function indexToPosition(str: string, index: number): Position {
const { line, col } = lineColumn(str + '\n', index)
return { line: line - 1, character: col - 1 }
}

View File

@ -34,6 +34,7 @@ export type Settings = {
lint: {
utilityConflicts: DiagnosticSeveritySetting
unsupportedApply: DiagnosticSeveritySetting
unknownScreen: DiagnosticSeveritySetting
}
}