Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(variant-group)!: remove quotes matching, add option to config seperator #1231

Merged
merged 7 commits into from Jul 9, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
124 changes: 0 additions & 124 deletions packages/core/src/utils/extractQuoted.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/src/utils/index.ts
Expand Up @@ -7,4 +7,3 @@ export * from './layer'
export * from './variantGroup'
export * from './warn'
export * from './handlers'
export * from './extractQuoted'
12 changes: 8 additions & 4 deletions packages/core/src/utils/variantGroup.ts
Expand Up @@ -7,9 +7,10 @@ export function expandVariantGroup(str: MagicString): MagicString
export function expandVariantGroup(str: string | MagicString) {
regexClassGroup.lastIndex = 0
let hasChanged = false
let content = str.toString()
do {
const before = str.toString()
str = str.replace(
const before = content
content = content.replace(
regexClassGroup,
(_, pre, sep, body: string) => {
return body
Expand All @@ -18,8 +19,11 @@ export function expandVariantGroup(str: string | MagicString) {
.join(' ')
},
)
hasChanged = str.toString() !== before
hasChanged = content !== before
} while (hasChanged)

return str
if (typeof str === 'string')
return content
else
return str.overwrite(0, str.length(), content)
}
13 changes: 2 additions & 11 deletions packages/transformer-variant-group/src/index.ts
@@ -1,21 +1,12 @@
import type { SourceCodeTransformer } from '@unocss/core'
import { expandVariantGroup, extractQuoted } from '@unocss/core'
import { expandVariantGroup } from '@unocss/core'

export default function transformerVariantGroup(): SourceCodeTransformer {
return {
name: 'variant-group',
enforce: 'pre',
transform(s) {
extractQuoted(
s.toString(),
{
details: true,
templateStaticOnly: true,
deep: true,
},
)
.filter(({ value: { length } }) => length)
.forEach(({ value, range }) => s.overwrite(...range, expandVariantGroup(value)))
expandVariantGroup(s)
},
}
}
64 changes: 0 additions & 64 deletions test/extract-quoted.test.ts

This file was deleted.

11 changes: 2 additions & 9 deletions test/transformer-variant-group.test.ts
@@ -1,5 +1,4 @@
import { readFile } from 'fs/promises'
import transformerVariantGroup from '@unocss/transformer-variant-group'
import { describe, expect, test } from 'vitest'
import { expandVariantGroup } from '@unocss/core'
import MagicString from 'magic-string'
Expand Down Expand Up @@ -33,22 +32,16 @@ describe('transformer-variant-group', () => {
})

test('vue file', async () => {
const transformer = transformerVariantGroup()
const transform = async (code: string) => {
const s = new MagicString(code)
await transformer.transform(s, '', {} as any)
return s.toString()
}
const file = await readFile('./test/assets/variant-group.vue', 'utf-8')
const result = await transform(file)
expect(result).toMatchInlineSnapshot(`
"<script setup lang=\\"ts\\">
const a = 1
const b = 2
// eslint-disable-next-line @typescript-eslint/space-infix-ops
const c = a-(b -a -b)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this one is a JS that should not be transformed.

Copy link
Member Author

@Dunqing Dunqing Jul 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but there is no good way to solve this problem.

I think this is a very rare case where no one would write arithmetic this way, and we can either wait for this problem to appear and then solve it, or provide an option to disable the -transform.

const c = a-b a--a a--b
</script>

<template>
<div class=\\"bg-white font-light sm:hover:bg-gray-100 sm:hover:font-medium\\" />
<div class=\\"lt-sm:hover:p-1 lt-sm:hover:p-2\\" />
Expand Down