add initial "unsupported tailwind directive" lint rule

master
Brad Cornes 2020-06-12 17:34:59 +01:00
parent 2c2f63a0b8
commit 81aad28bc8
4 changed files with 67 additions and 0 deletions

View File

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

View File

@ -17,6 +17,7 @@ import { getClassNameDecls } from '../util/getClassNameDecls'
import { equal, flatten } from '../../util/array'
import { getDocumentSettings } from '../util/getDocumentSettings'
const dlv = require('dlv')
import semver from 'semver'
function getUnsupportedApplyDiagnostics(
state: State,
@ -265,6 +266,50 @@ function getUnknownConfigKeyDiagnostics(
.filter(Boolean)
}
function getUnsupportedTailwindDirectiveDiagnostics(
state: State,
document: TextDocument,
settings: Settings
): Diagnostic[] {
let severity = settings.lint.unsupportedTailwindDirective
if (severity === 'ignore') return []
let text = document.getText()
let matches = findAll(/(?:\s|^)@tailwind\s+(?<value>[^;]+)/g, text)
let allowed = [
'utilities',
'components',
'screens',
semver.gte(state.version, '1.0.0-beta.1') ? 'base' : 'preflight',
]
return matches
.map((match) => {
if (allowed.includes(match.groups.value)) {
return null
}
return {
range: {
start: indexToPosition(
text,
match.index + match[0].length - match.groups.value.length
),
end: indexToPosition(text, match.index + match[0].length),
},
severity:
severity === 'error'
? DiagnosticSeverity.Error
: DiagnosticSeverity.Warning,
message: `Unsupported value: ${match.groups.value}${
match.groups.value === 'preflight' ? '. Use base instead.' : ''
}`,
}
})
.filter(Boolean)
}
export async function provideDiagnostics(
state: State,
document: TextDocument
@ -280,6 +325,11 @@ export async function provideDiagnostics(
...getUnknownScreenDiagnostics(state, document, settings),
...getUnknownVariantDiagnostics(state, document, settings),
...getUnknownConfigKeyDiagnostics(state, document, settings),
...getUnsupportedTailwindDirectiveDiagnostics(
state,
document,
settings
),
]
: []),
]

View File

@ -43,6 +43,7 @@ const defaultSettings: Settings = {
unknownScreen: 'error',
unknownVariant: 'error',
unknownConfigKey: 'error',
unsupportedTailwindDirective: 'error',
},
}
let globalSettings: Settings = defaultSettings

View File

@ -37,6 +37,7 @@ export type Settings = {
unknownScreen: DiagnosticSeveritySetting
unknownVariant: DiagnosticSeveritySetting
unknownConfigKey: DiagnosticSeveritySetting
unsupportedTailwindDirective: DiagnosticSeveritySetting
}
}