Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only detect nesting when using @apply #13325

Merged
merged 7 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 0 additions & 47 deletions src/lib/detectNesting.js

This file was deleted.

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(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used an error instead of a warning like we used to do for 2 reasons:

  1. All apply issues in this file use error.
  2. If we just warn, we would still generate incorrect code.

[
`The \`${applyCandidate}\` class cannot be applied because it uses nested CSS.`,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a new message, so we can definitely change some wording here.

Copy link
Member

@adamwathan adamwathan Mar 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like:

`The \`${applyCandidate}\` class cannot be used with \`@apply\` because \`@apply\` does not currently support nested CSS.`,
'Rewrite the selector without nesting or configure the `tailwindcss/nesting` plugin:',
'https://tailwindcss.com/docs/using-with-preprocessors#nesting',

'Please enable a CSS nesting plugin *before* Tailwind in your configuration.',
'See how here: https://tailwindcss.com/docs/using-with-preprocessors#nesting',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is coming from the original detectNesting code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can drop this part with the change above I think.

].join('\n')
)
})
}
RobinMalfait marked this conversation as resolved.
Show resolved Hide resolved

candidates.push([applyCandidate, important, rules])
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/processTailwindFeatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ import resolveDefaultsAtRules from './lib/resolveDefaultsAtRules'
import collapseAdjacentRules from './lib/collapseAdjacentRules'
import collapseDuplicateDeclarations from './lib/collapseDuplicateDeclarations'
import partitionApplyAtRules from './lib/partitionApplyAtRules'
import detectNesting from './lib/detectNesting'
import { createContext } from './lib/setupContextUtils'
import { issueFlagNotices } from './featureFlags'

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

detectNesting()(root, result)

// Partition apply rules that are found in the css
// itself.
partitionApplyAtRules()(root, result)
Expand Down
69 changes: 69 additions & 0 deletions tests/apply.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2143,3 +2143,72 @@ 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 = {
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;
}
}
`

expect.assertions(1)

return run(input, config).catch((err) => {
expect(err.reason).toMatchInlineSnapshot(`
"The \`baz\` 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"
`)
})
})

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

let input = css`
.foo {
.bar {
color: red;
}
}

.example {
@apply bar;
}
`

expect.assertions(1)

return run(input, config).catch((err) => {
expect(err.reason).toMatchInlineSnapshot(`
"The \`bar\` 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"
`)
})
})
128 changes: 0 additions & 128 deletions tests/detect-nesting.test.js

This file was deleted.

Loading