From 7c0247fb117799e3e456d69cf3ce46e3ea9e38ba Mon Sep 17 00:00:00 2001 From: Brad Cornes Date: Fri, 12 Jun 2020 16:41:49 +0100 Subject: [PATCH] add initial "unknown screen" diagnostics --- package.json | 15 ++++++++ src/lsp/providers/diagnosticsProvider.ts | 49 +++++++++++++++++++++++- src/lsp/server.ts | 1 + src/lsp/util/find.ts | 2 +- src/lsp/util/state.ts | 1 + 5 files changed, 65 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 6800a5f..aff66ab 100755 --- a/package.json +++ b/package.json @@ -104,6 +104,21 @@ "", "" ] + }, + "tailwindCSS.lint.unknownScreen": { + "type": "string", + "enum": [ + "ignore", + "warning", + "error" + ], + "default": "error", + "markdownDescription": "", + "markdownEnumDescriptions": [ + "", + "", + "" + ] } } } diff --git a/src/lsp/providers/diagnosticsProvider.ts b/src/lsp/providers/diagnosticsProvider.ts index 9602ee7..ccb74ac 100644 --- a/src/lsp/providers/diagnosticsProvider.ts +++ b/src/lsp/providers/diagnosticsProvider.ts @@ -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+(?[^\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), + ] : []), ] : [] diff --git a/src/lsp/server.ts b/src/lsp/server.ts index efbe0ab..979c318 100644 --- a/src/lsp/server.ts +++ b/src/lsp/server.ts @@ -40,6 +40,7 @@ const defaultSettings: Settings = { lint: { utilityConflicts: 'warning', unsupportedApply: 'error', + unknownScreen: 'error', }, } let globalSettings: Settings = defaultSettings diff --git a/src/lsp/util/find.ts b/src/lsp/util/find.ts index 3eb49e3..0f996a4 100644 --- a/src/lsp/util/find.ts +++ b/src/lsp/util/find.ts @@ -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 } } diff --git a/src/lsp/util/state.ts b/src/lsp/util/state.ts index 97aca8a..44ebc1a 100644 --- a/src/lsp/util/state.ts +++ b/src/lsp/util/state.ts @@ -34,6 +34,7 @@ export type Settings = { lint: { utilityConflicts: DiagnosticSeveritySetting unsupportedApply: DiagnosticSeveritySetting + unknownScreen: DiagnosticSeveritySetting } }