Ignore PostCSS nodes returned by addVariant (#8608)

* Fix issue with returning postcss nodes in addVariant

It’s not a supported use case but it didn’t use to break so let’s just fail silently

* Update changelog
This commit is contained in:
Jordan Pittman 2022-06-12 10:50:41 -04:00 • committed by GitHub
parent a9c7e52a59
commit 037396b4a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 2 deletions

View file

@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix extraction of multi-word utilities with arbitrary values and quotes ([#8604](https://github.com/tailwindlabs/tailwindcss/pull/8604))
- Fix casing of import of `corePluginList` type definition ([#8587](https://github.com/tailwindlabs/tailwindcss/pull/8587))
- Ignore PostCSS nodes returned by `addVariant` ([#8608](https://github.com/tailwindlabs/tailwindcss/pull/8608))
## [3.1.2] - 2022-06-10

View file

@ -465,11 +465,14 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
}
if (Array.isArray(result)) {
return result.map((variant) => parseVariant(variant))
return result
.filter((variant) => typeof variant === 'string')
.map((variant) => parseVariant(variant))
}
// result may be undefined with legacy variants that use APIs like `modifySelectors`
return result && parseVariant(result)(api)
// result may also be a postcss node if someone was returning the result from `modifySelectors`
return result && typeof result === 'string' && parseVariant(result)(api)
}
}

View file

@ -461,6 +461,57 @@ test('before and after variants are a bit special, and forced to the end (2)', (
})
})
test('returning non-strings and non-selectors in addVariant', () => {
/** @type {import('../types/config').Config} */
let config = {
content: [
{
raw: html`
<div class="peer-aria-expanded:text-center"></div>
<div class="peer-aria-expanded-2:text-center"></div>
`,
},
],
plugins: [
function ({ addVariant, e }) {
addVariant('peer-aria-expanded', ({ modifySelectors, separator }) =>
// Returning anything other string | string[] | undefined here is not supported
// But we're trying to be lenient here and just throw it out
modifySelectors(
({ className }) =>
`.peer[aria-expanded="true"] ~ .${e(`peer-aria-expanded${separator}${className}`)}`
)
)
addVariant('peer-aria-expanded-2', ({ modifySelectors, separator }) => {
let nodes = modifySelectors(
({ className }) =>
`.peer[aria-expanded="false"] ~ .${e(`peer-aria-expanded${separator}${className}`)}`
)
return [
// Returning anything other than strings here is not supported
// But we're trying to be lenient here and just throw it out
nodes,
'.peer[aria-expanded="false"] ~ &',
]
})
},
],
}
return run('@tailwind components;@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.peer[aria-expanded='true'] ~ .peer-aria-expanded\:text-center {
text-align: center;
}
.peer[aria-expanded='false'] ~ .peer-aria-expanded-2\:text-center {
text-align: center;
}
`)
})
})
it('should not generate variants of user css if it is not inside a layer', () => {
let config = {
content: [{ raw: html`<div class="hover:foo"></div>` }],