Skip to content

Commit

Permalink
Improve nesting detection (#6011)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Nov 10, 2021
1 parent 4860957 commit 4e21639
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
25 changes: 22 additions & 3 deletions src/lib/detectNesting.js
Expand Up @@ -2,17 +2,36 @@ export default function (_context) {
return (root, result) => {
let found = false

root.walkAtRules('tailwind', (node) => {
if (found) return false

if (node.parent && node.parent.type !== 'root') {
found = true
node.warn(
result,
[
'Nested @tailwind rules were detected, but are not supported.',
"Consider using a prefix to scope Tailwind's classes: https://tailwindcss.com/docs/configuration#prefix",
'Alternatively, use the important selector strategy: https://tailwindcss.com/docs/configuration#selector-strategy',
].join('\n')
)
return false
}
})

root.walkRules((rule) => {
if (found) return false

rule.walkRules((nestedRule) => {
found = true
nestedRule.warn(
result,
// TODO: Improve this warning message
'Nested CSS detected, checkout the docs on how to support nesting: https://tailwindcss.com/docs/using-with-preprocessors#nesting'
[
'Nested CSS was detected, but CSS nesting has not been configured correctly.',
'Please enable a CSS nesting plugin *before* Tailwind in your configuration.',
'See how here: https://tailwindcss.com/docs/using-with-preprocessors#nesting',
].join('\n')
)

return false
})
})
Expand Down
32 changes: 31 additions & 1 deletion tests/detect-nesting.test.js
Expand Up @@ -19,7 +19,37 @@ it('should warn when we detect nested css', () => {
expect(result.messages).toMatchObject([
{
type: 'warning',
text: 'Nested CSS detected, checkout the docs on how to support nesting: https://tailwindcss.com/docs/using-with-preprocessors#nesting',
text: [
'Nested CSS was detected, but CSS nesting has not been configured correctly.',
'Please enable a CSS nesting plugin *before* Tailwind in your configuration.',
'See how here: https://tailwindcss.com/docs/using-with-preprocessors#nesting',
].join('\n'),
},
])
})
})

it('should warn when we detect namespaced @tailwind at rules', () => {
let config = {
content: [{ raw: html`<div class="text-center"></div>` }],
}

let input = css`
.namespace {
@tailwind utilities;
}
`

return run(input, config).then((result) => {
expect(result.messages).toHaveLength(1)
expect(result.messages).toMatchObject([
{
type: 'warning',
text: [
'Nested @tailwind rules were detected, but are not supported.',
"Consider using a prefix to scope Tailwind's classes: https://tailwindcss.com/docs/configuration#prefix",
'Alternatively, use the important selector strategy: https://tailwindcss.com/docs/configuration#selector-strategy',
].join('\n'),
},
])
})
Expand Down

0 comments on commit 4e21639

Please sign in to comment.