add initial "unknown config key" lint rule

master
Brad Cornes 2020-06-12 17:16:54 +01:00
parent 5e63a12b48
commit 2c2f63a0b8
4 changed files with 66 additions and 0 deletions

View File

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

View File

@ -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(
/(?<prefix>\s|^)(?<helper>config|theme)\((?<quote>['"])(?<key>[^)]+)\k<quote>\)/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( export async function provideDiagnostics(
state: State, state: State,
document: TextDocument document: TextDocument
@ -231,6 +279,7 @@ export async function provideDiagnostics(
...getUnsupportedApplyDiagnostics(state, document, settings), ...getUnsupportedApplyDiagnostics(state, document, settings),
...getUnknownScreenDiagnostics(state, document, settings), ...getUnknownScreenDiagnostics(state, document, settings),
...getUnknownVariantDiagnostics(state, document, settings), ...getUnknownVariantDiagnostics(state, document, settings),
...getUnknownConfigKeyDiagnostics(state, document, settings),
] ]
: []), : []),
] ]

View File

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

View File

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