Reject invalid custom and arbitrary variants (#8345)
* WIP Still need to write error message * Update error message first pass at something better * Detect invalid variant formats returned by functions * Add proper error message Co-authored-by: Jordan Pittman <jordan@cryptica.me>
This commit is contained in:
parent
e41bf3d2a7
commit
7fa2a200b2
4 changed files with 90 additions and 2 deletions
|
|
@ -170,6 +170,10 @@ function withIdentifiers(styles) {
|
|||
})
|
||||
}
|
||||
|
||||
export function isValidVariantFormatString(format) {
|
||||
return format.startsWith('@') || format.includes('&')
|
||||
}
|
||||
|
||||
export function parseVariant(variant) {
|
||||
variant = variant
|
||||
.replace(/\n+/g, '')
|
||||
|
|
@ -221,10 +225,24 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
|
|||
if (typeof variantFunction !== 'string') {
|
||||
// Safelist public API functions
|
||||
return ({ modifySelectors, container, separator }) => {
|
||||
return variantFunction({ modifySelectors, container, separator })
|
||||
let result = variantFunction({ modifySelectors, container, separator })
|
||||
|
||||
if (typeof result === 'string' && !isValidVariantFormatString(result)) {
|
||||
throw new Error(
|
||||
`Your custom variant \`${variantName}\` has an invalid format string. Make sure it's an at-rule or contains a \`&\` placeholder.`
|
||||
)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
if (!isValidVariantFormatString(variantFunction)) {
|
||||
throw new Error(
|
||||
`Your custom variant \`${variantName}\` has an invalid format string. Make sure it's an at-rule or contains a \`&\` placeholder.`
|
||||
)
|
||||
}
|
||||
|
||||
return parseVariant(variantFunction)
|
||||
})
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue