Skip to content

Commit 942cd34

Browse files
authored
fix(eslint-plugin): enhance generator to support filename context for config loading (#4978)
1 parent f106ed4 commit 942cd34

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

packages-integrations/eslint-plugin/src/rules/order-attributify.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export default createRule({
3434
context.settings.unocss?.configPath,
3535
'sort',
3636
input,
37+
context.filename,
3738
)
3839
if (sorted !== input) {
3940
context.report({

packages-integrations/eslint-plugin/src/rules/order.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export default createRule({
6060
context.settings.unocss?.configPath,
6161
'sort',
6262
input,
63+
context.filename,
6364
).trim()
6465

6566
if (addSpace === 'before')
@@ -109,6 +110,7 @@ export default createRule({
109110
context.settings.unocss?.configPath,
110111
'sort',
111112
input,
113+
context.filename,
112114
).trim()
113115
if (/^\s/.test(input))
114116
sorted = ` ${sorted}`

packages-integrations/eslint-plugin/src/worker.ts

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
import type { BlocklistMeta, UnoGenerator } from '@unocss/core'
2+
import { dirname } from 'node:path'
23
import process from 'node:process'
34
import { sortRules } from '#integration/sort-rules'
45
import { loadConfig } from '@unocss/config'
56
import { createGenerator } from '@unocss/core'
67
import { runAsWorker } from 'synckit'
78

8-
const promises = new Map<string | undefined, Promise<UnoGenerator<any>> | undefined>()
9+
const promises = new Map<string, Promise<UnoGenerator<any>> | undefined>()
910

1011
// bypass icon rules in ESLint
1112
process.env.ESLINT ||= 'true'
1213

13-
async function _getGenerator(configPath?: string) {
14+
async function _getGenerator(configPath?: string, id?: string) {
15+
// Determine the search directory:
16+
// 1. If configPath is provided, use process.cwd() (existing behavior for explicit config)
17+
// 2. If filename is provided and no configPath, search from the file's directory (monorepo support)
18+
// 3. Otherwise, use process.cwd() as fallback
19+
const searchFrom = configPath
20+
? process.cwd()
21+
: id
22+
? dirname(id)
23+
: process.cwd()
24+
1425
const { config, sources } = await loadConfig(
15-
process.cwd(),
26+
searchFrom,
1627
configPath,
1728
)
1829
if (!sources.length)
@@ -23,25 +34,36 @@ async function _getGenerator(configPath?: string) {
2334
})
2435
}
2536

26-
export async function getGenerator(configPath?: string) {
27-
let promise = promises.get(configPath)
37+
function getCacheKey(configPath?: string, id?: string): string {
38+
// Create a cache key based on configPath or the directory of the filename
39+
if (configPath)
40+
return `config:${configPath}`
41+
if (id)
42+
return `dir:${dirname(id)}`
43+
return `cwd:${process.cwd()}`
44+
}
45+
46+
export async function getGenerator(configPath?: string, id?: string) {
47+
const cacheKey = getCacheKey(configPath, id)
48+
let promise = promises.get(cacheKey)
2849
if (!promise) {
29-
promise = _getGenerator(configPath)
30-
promises.set(configPath, promise)
50+
promise = _getGenerator(configPath, id)
51+
promises.set(cacheKey, promise)
3152
}
3253
return await promise
3354
}
3455

3556
export function setGenerator(generator: Awaited<UnoGenerator<any>>, configPath?: string | undefined) {
36-
promises.set(configPath, Promise.resolve(generator))
57+
const cacheKey = configPath ? `config:${configPath}` : `cwd:${process.cwd()}`
58+
promises.set(cacheKey, Promise.resolve(generator))
3759
}
3860

39-
async function actionSort(configPath: string | undefined, classes: string) {
40-
return await sortRules(classes, await getGenerator(configPath))
61+
async function actionSort(configPath: string | undefined, classes: string, id?: string) {
62+
return await sortRules(classes, await getGenerator(configPath, id))
4163
}
4264

4365
async function actionBlocklist(configPath: string | undefined, classes: string, id?: string): Promise<[string, BlocklistMeta | undefined][]> {
44-
const uno = await getGenerator(configPath)
66+
const uno = await getGenerator(configPath, id)
4567
const blocked = new Map<string, BlocklistMeta | undefined>()
4668

4769
const extracted = await uno.applyExtractors(classes, id)
@@ -81,7 +103,7 @@ async function actionBlocklist(configPath: string | undefined, classes: string,
81103
return [...blocked]
82104
}
83105

84-
export function runAsync(configPath: string | undefined, action: 'sort', classes: string): Promise<string>
106+
export function runAsync(configPath: string | undefined, action: 'sort', classes: string, id?: string): Promise<string>
85107
export function runAsync(configPath: string | undefined, action: 'blocklist', classes: string, id?: string): Promise<[string, BlocklistMeta | undefined][]>
86108
export async function runAsync(configPath: string | undefined, action: string, ...args: any[]): Promise<any> {
87109
switch (action) {
@@ -94,7 +116,7 @@ export async function runAsync(configPath: string | undefined, action: string, .
94116
}
95117
}
96118

97-
export function run(configPath: string | undefined, action: 'sort', classes: string): string
119+
export function run(configPath: string | undefined, action: 'sort', classes: string, id?: string): string
98120
export function run(configPath: string | undefined, action: 'blocklist', classes: string, id?: string): [string, BlocklistMeta | undefined][]
99121
export function run(configPath: string | undefined, action: string, ...args: any[]): any {
100122
// @ts-expect-error cast

0 commit comments

Comments
 (0)