Skip to content

Commit

Permalink
fix(eslint-plugin): blocklist rule cant match variants (#3370)
Browse files Browse the repository at this point in the history
  • Loading branch information
chizukicn committed Nov 29, 2023
1 parent f5a10ce commit db19a01
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
40 changes: 36 additions & 4 deletions packages/eslint-plugin/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,44 @@ export async function getGenerator() {
return await promise
}

export function setGenerator(generator: Awaited<UnoGenerator<any>>) {
promise = Promise.resolve(generator)
}

async function actionSort(classes: string) {
return await sortRules(classes, await getGenerator())
}

async function actionBlocklist(classes: string, id?: string) {
const uno = await getGenerator()
const blocked = new Set<string>()

const extracted = await uno.applyExtractors(classes, id)
return [...extracted.values()].filter(i => uno.isBlocked(i))
const values = [...extracted.values()]

const matchBlocked = async (raw: string) => {
if (blocked.has(raw))
return
if (uno.isBlocked(raw)) {
blocked.add(raw)
return
}
let current = raw
for (const p of uno.config.preprocess)
current = p(raw)!
const applied = await uno.matchVariants(raw, current)
if (applied && uno.isBlocked(applied[1]))
blocked.add(raw)
}

await Promise.all(values.map(matchBlocked))

return [...blocked]
}

export function run(action: 'sort', classes: string): string
export function run(action: 'blocklist', classes: string, id?: string): string[]
export function run(action: string, ...args: any[]): any {
export function runAsync(action: 'sort', classes: string): Promise<string>
export function runAsync(action: 'blocklist', classes: string, id?: string): Promise<string[]>
export async function runAsync(action: string, ...args: any[]): Promise<any> {
switch (action) {
case 'sort':
// @ts-expect-error cast
Expand All @@ -48,4 +73,11 @@ export function run(action: string, ...args: any[]): any {
}
}

export function run(action: 'sort', classes: string): string
export function run(action: 'blocklist', classes: string, id?: string): string[]
export function run(action: string, ...args: any[]): any {
// @ts-expect-error cast
return runAsync(action, ...args)
}

runAsWorker(run as any)
32 changes: 32 additions & 0 deletions test/eslint-plugin-worker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, it } from 'vitest'
import { createGenerator } from '@unocss/core'
import { presetUno } from '@unocss/preset-uno'
import { runAsync, setGenerator } from '../packages/eslint-plugin/src/worker'

describe('worker', () => {
it('blocklist', async () => {
const uno = createGenerator({
presets: [
presetUno(),
],
blocklist: [
'block',
/^text-/,
i => i.includes('green'),
],
})
setGenerator(uno)
const rs = await runAsync('blocklist', 'block !block w-3px bg-green-500 text-red-500')
expect(rs).toEqual(['block', 'bg-green-500', 'text-red-500', '!block'])
})
it('sort', async () => {
const uno = createGenerator({
presets: [
presetUno(),
],
})
setGenerator(uno)
const rs = await runAsync('sort', 'text-red-300 w-8')
expect(rs).toMatchInlineSnapshot(`"w-8 text-red-300"`)
})
})

0 comments on commit db19a01

Please sign in to comment.