Skip to content
Merged
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
add dedicated optimization test
  • Loading branch information
RobinMalfait committed Jan 31, 2025
commit 356d0c52f2af510af0926c04d8752ef5fc0137ef
87 changes: 87 additions & 0 deletions packages/tailwindcss/src/ast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { expect, it } from 'vitest'
import { context, decl, optimizeAst, styleRule, toCss, walk, WalkAction } from './ast'
import * as CSS from './css-parser'

const css = String.raw

it('should pretty print an AST', () => {
expect(toCss(optimizeAst(CSS.parse('.foo{color:red;&:hover{color:blue;}}'))))
.toMatchInlineSnapshot(`
Expand Down Expand Up @@ -95,3 +97,88 @@ it('should stop walking when returning `WalkAction.Stop`', () => {
}
`)
})

it('should not emit empty rules once optimized', () => {
let ast = CSS.parse(css`
/* Empty rule */
.foo {
}

/* Empty rule, with nesting */
.foo {
.bar {
}
.baz {
}
}

/* Empty rule, with special case '&' rules */
.foo {
& {
&:hover {
}
&:focus {
}
}
}

/* Empty at-rule */
@media (min-width: 768px) {
}

/* Empty at-rule with nesting*/
@media (min-width: 768px) {
.foo {
}

@media (min-width: 1024px) {
.bar {
}
}
}

/* Exceptions: */
@charset 'UTF-8';
@layer foo, bar, baz;
@custom-media --modern (color), (hover);
`)

expect(toCss(ast)).toMatchInlineSnapshot(`
".foo {
}
.foo {
.bar {
}
.baz {
}
}
.foo {
& {
&:hover {
}
&:focus {
}
}
}
@media (min-width: 768px);
@media (min-width: 768px) {
.foo {
}
@media (min-width: 1024px) {
.bar {
}
}
}
@charset 'UTF-8';
@layer foo, bar, baz;
@custom-media --modern (color), (hover);
"
`)

expect(toCss(optimizeAst(ast))).toMatchInlineSnapshot(`
"@charset 'UTF-8';
@layer foo, bar, baz;
@custom-media --modern (color), (hover);
"
`)
})