tailwindcss/packages/@tailwindcss-upgrade/src
Robin Malfait 68fcdf1f05
Do not crash when migrating empty classes (#20205)
This PR fixes a crash while using `npx @tailwindcss/upgrade` when
migrating classes with no body inside of an `@layer utilities`.

When running `npx @tailwindcss/upgrade`, one thing we do is migrate the
CSS from:
```css
@layer utilities {
  .foo {
    color: red;
  }
}
```
To:
```css
@utility foo {
  color: red;
}
```

We already have some logic that leaves non-classe (IDs, attribute
selectors, ...) alone. But if we are migrating a class that has no body,
then we will migrate that as well:
```css
@layer utilities {
  .empty {
  }
}
```
Is turned into:
```css
@utility empty {
}
```

But later in the migration process this will result in an error because
a `@utility` has to have at least _some_ nodes.

Ideally, you don't even have CSS that has empty rules since it doesn't
have any effect in the browser (except of making your CSS file bigger),
but it could be that you don't have control over this file, so a fix is
still valid.

This PR solves that by leaving those rules alone, and keep them in an
`@layer utilities`.

Fixes: #20204

## Test plan

1. Added a dedicated test for this usecase
2026-06-07 21:50:35 +02:00
..
codemods Do not crash when migrating empty classes (#20205) 2026-06-07 21:50:35 +02:00
commands/help Add initial codemod tooling (#14434) 2024-09-18 16:45:43 +02:00
utils Make upgrade tooling more stable (#19846) 2026-03-24 17:24:12 +01:00
index.test.ts Ensure @tailwindcss/upgrade runs on Tailwind CSS v4 projects and is idempotent (#17717) 2025-04-22 11:10:46 -04:00
index.ts Bump dependencies (#19957) 2026-04-24 21:21:12 +02:00
stylesheet.ts Bump dependencies (#19957) 2026-04-24 21:21:12 +02:00