8000 fix(transformer-directives): safely remove empty blocks (#4644) · unocss/unocss@ac4f185 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit ac4f185

Browse files
authored
fix(transformer-directives): safely remove empty blocks (#4644)
1 parent c70d0fa commit ac4f185

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

packages-presets/transformer-directives/src/transform.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,16 @@ export async function transformDirectives(
7171

7272
await Promise.all(stack)
7373

74-
// Remove empty blocks
7574
const oldCode = code.toString()
75+
// transformDirectives has recursive behavior, only for the last call
7676
if (!isHasApply(oldCode)) {
77-
const newCode = oldCode.replace(/[^{}]*\{\s*\}\s*/g, '')
77+
const newCode = oldCode.replace(/([^{}]+)\{\s*\}\s*/g, (m, selector) => {
78+
// Only remove empty rules with valid selectors
79+
if (/^[\s\w\-.,#:[\]=*"'>~+^$|()\\]+$/.test(selector.trim()))
80+
return ''
81+
return m
82+
})
83+
7884
if (newCode !== oldCode)
7985
code.update(0, code.original.length, newCode)
8086
}

test/transformer-directives.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,28 @@ div {
655655
`)
656656
})
657657

658+
it('@apply with empty block comments', async () => {
659+
const result = await transform(
660+
`
661+
.aaa {
662+
@apply text-blue-500;
663+
/* {} empty curly brace in comment will lead to wrong parsing */
664+
font-weight: 900;
665+
}
666+
`,
667+
)
668+
expect(result)
669+
.toMatchInlineSnapshot(`
670+
".aaa {
671+
--un-text-opacity: 1;
672+
color: rgb(59 130 246 / var(--un-text-opacity));
673+
/* {} empty curly brace in comment will lead to wrong parsing */
674+
font-weight: 900;
675+
}
676+
"
677+
`)
678+
})
679+
658680
it('@apply selector group', async () => {
659681
const result = await transform(
660682
'.btn { @apply: sgroup:bg-orange }',

0 commit comments

Comments
 (0)
0