Skip to content

Commit 7852d4f

Browse files
authored
Throw an error when applying the .group utility (#4666)
1 parent 36a02ed commit 7852d4f

File tree

3 files changed

+73
-11
lines changed

3 files changed

+73
-11
lines changed

src/lib/expandApplyAtRules.js

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { resolveMatches } from './generateRules'
33
import bigSign from '../util/bigSign'
44
import escapeClassName from '../util/escapeClassName'
55

6+
function prefix(context, selector) {
7+
let prefix = context.tailwindConfig.prefix
8+
return typeof prefix === 'function' ? prefix(selector) : prefix + selector
9+
}
10+
611
function buildApplyCache(applyCandidates, context) {
712
for (let candidate of applyCandidates) {
813
if (context.notClassCache.has(candidate) || context.applyClassCache.has(candidate)) {
@@ -170,6 +175,11 @@ function processApply(root, context) {
170175

171176
for (let applyCandidate of applyCandidates) {
172177
if (!applyClassCache.has(applyCandidate)) {
178+
if (applyCandidate === prefix(context, 'group')) {
179+
// TODO: Link to specific documentation page with error code.
180+
throw apply.error(`@apply should not be used with the '${applyCandidate}' utility`)
181+
}
182+
173183
throw apply.error(
174184
`The \`${applyCandidate}\` class does not exist. If \`${applyCandidate}\` is a custom class, make sure it is defined within a \`@layer\` directive.`
175185
)

src/util/resolveConfig.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ export default function resolveConfig(configs) {
261261
let allConfigs = [
262262
...extractPluginConfigs(configs),
263263
{
264-
content: [],
265264
prefix: '',
266265
important: false,
267266
separator: ':',
@@ -302,10 +301,10 @@ function normalizeConfig(config) {
302301
content: (() => {
303302
let { content, purge } = config
304303

305-
if (Array.isArray(content)) return content
306-
if (Array.isArray(content?.content)) return content.content
307304
if (Array.isArray(purge)) return purge
308305
if (Array.isArray(purge?.content)) return purge.content
306+
if (Array.isArray(content)) return content
307+
if (Array.isArray(content?.content)) return content.content
309308

310309
return []
311310
})(),

tests/jit/apply.test.js

+61-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ function run(input, config = {}) {
1111
})
1212
}
1313

14+
function css(templates) {
15+
return templates.join('')
16+
}
17+
1418
test('@apply', () => {
1519
let config = {
1620
darkMode: 'class',
@@ -207,19 +211,68 @@ test('@apply error with nested @anyatrulehere', async () => {
207211
}
208212

209213
let css = `
210-
@tailwind components;
211-
@tailwind utilities;
214+
@tailwind components;
215+
@tailwind utilities;
212216
213-
@layer components {
214-
.foo {
215-
@genie {
216-
@apply text-black;
217+
@layer components {
218+
.foo {
219+
@genie {
220+
@apply text-black;
221+
}
217222
}
218223
}
219-
}
220-
`
224+
`
221225

222226
await expect(run(css, config)).rejects.toThrowError(
223227
'@apply is not supported within nested at-rules like @genie'
224228
)
225229
})
230+
231+
test('@apply error when using .group utility', async () => {
232+
let config = {
233+
darkMode: 'class',
234+
content: [{ raw: '<div class="foo"></div>' }],
235+
corePlugins: { preflight: false },
236+
plugins: [],
237+
}
238+
239+
let input = css`
240+
@tailwind components;
241+
@tailwind utilities;
242+
243+
@layer components {
244+
.foo {
245+
@apply group;
246+
}
247+
}
248+
`
249+
250+
await expect(run(input, config)).rejects.toThrowError(
251+
`@apply should not be used with the 'group' utility`
252+
)
253+
})
254+
255+
test('@apply error when using a prefixed .group utility', async () => {
256+
let config = {
257+
prefix: 'tw-',
258+
darkMode: 'class',
259+
content: [{ raw: '<div class="foo"></div>' }],
260+
corePlugins: { preflight: false },
261+
plugins: [],
262+
}
263+
264+
let css = `
265+
@tailwind components;
266+
@tailwind utilities;
267+
268+
@layer components {
269+
.foo {
270+
@apply tw-group;
271+
}
272+
}
273+
`
274+
275+
await expect(run(css, config)).rejects.toThrowError(
276+
`@apply should not be used with the 'tw-group' utility`
277+
)
278+
})

0 commit comments

Comments
 (0)