Skip to content

Commit b96774a

Browse files
committed
chore: deduplicate RTL warnings in uno-preset-rtl
1 parent f3eaedc commit b96774a

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

test/unit/uno-preset-rtl.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { afterEach, beforeEach, describe, expect, it, vi, type MockInstance } from 'vitest'
2-
import { presetRtl } from '../../uno-preset-rtl'
2+
import { presetRtl, resetRtlWarnings } from '../../uno-preset-rtl'
33
import { createGenerator } from 'unocss'
44

55
describe('uno-preset-rtl', () => {
66
let warnSpy: MockInstance
77

88
beforeEach(() => {
9+
resetRtlWarnings()
910
warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
1011
})
1112

uno-preset-rtl.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
import type { CSSEntries, DynamicMatcher, Preset, RuleContext } from 'unocss'
22
import { cornerMap, directionSize, h } from '@unocss/preset-wind4/utils'
33

4+
// Track warnings to avoid duplicates
5+
const warnedClasses = new Set<string>()
6+
7+
function warnOnce(message: string, key: string) {
8+
if (!warnedClasses.has(key)) {
9+
warnedClasses.add(key)
10+
// oxlint-disable-next-line no-console -- warn logging
11+
console.warn(message)
12+
}
13+
}
14+
15+
/** Reset warning state (for testing) */
16+
export function resetRtlWarnings() {
17+
warnedClasses.clear()
18+
}
19+
420
const directionMap: Record<string, string[]> = {
521
'l': ['-left'],
622
'r': ['-right'],
@@ -30,9 +46,9 @@ function directionSizeRTL(
3046
const defaultMap = { l: 'is', r: 'ie' }
3147
const map = prefixMap || defaultMap
3248
const replacement = map[direction as 'l' | 'r']
33-
// oxlint-disable-next-line no-console -- warn logging
34-
console.warn(
49+
warnOnce(
3550
`[RTL] Avoid using '${match}'. Use '${match.replace(direction === 'l' ? 'l' : 'r', replacement)}' instead.`,
51+
match,
3652
)
3753
return matcher([match, replacement, size], context)
3854
}
@@ -83,9 +99,9 @@ export function presetRtl(): Preset {
8399
([, direction, size], context) => {
84100
if (!size) return undefined
85101
const replacement = direction === 'left' ? 'inset-is' : 'inset-ie'
86-
// oxlint-disable-next-line no-console -- warn logging
87-
console.warn(
102+
warnOnce(
88103
`[RTL] Avoid using '${direction}-${size}'. Use '${replacement}-${size}' instead.`,
104+
`${direction}-${size}`,
89105
)
90106
return directionSize('inset')(['', direction === 'left' ? 'is' : 'ie', size], context)
91107
},
@@ -95,8 +111,10 @@ export function presetRtl(): Preset {
95111
/^text-(left|right)$/,
96112
([, direction]) => {
97113
const replacement = direction === 'left' ? 'start' : 'end'
98-
// oxlint-disable-next-line no-console -- warn logging
99-
console.warn(`[RTL] Avoid using 'text-${direction}'. Use 'text-${replacement}' instead.`)
114+
warnOnce(
115+
`[RTL] Avoid using 'text-${direction}'. Use 'text-${replacement}' instead.`,
116+
`text-${direction}`,
117+
)
100118
return { 'text-align': replacement }
101119
},
102120
{ autocomplete: 'text-(left|right)' },
@@ -112,9 +130,9 @@ export function presetRtl(): Preset {
112130
}
113131
const replacement = replacementMap[direction]
114132
if (!replacement) return undefined
115-
// oxlint-disable-next-line no-console -- warn logging
116-
console.warn(
133+
warnOnce(
117134
`[RTL] Avoid using 'rounded-${direction}'. Use 'rounded-${replacement}' instead.`,
135+
`rounded-${direction}`,
118136
)
119137
return handlerRounded(['', replacement, size ?? 'DEFAULT'], context)
120138
},
@@ -124,9 +142,9 @@ export function presetRtl(): Preset {
124142
args => {
125143
const [_, direction, size] = args
126144
const replacement = direction === 'l' ? 'is' : 'ie'
127-
// oxlint-disable-next-line no-console -- warn logging
128-
console.warn(
145+
warnOnce(
129146
`[RTL] Avoid using 'border-${direction}'. Use 'border-${replacement}' instead.`,
147+
`border-${direction}`,
130148
)
131149
return handlerBorderSize(['', replacement, size || '1'])
132150
},

0 commit comments

Comments
 (0)