Skip to content

Commit 8f2aa3a

Browse files
Error on invalid @plugin usage (#14134)
This PR just adds two minor errors to guard against invalid `@plugin` usage similarly to what we do with `@source` in #14078.
1 parent e000caa commit 8f2aa3a

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

packages/tailwindcss/src/index.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,44 @@ describe('Parsing themes values from CSS', () => {
12581258
})
12591259

12601260
describe('plugins', () => {
1261+
test('@plugin can not have a body.', () => {
1262+
expect(() =>
1263+
compile(
1264+
css`
1265+
@plugin {
1266+
color: red;
1267+
}
1268+
`,
1269+
{
1270+
loadPlugin: () => {
1271+
return ({ addVariant }) => {
1272+
addVariant('hocus', '&:hover, &:focus')
1273+
}
1274+
},
1275+
},
1276+
).build(['hocus:underline']),
1277+
).toThrowErrorMatchingInlineSnapshot(`[Error: \`@plugin\` cannot have a body.]`)
1278+
})
1279+
1280+
test('@plugin cannot be nested.', () => {
1281+
expect(() =>
1282+
compile(
1283+
css`
1284+
div {
1285+
@plugin "my-plugin";
1286+
}
1287+
`,
1288+
{
1289+
loadPlugin: () => {
1290+
return ({ addVariant }) => {
1291+
addVariant('hocus', '&:hover, &:focus')
1292+
}
1293+
},
1294+
},
1295+
).build(['hocus:underline']),
1296+
).toThrowErrorMatchingInlineSnapshot(`[Error: \`@plugin\` cannot be nested.]`)
1297+
})
1298+
12611299
test('addVariant with string selector', () => {
12621300
let compiled = compile(
12631301
css`

packages/tailwindcss/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,15 @@ export function compile(
7878
if (node.kind !== 'rule') return
7979

8080
// Collect paths from `@plugin` at-rules
81-
if (node.selector.startsWith('@plugin ')) {
81+
if (node.selector === '@plugin' || node.selector.startsWith('@plugin ')) {
82+
if (node.nodes.length > 0) {
83+
throw new Error('`@plugin` cannot have a body.')
84+
}
85+
86+
if (parent !== null) {
87+
throw new Error('`@plugin` cannot be nested.')
88+
}
89+
8290
plugins.push(loadPlugin(node.selector.slice(9, -1)))
8391
replaceWith([])
8492
return

0 commit comments

Comments
 (0)