Skip to content

Commit

Permalink
fix(vscode): remove css-directive transformer to get matched result f…
Browse files Browse the repository at this point in the history
…rom source code (#1639)

Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
wkeylin and antfu committed Sep 26, 2022
1 parent 9926fe8 commit 43b4d03
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/shared-common/src/index.ts
Expand Up @@ -95,15 +95,23 @@ export function getMatchedPositions(code: string, matched: string[], hasVariantG
return result.sort((a, b) => a[0] - b[0])
}

// remove css-directive transformer to get matched result from source code
const ignoreTransformers = [
'css-directive',
'compile-class',
]

export async function getMatchedPositionsFromCode(uno: UnoGenerator, code: string, id = '') {
const s = new MagicString(code)
const tokens = new Set()
const ctx = { uno, tokens } as any
for (const i of uno.config.transformers?.filter(i => i.enforce === 'pre') || [])

const transformers = uno.config.transformers?.filter(i => !ignoreTransformers.includes(i.name))
for (const i of transformers?.filter(i => i.enforce === 'pre') || [])
await i.transform(s, id, ctx)
for (const i of uno.config.transformers?.filter(i => !i.enforce || i.enforce === 'default') || [])
for (const i of transformers?.filter(i => !i.enforce || i.enforce === 'default') || [])
await i.transform(s, id, ctx)
for (const i of uno.config.transformers?.filter(i => i.enforce === 'post') || [])
for (const i of transformers?.filter(i => i.enforce === 'post') || [])
await i.transform(s, id, ctx)
const hasVariantGroup = !!uno.config.transformers?.find(i => i.name === 'variant-group')
const result = await uno.generate(s.toString(), { preflights: false })
Expand Down
31 changes: 31 additions & 0 deletions test/pos.test.ts
Expand Up @@ -4,6 +4,7 @@ import presetUno from '@unocss/preset-uno'
import { createGenerator } from '@unocss/core'
import { getMatchedPositionsFromCode as match } from '@unocss/shared-common'
import transformerVariantGroup from '@unocss/transformer-variant-group'
import cssDirectives from '@unocss/transformer-directives'

describe('matched-positions', async () => {
test('attributify', async () => {
Expand Down Expand Up @@ -31,6 +32,36 @@ describe('matched-positions', async () => {
`)
})

test('css-directive', async () => {
const uno = createGenerator({
presets: [
presetUno(),
],
transformers: [cssDirectives()],
})

expect(await match(uno, '.btn-center{@apply text-center my-0 font-medium;\n}'))
.toMatchInlineSnapshot(`
[
[
19,
30,
"text-center",
],
[
31,
35,
"my-0",
],
[
36,
47,
"font-medium",
],
]
`)
})

test('class-based', async () => {
const uno = createGenerator({
presets: [
Expand Down

0 comments on commit 43b4d03

Please sign in to comment.