From 5c76de72baa408d6cd32cd7ba97660617da6ff34 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Fri, 15 Apr 2022 11:57:06 -0400 Subject: [PATCH] Require matching prefix when detecting negatives (#8121) * Require matching prefix when detecting negatives * Update changelog --- CHANGELOG.md | 1 + src/lib/generateRules.js | 6 +++++- tests/prefix.test.js | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae4fc61fa214..1e94446bec2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Types: allow for arbitrary theme values (for 3rd party plugins) ([#7926](https://github.com/tailwindlabs/tailwindcss/pull/7926)) - Don’t split vars with numbers in them inside arbitrary values ([#8091](https://github.com/tailwindlabs/tailwindcss/pull/8091)) +- Require matching prefix when detecting negatives ([#8121](https://github.com/tailwindlabs/tailwindcss/pull/8121)) ### Added diff --git a/src/lib/generateRules.js b/src/lib/generateRules.js index fc9a6bbd6ec1..bbed6812eb48 100644 --- a/src/lib/generateRules.js +++ b/src/lib/generateRules.js @@ -382,7 +382,11 @@ function* resolveMatchedPlugins(classCandidate, context) { const twConfigPrefix = context.tailwindConfig.prefix const twConfigPrefixLen = twConfigPrefix.length - if (candidatePrefix[twConfigPrefixLen] === '-') { + + const hasMatchingPrefix = + candidatePrefix.startsWith(twConfigPrefix) || candidatePrefix.startsWith(`-${twConfigPrefix}`) + + if (candidatePrefix[twConfigPrefixLen] === '-' && hasMatchingPrefix) { negative = true candidatePrefix = twConfigPrefix + candidatePrefix.slice(twConfigPrefixLen + 1) } diff --git a/tests/prefix.test.js b/tests/prefix.test.js index 9124fd5b64c0..a82b98624ace 100644 --- a/tests/prefix.test.js +++ b/tests/prefix.test.js @@ -358,3 +358,19 @@ it('prefix with negative values and variants in the safelist', async () => { } `) }) + +it('prefix does not detect and generate unnecessary classes', async () => { + let config = { + prefix: 'tw-_', + content: [{ raw: html`-aaa-filter aaaa-table aaaa-hidden` }], + corePlugins: { preflight: false }, + } + + let input = css` + @tailwind utilities; + ` + + const result = await run(input, config) + + expect(result.css).toMatchFormattedCss(css``) +})