Skip to content

Commit 037396b

Browse files
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
1 parent a9c7e52 commit 037396b

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
- Fix extraction of multi-word utilities with arbitrary values and quotes ([#8604](https://github.com/tailwindlabs/tailwindcss/pull/8604))
1111
- Fix casing of import of `corePluginList` type definition ([#8587](https://github.com/tailwindlabs/tailwindcss/pull/8587))
12+
- Ignore PostCSS nodes returned by `addVariant` ([#8608](https://github.com/tailwindlabs/tailwindcss/pull/8608))
1213

1314
## [3.1.2] - 2022-06-10
1415

src/lib/setupContextUtils.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,14 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
465465
}
466466

467467
if (Array.isArray(result)) {
468-
return result.map((variant) => parseVariant(variant))
468+
return result
469+
.filter((variant) => typeof variant === 'string')
470+
.map((variant) => parseVariant(variant))
469471
}
470472

471473
// result may be undefined with legacy variants that use APIs like `modifySelectors`
472-
return result && parseVariant(result)(api)
474+
// result may also be a postcss node if someone was returning the result from `modifySelectors`
475+
return result && typeof result === 'string' && parseVariant(result)(api)
473476
}
474477
}
475478

tests/variants.test.js

+51
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,57 @@ test('before and after variants are a bit special, and forced to the end (2)', (
461461
})
462462
})
463463

464+
test('returning non-strings and non-selectors in addVariant', () => {
465+
/** @type {import('../types/config').Config} */
466+
let config = {
467+
content: [
468+
{
469+
raw: html`
470+
<div class="peer-aria-expanded:text-center"></div>
471+
<div class="peer-aria-expanded-2:text-center"></div>
472+
`,
473+
},
474+
],
475+
plugins: [
476+
function ({ addVariant, e }) {
477+
addVariant('peer-aria-expanded', ({ modifySelectors, separator }) =>
478+
// Returning anything other string | string[] | undefined here is not supported
479+
// But we're trying to be lenient here and just throw it out
480+
modifySelectors(
481+
({ className }) =>
482+
`.peer[aria-expanded="true"] ~ .${e(`peer-aria-expanded${separator}${className}`)}`
483+
)
484+
)
485+
486+
addVariant('peer-aria-expanded-2', ({ modifySelectors, separator }) => {
487+
let nodes = modifySelectors(
488+
({ className }) =>
489+
`.peer[aria-expanded="false"] ~ .${e(`peer-aria-expanded${separator}${className}`)}`
490+
)
491+
492+
return [
493+
// Returning anything other than strings here is not supported
494+
// But we're trying to be lenient here and just throw it out
495+
nodes,
496+
'.peer[aria-expanded="false"] ~ &',
497+
]
498+
})
499+
},
500+
],
501+
}
502+
503+
return run('@tailwind components;@tailwind utilities', config).then((result) => {
504+
return expect(result.css).toMatchFormattedCss(css`
505+
.peer[aria-expanded='true'] ~ .peer-aria-expanded\:text-center {
506+
text-align: center;
507+
}
508+
.peer[aria-expanded='false'] ~ .peer-aria-expanded-2\:text-center {
509+
text-align: center;
510+
}
511+
`)
512+
})
513+
})
514+
464515
it('should not generate variants of user css if it is not inside a layer', () => {
465516
let config = {
466517
content: [{ raw: html`<div class="hover:foo"></div>` }],

0 commit comments

Comments
 (0)