Skip to content

Commit adf22c3

Browse files
committed
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
1 parent a9c7e52 commit adf22c3

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

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

+48
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,54 @@ 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+
`)
509+
})
510+
})
511+
464512
it('should not generate variants of user css if it is not inside a layer', () => {
465513
let config = {
466514
content: [{ raw: html`<div class="hover:foo"></div>` }],

0 commit comments

Comments
 (0)