offer suggestions for unknown variants/screens/@tailwind

master
Brad Cornes 2020-06-16 15:41:45 +01:00
parent 62b30d5da2
commit bb3605c9ad
2 changed files with 39 additions and 15 deletions

View File

@ -22,8 +22,8 @@ import semver from 'semver'
import { getLanguageBoundaries } from '../util/getLanguageBoundaries' import { getLanguageBoundaries } from '../util/getLanguageBoundaries'
import { absoluteRange } from '../util/absoluteRange' import { absoluteRange } from '../util/absoluteRange'
import { isObject } from '../../class-names/isObject' import { isObject } from '../../class-names/isObject'
import levenshtein from 'js-levenshtein'
import { stringToPath } from '../util/stringToPath' import { stringToPath } from '../util/stringToPath'
import { closest } from '../util/closest'
function getUnsupportedApplyDiagnostics( function getUnsupportedApplyDiagnostics(
state: State, state: State,
@ -170,6 +170,12 @@ function getUnknownScreenDiagnostics(
return null return null
} }
let message = `The screen '${match.groups.screen}' does not exist in your theme config.`
let suggestion = closest(match.groups.screen, screens)
if (suggestion) {
message += ` Did you mean '${suggestion}'?`
}
diagnostics.push({ diagnostics.push({
range: absoluteRange( range: absoluteRange(
{ {
@ -185,7 +191,7 @@ function getUnknownScreenDiagnostics(
severity === 'error' severity === 'error'
? DiagnosticSeverity.Error ? DiagnosticSeverity.Error
: DiagnosticSeverity.Warning, : DiagnosticSeverity.Warning,
message: 'Unknown screen', message,
}) })
}) })
}) })
@ -227,6 +233,12 @@ function getUnknownVariantDiagnostics(
continue continue
} }
let message = `The variant '${variant}' does not exist.`
let suggestion = closest(variant, state.variants)
if (suggestion) {
message += ` Did you mean '${suggestion}'?`
}
let variantStartIndex = let variantStartIndex =
listStartIndex + variants.slice(0, i).join('').length listStartIndex + variants.slice(0, i).join('').length
@ -242,7 +254,7 @@ function getUnknownVariantDiagnostics(
severity === 'error' severity === 'error'
? DiagnosticSeverity.Error ? DiagnosticSeverity.Error
: DiagnosticSeverity.Warning, : DiagnosticSeverity.Warning,
message: `Unknown variant: ${variant}`, message,
}) })
} }
}) })
@ -329,17 +341,14 @@ function getInvalidHelperKeyDiagnostics(
...keys.slice(0, keys.length - 1), ...keys.slice(0, keys.length - 1),
]) ])
if (isObject(parentValue)) { if (isObject(parentValue)) {
let validKeys = Object.keys(parentValue) let closestValidKey = closest(
.filter((key) => isValid(parentValue[key])) keys[keys.length - 1],
.sort( Object.keys(parentValue).filter((key) => isValid(parentValue[key]))
(a, b) => )
levenshtein(keys[keys.length - 1], a) - if (closestValidKey) {
levenshtein(keys[keys.length - 1], b)
)
if (validKeys.length) {
message += ` Did you mean '${stitch([ message += ` Did you mean '${stitch([
...keys.slice(0, keys.length - 1), ...keys.slice(0, keys.length - 1),
validKeys[0], closestValidKey,
])}'?` ])}'?`
} }
} }
@ -422,6 +431,16 @@ function getUnsupportedTailwindDirectiveDiagnostics(
return null return null
} }
let message = `'${match.groups.value}' is not a valid group.`
if (match.groups.value === 'preflight') {
message += ` Did you mean 'base'?`
} else {
let suggestion = closest(match.groups.value, valid)
if (suggestion) {
message += ` Did you mean '${suggestion}'?`
}
}
diagnostics.push({ diagnostics.push({
range: absoluteRange( range: absoluteRange(
{ {
@ -437,9 +456,7 @@ function getUnsupportedTailwindDirectiveDiagnostics(
severity === 'error' severity === 'error'
? DiagnosticSeverity.Error ? DiagnosticSeverity.Error
: DiagnosticSeverity.Warning, : DiagnosticSeverity.Warning,
message: `Unsupported value: ${match.groups.value}${ message,
match.groups.value === 'preflight' ? '. Use base instead.' : ''
}`,
}) })
}) })
}) })

View File

@ -0,0 +1,7 @@
import levenshtein from 'js-levenshtein'
export function closest(input: string, options: string[]): string | undefined {
return options.sort(
(a, b) => levenshtein(input, a) - levenshtein(input, b)
)[0]
}