Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
detect nesting only when using @apply with a class that uses nesting
  • Loading branch information
RobinMalfait committed Mar 22, 2024
commit ce0ec5adb462c474da34e410a911686a3a104bef
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')
)
})
}

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()
})