Skip to content

Commit bda8421

Browse files
authored
Only detect nesting when using @apply (#13325)
* drop nesting detection for `@tailwind` * drop separate nesting detection entirely * detect nesting only when using `@apply` with a class that uses nesting * drop unnecessary `important` config * add test to verify applying nested user CSS errors * add error reason to tests * update `@apply` error message
1 parent 3ba51d1 commit bda8421

5 files changed

+86
-178
lines changed

src/lib/detectNesting.js

-47
This file was deleted.

src/lib/expandApplyAtRules.js

+17
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,23 @@ function processApply(root, context, localCache) {
432432

433433
let rules = applyClassCache.get(applyCandidate)
434434

435+
// Verify that we can apply the class
436+
for (let [, rule] of rules) {
437+
if (rule.type === 'atrule') {
438+
continue
439+
}
440+
441+
rule.walkRules(() => {
442+
throw apply.error(
443+
[
444+
`The \`${applyCandidate}\` class cannot be used with \`@apply\` because \`@apply\` does not currently support nested CSS.`,
445+
'Rewrite the selector without nesting or configure the `tailwindcss/nesting` plugin:',
446+
'https://tailwindcss.com/docs/using-with-preprocessors#nesting',
447+
].join('\n')
448+
)
449+
})
450+
}
451+
435452
candidates.push([applyCandidate, important, rules])
436453
}
437454
}

src/processTailwindFeatures.js

-3
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@ import resolveDefaultsAtRules from './lib/resolveDefaultsAtRules'
77
import collapseAdjacentRules from './lib/collapseAdjacentRules'
88
import collapseDuplicateDeclarations from './lib/collapseDuplicateDeclarations'
99
import partitionApplyAtRules from './lib/partitionApplyAtRules'
10-
import detectNesting from './lib/detectNesting'
1110
import { createContext } from './lib/setupContextUtils'
1211
import { issueFlagNotices } from './featureFlags'
1312

1413
export default function processTailwindFeatures(setupContext) {
1514
return async function (root, result) {
1615
let { tailwindDirectives, applyDirectives } = normalizeTailwindDirectives(root)
1716

18-
detectNesting()(root, result)
19-
2017
// Partition apply rules that are found in the css
2118
// itself.
2219
partitionApplyAtRules()(root, result)

tests/apply.test.js

+69
Original file line numberDiff line numberDiff line change
@@ -2143,3 +2143,72 @@ test('should not break replacing important selector when the same as the parent
21432143
}
21442144
`)
21452145
})
2146+
2147+
test('applying classes with nested CSS should result in an error', async () => {
2148+
let config = {
2149+
content: [
2150+
{
2151+
raw: html`<div class="foo"></div>`,
2152+
},
2153+
],
2154+
}
2155+
2156+
let input = css`
2157+
@tailwind components;
2158+
@layer components {
2159+
.bar .baz {
2160+
color: red;
2161+
2162+
&:hover {
2163+
color: red;
2164+
}
2165+
}
2166+
2167+
.foo {
2168+
@apply flex baz;
2169+
}
2170+
}
2171+
`
2172+
2173+
expect.assertions(1)
2174+
2175+
return run(input, config).catch((err) => {
2176+
expect(err.reason).toMatchInlineSnapshot(`
2177+
"The \`baz\` class cannot be used with \`@apply\` because \`@apply\` does not currently support nested CSS.
2178+
Rewrite the selector without nesting or configure the \`tailwindcss/nesting\` plugin:
2179+
https://tailwindcss.com/docs/using-with-preprocessors#nesting"
2180+
`)
2181+
})
2182+
})
2183+
2184+
test('applying user defined classes with nested CSS should result in an error', async () => {
2185+
let config = {
2186+
content: [
2187+
{
2188+
raw: html`<div class="example"></div>`,
2189+
},
2190+
],
2191+
}
2192+
2193+
let input = css`
2194+
.foo {
2195+
.bar {
2196+
color: red;
2197+
}
2198+
}
2199+
2200+
.example {
2201+
@apply bar;
2202+
}
2203+
`
2204+
2205+
expect.assertions(1)
2206+
2207+
return run(input, config).catch((err) => {
2208+
expect(err.reason).toMatchInlineSnapshot(`
2209+
"The \`bar\` class cannot be used with \`@apply\` because \`@apply\` does not currently support nested CSS.
2210+
Rewrite the selector without nesting or configure the \`tailwindcss/nesting\` plugin:
2211+
https://tailwindcss.com/docs/using-with-preprocessors#nesting"
2212+
`)
2213+
})
2214+
})

tests/detect-nesting.test.js

-128
This file was deleted.

0 commit comments

Comments
 (0)