From 2c2f63a0b8b54c19dd104e102bada4e0b889b2cb Mon Sep 17 00:00:00 2001 From: Brad Cornes Date: Fri, 12 Jun 2020 17:16:54 +0100 Subject: [PATCH] add initial "unknown config key" lint rule --- package.json | 15 ++++++++ src/lsp/providers/diagnosticsProvider.ts | 49 ++++++++++++++++++++++++ src/lsp/server.ts | 1 + src/lsp/util/state.ts | 1 + 4 files changed, 66 insertions(+) diff --git a/package.json b/package.json index 5992723..0aa66cb 100755 --- a/package.json +++ b/package.json @@ -134,6 +134,21 @@ "", "" ] + }, + "tailwindCSS.lint.unknownConfigKey": { + "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 bf87c9f..18f3bc5 100644 --- a/src/lsp/providers/diagnosticsProvider.ts +++ b/src/lsp/providers/diagnosticsProvider.ts @@ -217,6 +217,54 @@ function getUnknownVariantDiagnostics( ) } +function getUnknownConfigKeyDiagnostics( + state: State, + document: TextDocument, + settings: Settings +): Diagnostic[] { + let severity = settings.lint.unknownConfigKey + if (severity === 'ignore') return [] + + let text = document.getText() + let matches = findAll( + /(?\s|^)(?config|theme)\((?['"])(?[^)]+)\k\)/g, + text + ) + + return matches + .map((match) => { + let base = match.groups.helper === 'theme' ? ['theme'] : [] + let keys = match.groups.key.split(/[.\[\]]/).filter(Boolean) + let value = dlv(state.config, [...base, ...keys]) + + // TODO: check that the type is valid + // e.g. objects are not valid + if (typeof value !== 'undefined') { + return null + } + + let startIndex = + match.index + + match.groups.prefix.length + + match.groups.helper.length + + 1 + // open paren + match.groups.quote.length + + return { + range: { + start: indexToPosition(text, startIndex), + end: indexToPosition(text, startIndex + match.groups.key.length), + }, + severity: + severity === 'error' + ? DiagnosticSeverity.Error + : DiagnosticSeverity.Warning, + message: `Unknown ${match.groups.helper} key: ${match.groups.key}`, + } + }) + .filter(Boolean) +} + export async function provideDiagnostics( state: State, document: TextDocument @@ -231,6 +279,7 @@ export async function provideDiagnostics( ...getUnsupportedApplyDiagnostics(state, document, settings), ...getUnknownScreenDiagnostics(state, document, settings), ...getUnknownVariantDiagnostics(state, document, settings), + ...getUnknownConfigKeyDiagnostics(state, document, settings), ] : []), ] diff --git a/src/lsp/server.ts b/src/lsp/server.ts index c3cefc3..3c97071 100644 --- a/src/lsp/server.ts +++ b/src/lsp/server.ts @@ -42,6 +42,7 @@ const defaultSettings: Settings = { unsupportedApply: 'error', unknownScreen: 'error', unknownVariant: 'error', + unknownConfigKey: 'error', }, } let globalSettings: Settings = defaultSettings diff --git a/src/lsp/util/state.ts b/src/lsp/util/state.ts index f4d8a9f..45f4106 100644 --- a/src/lsp/util/state.ts +++ b/src/lsp/util/state.ts @@ -36,6 +36,7 @@ export type Settings = { unsupportedApply: DiagnosticSeveritySetting unknownScreen: DiagnosticSeveritySetting unknownVariant: DiagnosticSeveritySetting + unknownConfigKey: DiagnosticSeveritySetting } }