Skip to content

Commit

Permalink
detect nesting only when using @apply with a class that uses nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Mar 22, 2024
1 parent 87338ed commit ce0ec5a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/lib/expandApplyAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,23 @@ function processApply(root, context, localCache) {

let rules = applyClassCache.get(applyCandidate)

// Verify that we can apply the class
for (let [, rule] of rules) {
if (rule.type === 'atrule') {
continue
}

rule.walkRules(() => {
throw apply.error(
[
`The \`${applyCandidate}\` class cannot be applied because it uses nested CSS.`,
'Please enable a CSS nesting plugin *before* Tailwind in your configuration.',
'See how here: https://tailwindcss.com/docs/using-with-preprocessors#nesting',
].join('\n')
)
})
}

candidates.push([applyCandidate, important, rules])
}
}
Expand Down
30 changes: 30 additions & 0 deletions tests/apply.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2143,3 +2143,33 @@ test('should not break replacing important selector when the same as the parent
}
`)
})

test('applying classes with nested CSS should result in an error', async () => {
let config = {
important: '.foo',
content: [
{
raw: html`<div class="foo"></div>`,
},
],
}

let input = css`
@tailwind components;
@layer components {
.bar .baz {
color: red;
&:hover {
color: red;
}
}
.foo {
@apply flex baz;
}
}
`

return expect(() => run(input, config)).rejects.toThrowError()
})

0 comments on commit ce0ec5a

Please sign in to comment.