From 4e21639903a8b54d1d71d8d19768125f682cf04d Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Wed, 10 Nov 2021 13:15:32 +0100 Subject: [PATCH] Improve nesting detection (#6011) --- src/lib/detectNesting.js | 25 ++++++++++++++++++++++--- tests/detect-nesting.test.js | 32 +++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/lib/detectNesting.js b/src/lib/detectNesting.js index 5a3181a273e0..02b39aec81d7 100644 --- a/src/lib/detectNesting.js +++ b/src/lib/detectNesting.js @@ -2,6 +2,23 @@ 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 @@ -9,10 +26,12 @@ export default function (_context) { 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 }) }) diff --git a/tests/detect-nesting.test.js b/tests/detect-nesting.test.js index 35e30393d5ed..bd123eca536d 100644 --- a/tests/detect-nesting.test.js +++ b/tests/detect-nesting.test.js @@ -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`
` }], + } + + 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'), }, ]) })