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(vscode): remove css-directive transformer to get matched result from source code #1639

Merged
merged 3 commits into from Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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