Skip to content

AI-native design editor. Open-source Figma alternative.

fix(docs): check SDK examples against the docs stylesheet (#1163)

Danila Poyarkov9c5fdf759575
diff --git a/packages/vue/src/primitives/BindableValue/examples/ModeEdit.vue b/packages/vue/src/primitives/BindableValue/examples/ModeEdit.vue
index cd54e5253..38c9a3a55 100644
--- a/packages/vue/src/primitives/BindableValue/examples/ModeEdit.vue
+++ b/packages/vue/src/primitives/BindableValue/examples/ModeEdit.vue
@@ -70,7 +70,7 @@ function refresh(action: () => unknown) {
:value="8"
policy="edit-variable"
>
- <div class="flex flex-wrap gap-2 bg-panel p-4 text-surface">
+ <div class="flex flex-wrap gap-2 bg-[var(--vp-c-bg)] p-4 text-[var(--vp-c-text-1)]">
<button @click="actions.beginMutation('edit')">Begin edit</button>
<button @click="refresh(() => actions.applyValue(12))">Set 12</button>
<button @click="changeTargets">Change selection and mode</button>
diff --git a/packages/vue/src/primitives/Fill/examples/States.vue b/packages/vue/src/primitives/Fill/examples/States.vue
index 5a0eade74..3e0bf5e90 100644
--- a/packages/vue/src/primitives/Fill/examples/States.vue
+++ b/packages/vue/src/primitives/Fill/examples/States.vue
@@ -126,7 +126,7 @@ const provider: BindingProvider<Color> = {
class="absolute h-3 w-full rounded bg-[linear-gradient(to_right,#ddd,#7c3aed)]"
/>
<ChannelSliderThumb
- class="block size-4 rounded-full border-2 border-white bg-accent shadow outline-none focus-visible:ring-2"
+ class="block size-4 rounded-full border-2 border-white bg-[var(--vp-c-brand-1)] shadow outline-none focus-visible:ring-2"
/>
<output class="ml-auto pl-4 text-xs tabular-nums"
>{{ Math.round(slider.value * 100) }}%</output
diff --git a/tools/checks/lint/src/rules/quality/tailwind-classes.ts b/tools/checks/lint/src/rules/quality/tailwind-classes.ts
index d57d7d5a6..91bdb91fe 100644
--- a/tools/checks/lint/src/rules/quality/tailwind-classes.ts
+++ b/tools/checks/lint/src/rules/quality/tailwind-classes.ts
@@ -14,36 +14,68 @@ import type { ESTree } from '@oxlint/plugins'
import { __unstable__loadDesignSystem } from '@tailwindcss/node'
import { compact } from 'es-toolkit/array'
+type DesignSystem = Awaited<ReturnType<typeof __unstable__loadDesignSystem>>
+
+interface Theme {
+ stylesheet: string
+ /** Tailwind's own model of the stylesheet's theme, utilities, and variants. */
+ system: DesignSystem
+}
+
/**
- * Tailwind's own model of the app's theme, utilities, and variants. `bun run lint` and the rule's
- * tests run Oxlint from the repository root; a run elsewhere has no app theme to check against,
- * and the rule stays quiet rather than keep the plugin's other rules from loading.
+ * The stylesheet that builds each part of the repository, first match first: the SDK's
+ * primitives and their examples render in the docs site, everything else in the app.
*/
-type DesignSystem = Awaited<ReturnType<typeof __unstable__loadDesignSystem>>
+const STYLESHEETS = [
+ { files: 'packages/vue/src/', stylesheet: 'packages/docs/.vitepress/theme/tailwind.css' },
+ { files: '', stylesheet: 'src/app.css' }
+]
+
+/**
+ * `bun run lint` and the rule's tests run Oxlint from the repository root; a run elsewhere has
+ * no stylesheet to check against, and the rule stays quiet rather than keep the plugin's other
+ * rules from loading.
+ */
+async function loadTheme(stylesheet: string): Promise<Theme | null> {
+ const path = resolve(stylesheet)
+ if (!existsSync(path)) return null
+ const system = await __unstable__loadDesignSystem(readFileSync(path, 'utf8'), {
+ base: dirname(path)
+ })
+ return { stylesheet, system }
+}
-const stylesheet = resolve('src/app.css')
-const designSystem = existsSync(stylesheet)
- ? await __unstable__loadDesignSystem(readFileSync(stylesheet, 'utf8'), {
- base: dirname(stylesheet)
- })
- : null
+const themes = await Promise.all(
+ STYLESHEETS.map(async ({ files, stylesheet }) => ({ files, theme: await loadTheme(stylesheet) }))
+)
-const verdicts = new Map<string, string | null>()
+function themeFor(file: string): Theme | null {
+ const match = themes.find(({ files }) => file.startsWith(files) || file.includes(`/${files}`))
+ return match?.theme ?? null
+}
+
+const verdicts = new WeakMap<Theme, Map<string, string | null>>()
/**
* Why Tailwind cannot use a class, or null when it can or the class is not Tailwind's. A class
* Tailwind parses as a utility but compiles to nothing names a value the theme lacks, such as a
* color with no token. A color written as `[var(--color-*)]` has a utility of its own.
*/
-function classProblem(system: DesignSystem, className: string): string | null {
- const cached = verdicts.get(className)
+function classProblem(theme: Theme, className: string): string | null {
+ const { system, stylesheet } = theme
+ let cache = verdicts.get(theme)
+ if (!cache) {
+ cache = new Map()
+ verdicts.set(theme, cache)
+ }
+ const cached = cache.get(className)
if (cached !== undefined) return cached
const candidates = system.parseCandidate(className)
const [css] = system.candidatesToCss([className])
let problem: string | null = null
if (!css) {
if (candidates.length > 0)
- problem = `Tailwind has no \`${className}\`; src/app.css defines no such value`
+ problem = `Tailwind has no \`${className}\`; ${stylesheet} defines no such value`
} else {
for (const candidate of candidates) {
if (candidate.kind !== 'functional' || candidate.value?.kind !== 'arbitrary') continue
@@ -51,11 +83,11 @@ function classProblem(system: DesignSystem, className: string): string | null {
if (!color) continue
problem =
system.resolveThemeValue(`--color-${color[1]}`) === undefined
- ? `\`${className}\` names \`--color-${color[1]}\`, which src/app.css does not define`
+ ? `\`${className}\` names \`--color-${color[1]}\`, which ${stylesheet} does not define`
: `use \`${candidate.root}-${color[1]}\` instead of \`${className}\``
}
}
- verdicts.set(className, problem)
+ cache.set(className, problem)
return problem
}
@@ -133,14 +165,14 @@ export const noUnknownTailwindClassesRule = defineRule({
}
},
create(context) {
- const system = designSystem
- if (!system) return {}
const file = normalizedFilename(context)
+ const theme = themeFor(file)
+ if (!theme) return {}
const themeModule = file.startsWith('src/theme/') || file.includes('/src/theme/')
const check = (node: ESTree.Node, text: string) => {
for (const className of classNames(text)) {
- const problem = classProblem(system, className)
+ const problem = classProblem(theme, className)
if (problem) context.report({ node, message: `Tailwind class: ${problem}.` })
}
}
diff --git a/tools/checks/lint/tests/tailwind-classes.test.ts b/tools/checks/lint/tests/tailwind-classes.test.ts
index acd807369..393f03def 100644
--- a/tools/checks/lint/tests/tailwind-classes.test.ts
+++ b/tools/checks/lint/tests/tailwind-classes.test.ts
@@ -87,3 +87,17 @@ describe('no-unknown-tailwind-classes', () => {
expect(files.toSorted()).toEqual(['Bound.vue', 'Static.vue', 'variants.ts'])
})
})
+
+test('checks SDK examples against the docs stylesheet that builds them', async () => {
+ const diagnostics = ruleDiagnostics(
+ await lintFiles(
+ {
+ 'packages/vue/src/primitives/Demo/examples/AppToken.vue': `${SCRIPT}<template><p class="bg-panel" /></template>\n`,
+ 'packages/vue/src/primitives/Demo/examples/Palette.vue': `${SCRIPT}<template><p class="bg-violet-600 text-[var(--vp-c-text-1)]" /></template>\n`
+ },
+ rules
+ ),
+ rule
+ )
+ expect(diagnostics.map((item) => item.filename?.split('/').pop())).toEqual(['AppToken.vue'])
+})