basalt-ui 1.3.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/agent/rules/basalt-tokens.md +7 -5
- package/configs/lefthook.yml +13 -6
- package/dist/cli/index.d.ts +21 -3
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +86 -18
- package/dist/guard/guard-hook.d.ts.map +1 -1
- package/dist/guard/guard-hook.js +3 -1
- package/dist/guard/index.d.ts +2 -2
- package/dist/guard/index.d.ts.map +1 -1
- package/dist/guard/index.js +19 -5
- package/dist/guard/types.d.ts +34 -0
- package/dist/guard/types.d.ts.map +1 -1
- package/llms.txt +2 -2
- package/package.json +1 -1
- package/src/cli/check-theme.test.ts +58 -0
- package/src/cli/index.ts +163 -22
- package/src/guard/check-source.test.ts +156 -0
- package/src/guard/guard-hook.test.ts +30 -0
- package/src/guard/guard-hook.ts +6 -1
- package/src/guard/index.ts +76 -11
- package/src/guard/types.ts +35 -0
package/src/cli/index.ts
CHANGED
|
@@ -33,11 +33,11 @@ import { dirname, isAbsolute, relative, resolve } from 'node:path'
|
|
|
33
33
|
import { fileURLToPath } from 'node:url'
|
|
34
34
|
|
|
35
35
|
import { checkSource, DEFAULT_GUARD_CONFIG, GUARD_RULES } from '../guard'
|
|
36
|
-
import type { Finding, GuardConfig, GuardKind } from '../guard'
|
|
36
|
+
import type { Finding, GuardConfig, GuardKind, GuardSeverity } from '../guard'
|
|
37
37
|
import { evaluateGuardHook } from '../guard/guard-hook'
|
|
38
38
|
import { RULE_NAMES, SKILL_NAMES, SURFACES, TOKEN_LAYER_BOUNDARY_SURFACES } from '../surfaces'
|
|
39
39
|
import type { DoctrineSpec, SurfaceSpec } from '../surfaces'
|
|
40
|
-
import { buildPaletteCss } from '../tokens'
|
|
40
|
+
import { buildPaletteCss, deriveSpacing } from '../tokens'
|
|
41
41
|
|
|
42
42
|
/** Shape of the optional `"basalt"` key in a consumer's package.json. */
|
|
43
43
|
export type BasaltConfig = {
|
|
@@ -47,6 +47,9 @@ export type BasaltConfig = {
|
|
|
47
47
|
exempt?: string[]
|
|
48
48
|
/** Named spacing-scale steps (px) flagged when used as a raw spacing prop. Default: 10/12/16/20/32. */
|
|
49
49
|
spacingSteps?: number[]
|
|
50
|
+
/** Per-kind severity override — `'warn'` reports without failing, `'error'` fails the build.
|
|
51
|
+
* See `GuardSeverity` in ../guard/types for the grace-minor doctrine behind it. */
|
|
52
|
+
severity?: Partial<Record<GuardKind, GuardSeverity>>
|
|
50
53
|
/** Off-identity Mantine accent families forbidden as chrome accents. Default: argo's set. */
|
|
51
54
|
forbiddenAccents?: string[]
|
|
52
55
|
/** Earned accent hue recorded in DESIGN.md `{{ACCENT_HUE}}`. Default: `blue`. */
|
|
@@ -207,12 +210,46 @@ const SKIP = /\.gen\.ts$|\.test\.[tj]sx?$|\.d\.ts$/
|
|
|
207
210
|
/** Filenames that look like they ARE the palette source, for the check-theme escape-hatch hint. */
|
|
208
211
|
const SERIES_MODULE_HINT_RE = /(series|palette)\.tsx?$/i
|
|
209
212
|
|
|
213
|
+
const SEVERITY_VALUES: readonly string[] = ['warn', 'error']
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Drop `basalt.severity` entries whose value is not `'warn'` / `'error'`, loudly.
|
|
217
|
+
*
|
|
218
|
+
* This is the one config field where a typo REMOVES enforcement rather than failing: package.json
|
|
219
|
+
* is untyped at runtime, and `"warning"` would stamp findings with a value matching neither the
|
|
220
|
+
* error bucket (which fails the build) nor the warn bucket (which prints them) — the kind would
|
|
221
|
+
* vanish and `check-theme` would exit 0 as if the tree were clean. Dropping the entry falls the
|
|
222
|
+
* kind back to its default, so the failure direction of a typo is "stricter than you meant, and
|
|
223
|
+
* told about it" instead of "silently unguarded".
|
|
224
|
+
*/
|
|
225
|
+
function sanitizeSeverity(cfg: BasaltConfig): BasaltConfig {
|
|
226
|
+
const raw: unknown = cfg.severity
|
|
227
|
+
if (raw === undefined) return cfg
|
|
228
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
229
|
+
console.error('⚠ basalt.severity is not an object — ignoring it; every kind keeps its default.')
|
|
230
|
+
const { severity: _dropped, ...rest } = cfg
|
|
231
|
+
return rest
|
|
232
|
+
}
|
|
233
|
+
const kept: Record<string, GuardSeverity> = {}
|
|
234
|
+
for (const [kind, value] of Object.entries(raw)) {
|
|
235
|
+
if (typeof value === 'string' && SEVERITY_VALUES.includes(value)) {
|
|
236
|
+
kept[kind] = value as GuardSeverity
|
|
237
|
+
continue
|
|
238
|
+
}
|
|
239
|
+
console.error(
|
|
240
|
+
`⚠ basalt.severity["${kind}"] is ${JSON.stringify(value)}, not 'warn' or 'error' — ` +
|
|
241
|
+
'ignoring it; that kind keeps its default severity.',
|
|
242
|
+
)
|
|
243
|
+
}
|
|
244
|
+
return { ...cfg, severity: kept as Partial<Record<GuardKind, GuardSeverity>> }
|
|
245
|
+
}
|
|
246
|
+
|
|
210
247
|
function readBasaltConfig(cwd: string): BasaltConfig {
|
|
211
248
|
try {
|
|
212
249
|
const pkg = JSON.parse(readFileSync(resolve(cwd, 'package.json'), 'utf8')) as {
|
|
213
250
|
basalt?: BasaltConfig
|
|
214
251
|
}
|
|
215
|
-
return pkg.basalt ?? {}
|
|
252
|
+
return sanitizeSeverity(pkg.basalt ?? {})
|
|
216
253
|
} catch {
|
|
217
254
|
return {}
|
|
218
255
|
}
|
|
@@ -325,6 +362,7 @@ export function checkTheme(cwd: string = process.cwd()): number {
|
|
|
325
362
|
sub16InputFont: cfg.sub16InputFont ?? DEFAULT_GUARD_CONFIG.sub16InputFont,
|
|
326
363
|
allowComment: 'theme-allow',
|
|
327
364
|
exemptRules: cfg.exemptRules ?? DEFAULT_GUARD_CONFIG.exemptRules,
|
|
365
|
+
severity: cfg.severity ?? DEFAULT_GUARD_CONFIG.severity,
|
|
328
366
|
}
|
|
329
367
|
|
|
330
368
|
const findings: Finding[] = []
|
|
@@ -365,20 +403,43 @@ export function checkTheme(cwd: string = process.cwd()): number {
|
|
|
365
403
|
return 0
|
|
366
404
|
}
|
|
367
405
|
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
406
|
+
// Errors and warnings are reported as two separate blocks, not one list with a column: a
|
|
407
|
+
// consumer scanning the output needs "what breaks my build" answered before "what will later".
|
|
408
|
+
const errors = findings.filter((f) => f.severity === 'error')
|
|
409
|
+
const warnings = findings.filter((f) => f.severity === 'warn')
|
|
410
|
+
|
|
411
|
+
const report = (group: Finding[], heading: string): void => {
|
|
412
|
+
const byFile = new Map<string, Finding[]>()
|
|
413
|
+
for (const f of group) {
|
|
414
|
+
const list = byFile.get(f.relPath) ?? []
|
|
415
|
+
list.push(f)
|
|
416
|
+
byFile.set(f.relPath, list)
|
|
379
417
|
}
|
|
380
|
-
console.error(
|
|
418
|
+
console.error(heading)
|
|
419
|
+
for (const [file, vs] of [...byFile].toSorted()) {
|
|
420
|
+
console.error(file)
|
|
421
|
+
for (const v of vs.toSorted((a, b) => a.line - b.line)) {
|
|
422
|
+
console.error(` ${String(v.line).padStart(4)} ${v.kind.padEnd(22)} ${v.token}`)
|
|
423
|
+
}
|
|
424
|
+
console.error('')
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
if (warnings.length > 0) {
|
|
429
|
+
report(
|
|
430
|
+
warnings,
|
|
431
|
+
// Deliberately neutral about WHY a kind is a warning. Two origins reach this block — a kind
|
|
432
|
+
// in its grace minor, and a consumer's own `basalt.severity` override — and the finding
|
|
433
|
+
// doesn't carry which. Promising promotion next minor would be wrong for the second.
|
|
434
|
+
`⚠ Theme guard: ${warnings.length} warning(s) — reported, not fatal. A kind warns while it ` +
|
|
435
|
+
'is in its grace minor, or because `basalt.severity` turned it down; fix on your own ' +
|
|
436
|
+
'schedule.\n',
|
|
437
|
+
)
|
|
381
438
|
}
|
|
439
|
+
if (errors.length > 0) {
|
|
440
|
+
report(errors, `✖ Theme guard: ${errors.length} off-palette / off-identity violation(s)\n`)
|
|
441
|
+
}
|
|
442
|
+
|
|
382
443
|
const presentKinds = new Set(findings.map((f) => f.kind))
|
|
383
444
|
const explanations = (Object.keys(GUARD_KIND_EXPLANATIONS) as GuardKind[])
|
|
384
445
|
.filter((kind) => presentKinds.has(kind))
|
|
@@ -392,13 +453,15 @@ export function checkTheme(cwd: string = process.cwd()): number {
|
|
|
392
453
|
|
|
393
454
|
// A violation in a file that looks like the palette source itself is likely intentional — point
|
|
394
455
|
// at the exempt escape hatch instead of leaving the author to search for it.
|
|
395
|
-
if (
|
|
456
|
+
if (findings.some((f) => SERIES_MODULE_HINT_RE.test(f.relPath))) {
|
|
396
457
|
console.error(
|
|
397
458
|
'Hint: if a flagged file IS your palette/series source, exempt it via ' +
|
|
398
459
|
'"basalt": { "exempt": ["<path>"] } in package.json.',
|
|
399
460
|
)
|
|
400
461
|
}
|
|
401
|
-
|
|
462
|
+
// Warnings alone pass. That is the whole point of the grace minor: a consumer takes the upgrade
|
|
463
|
+
// on a green build and schedules the fix, instead of the release scheduling it for them.
|
|
464
|
+
return errors.length > 0 ? 1 : 0
|
|
402
465
|
}
|
|
403
466
|
|
|
404
467
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
@@ -743,6 +806,19 @@ type Manifest = {
|
|
|
743
806
|
basaltVersion?: string
|
|
744
807
|
/** dest path → sha256 of the managed unit (file bytes, block body, or stanza) at last write. */
|
|
745
808
|
files: Record<string, string>
|
|
809
|
+
/**
|
|
810
|
+
* The resolved level-0 spacing scale (`xs`..`xl`, px) at last write — `doctor`'s second
|
|
811
|
+
* reconciliation axis, and the only one that reports a change in RENDERED OUTPUT rather than in
|
|
812
|
+
* placed files.
|
|
813
|
+
*
|
|
814
|
+
* A retune of the spacing bases moves every surface in every consumer calling
|
|
815
|
+
* `createBasaltTheme()` bare. basalt-ui bans majors by design, so the changelog is the only
|
|
816
|
+
* channel that carries that, and a one-line `feat:` subject cannot convey blast radius: the
|
|
817
|
+
* 1.2.0 retune shipped as "tighten the sidebar, open up components" and went unverified in
|
|
818
|
+
* production for a day because nothing said "every padding moved". Recording the scale makes the
|
|
819
|
+
* move mechanically visible on the next `doctor` run instead of depending on a subject line.
|
|
820
|
+
*/
|
|
821
|
+
spacingScale?: Record<string, number>
|
|
746
822
|
}
|
|
747
823
|
|
|
748
824
|
function readManifest(cwd: string): Manifest {
|
|
@@ -752,6 +828,20 @@ function readManifest(cwd: string): Manifest {
|
|
|
752
828
|
const parsed = JSON.parse(raw) as Partial<Manifest>
|
|
753
829
|
const manifest: Manifest = { version: 1, files: parsed.files ?? {} }
|
|
754
830
|
if (typeof parsed.basaltVersion === 'string') manifest.basaltVersion = parsed.basaltVersion
|
|
831
|
+
// `typeof null === 'object'` and so is an array — a hand-edited manifest carrying either would
|
|
832
|
+
// reach doctor and be indexed as a record. Values must be numbers too: a `"4"` would never
|
|
833
|
+
// `===` the derived `4`, so doctor would report every step as moved. Anything else is treated
|
|
834
|
+
// as unrecorded, which reads as "run sync" rather than as a garbled diff.
|
|
835
|
+
const scale: unknown = parsed.spacingScale
|
|
836
|
+
if (
|
|
837
|
+
scale !== null &&
|
|
838
|
+
scale !== undefined &&
|
|
839
|
+
typeof scale === 'object' &&
|
|
840
|
+
!Array.isArray(scale) &&
|
|
841
|
+
Object.values(scale).every((v) => typeof v === 'number')
|
|
842
|
+
) {
|
|
843
|
+
manifest.spacingScale = scale as Record<string, number>
|
|
844
|
+
}
|
|
755
845
|
return manifest
|
|
756
846
|
} catch {
|
|
757
847
|
return { version: 1, files: {} }
|
|
@@ -973,6 +1063,7 @@ export function init(cwd: string = process.cwd(), scaffoldFlags: ScaffoldFlags =
|
|
|
973
1063
|
}
|
|
974
1064
|
|
|
975
1065
|
manifest.basaltVersion = ctx.vars.BASALT_VERSION
|
|
1066
|
+
manifest.spacingScale = { ...deriveSpacing(0).scale }
|
|
976
1067
|
writeFileEnsuringDir(resolve(cwd, MANIFEST_PATH), `${JSON.stringify(manifest, null, 2)}\n`)
|
|
977
1068
|
|
|
978
1069
|
console.log(`basalt-ui init: ${written} written, ${skipped} kept, manifest at ${MANIFEST_PATH}`)
|
|
@@ -1095,6 +1186,7 @@ export function sync(opts: SyncOptions = {}, cwd: string = process.cwd()): numbe
|
|
|
1095
1186
|
}
|
|
1096
1187
|
|
|
1097
1188
|
manifest.basaltVersion = ctx.vars.BASALT_VERSION
|
|
1189
|
+
manifest.spacingScale = { ...deriveSpacing(0).scale }
|
|
1098
1190
|
writeFileEnsuringDir(resolve(cwd, MANIFEST_PATH), `${JSON.stringify(manifest, null, 2)}\n`)
|
|
1099
1191
|
|
|
1100
1192
|
console.log(
|
|
@@ -1324,9 +1416,11 @@ type DoctorResult = {
|
|
|
1324
1416
|
* 2. The installed node_modules/basalt-ui version matches the manifest's basaltVersion
|
|
1325
1417
|
* (a mismatch means the package was upgraded but `sync` never ran — the placed doctrine is
|
|
1326
1418
|
* stale).
|
|
1327
|
-
* 3. The
|
|
1419
|
+
* 3. The spacing scale still matches the one stamped into the manifest at the last init/sync
|
|
1420
|
+
* (skipped when this CLI is demonstrably not the installed package — see the check).
|
|
1421
|
+
* 4. The running CLI's own version matches the installed basalt-ui (catches a stale
|
|
1328
1422
|
* `bunx basalt-ui` npm fetch; best-effort, skipped if node_modules is absent).
|
|
1329
|
-
*
|
|
1423
|
+
* 5. `basaltAppPlugin`'s (basalt-ui/vite) default icon filenames exist under public/ (only when
|
|
1330
1424
|
* a public/ dir exists at all — skipped otherwise).
|
|
1331
1425
|
*
|
|
1332
1426
|
* Returns the exit code: 0 = all good, 1 = one or more hard failures.
|
|
@@ -1386,11 +1480,57 @@ export function doctor(cwd: string = process.cwd()): number {
|
|
|
1386
1480
|
}
|
|
1387
1481
|
}
|
|
1388
1482
|
|
|
1389
|
-
|
|
1483
|
+
const cliVersion = readFrameworkVersion(packageRoot())
|
|
1484
|
+
|
|
1485
|
+
// ── Warn check 3: the spacing scale has not moved under the app ────────────
|
|
1486
|
+
// The only check here that reports a change in RENDERED OUTPUT rather than in placed files. A
|
|
1487
|
+
// retune of the spacing bases moves every surface in an app calling `createBasaltTheme()` bare,
|
|
1488
|
+
// and majors are banned by design, so nothing in the version number says so — 1.2.0's retune
|
|
1489
|
+
// shipped under "tighten the sidebar, open up components" and sat unverified in production for a
|
|
1490
|
+
// day. Diffing the current scale against what last synced makes that move something a consumer
|
|
1491
|
+
// is told rather than something they have to notice.
|
|
1492
|
+
//
|
|
1493
|
+
// The scale that matters is the INSTALLED package's — that is what the app renders with — but
|
|
1494
|
+
// `deriveSpacing` here is the RUNNING CLI's, which a stale `bunx basalt-ui` fetch makes a
|
|
1495
|
+
// different package. Rather than load the installed tokens entry (async, and doctor is sync),
|
|
1496
|
+
// the comparison is skipped whenever the two versions are known to disagree: a false "matches"
|
|
1497
|
+
// is worse than no answer. Check 4 below names that disagreement, so this stays a neutral note.
|
|
1498
|
+
const cliIsTheInstall = installedVersion === null || installedVersion === cliVersion
|
|
1499
|
+
if (manifestExists && !cliIsTheInstall) {
|
|
1500
|
+
pass(
|
|
1501
|
+
`spacing scale not compared — this CLI (${cliVersion}) is not the installed basalt-ui ` +
|
|
1502
|
+
`(${installedVersion}), so its scale is not the one the app renders with`,
|
|
1503
|
+
)
|
|
1504
|
+
} else if (manifestExists) {
|
|
1505
|
+
const recorded = readManifest(cwd).spacingScale
|
|
1506
|
+
const current = deriveSpacing(0).scale as Record<string, number>
|
|
1507
|
+
if (recorded === undefined) {
|
|
1508
|
+
// Silent for a manifest written before this field existed — the version check above already
|
|
1509
|
+
// says "run sync", and a second warning for the same cause is noise.
|
|
1510
|
+
pass('spacing scale not yet recorded — `basalt-ui sync` will stamp it')
|
|
1511
|
+
} else {
|
|
1512
|
+
// Over the UNION of both key sets: a step the scale gained since the last sync is a move the
|
|
1513
|
+
// app feels, and so is one it lost. Iterating only `current` would miss the second.
|
|
1514
|
+
const steps = [...new Set([...Object.keys(current), ...Object.keys(recorded)])].toSorted()
|
|
1515
|
+
const moved = steps
|
|
1516
|
+
.filter((step) => recorded[step] !== current[step])
|
|
1517
|
+
.map((step) => `${step} ${recorded[step] ?? '(none)'}→${current[step] ?? '(removed)'}`)
|
|
1518
|
+
if (moved.length > 0) {
|
|
1519
|
+
warn(
|
|
1520
|
+
`the spacing scale moved since the last sync (${moved.join(', ')}) — every surface in an ` +
|
|
1521
|
+
'app calling createBasaltTheme() bare shifts with it. Review the app visually, then run ' +
|
|
1522
|
+
'`basalt-ui sync` to record the new scale.',
|
|
1523
|
+
)
|
|
1524
|
+
} else {
|
|
1525
|
+
pass('spacing scale matches the last sync')
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
|
|
1530
|
+
// ── Warn check 4: running CLI version matches the consumer's installed basalt-ui ────
|
|
1390
1531
|
// Catches the failure mode where bunx fetches a stale published package instead of the local
|
|
1391
1532
|
// install — the CLI that ran doctor and the package resolved from the consumer's node_modules
|
|
1392
1533
|
// silently disagree.
|
|
1393
|
-
const cliVersion = readFrameworkVersion(packageRoot())
|
|
1394
1534
|
if (installedVersion !== null) {
|
|
1395
1535
|
if (installedVersion !== cliVersion) {
|
|
1396
1536
|
warn(
|
|
@@ -1403,7 +1543,7 @@ export function doctor(cwd: string = process.cwd()): number {
|
|
|
1403
1543
|
}
|
|
1404
1544
|
}
|
|
1405
1545
|
|
|
1406
|
-
// ── Warn check
|
|
1546
|
+
// ── Warn check 5: basaltAppPlugin's default icon files exist in public/ ────
|
|
1407
1547
|
// basalt-ui/vite's basaltAppPlugin (head/manifest metadata) references these filenames by
|
|
1408
1548
|
// default (the realfavicongenerator convention). Only runs when a public/ dir exists at all —
|
|
1409
1549
|
// apps that don't use the plugin, or don't use Vite's public-dir convention, get no false
|
|
@@ -1696,6 +1836,7 @@ export async function guardHook(cwd: string = process.cwd()): Promise<number> {
|
|
|
1696
1836
|
sub16InputFont: cfg.sub16InputFont ?? DEFAULT_GUARD_CONFIG.sub16InputFont,
|
|
1697
1837
|
allowComment: 'theme-allow',
|
|
1698
1838
|
exemptRules: cfg.exemptRules ?? DEFAULT_GUARD_CONFIG.exemptRules,
|
|
1839
|
+
severity: cfg.severity ?? DEFAULT_GUARD_CONFIG.severity,
|
|
1699
1840
|
}
|
|
1700
1841
|
|
|
1701
1842
|
// Honor the consumer's roots / exempt / skip config so the hook never blocks edits to exempted
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* src/cli/check-theme.test.ts (temp-dir + exit-code contract).
|
|
9
9
|
*/
|
|
10
10
|
import { describe, expect, it } from 'bun:test'
|
|
11
|
+
import { pxRem } from '../tokens'
|
|
11
12
|
import { SPACE_SCALE } from '../tokens/palette'
|
|
12
13
|
import { checkSource, DEFAULT_GUARD_CONFIG } from './index'
|
|
13
14
|
import type { Finding, GuardKind } from './types'
|
|
@@ -1172,6 +1173,34 @@ describe('CSS applicability', () => {
|
|
|
1172
1173
|
expect(cssSpacing('padding: 2em')).toEqual(['inline-spacing'])
|
|
1173
1174
|
})
|
|
1174
1175
|
|
|
1176
|
+
it('reads a CSS number without its integer part, and one carrying an explicit +', () => {
|
|
1177
|
+
// `.75rem` IS `0.75rem` and `+12px` IS `12px`. Requiring a leading digit skipped both — the
|
|
1178
|
+
// same silent skip the `0.` guard was fixed for, one spelling over.
|
|
1179
|
+
expect(cssSpacing('padding: .75rem')).toEqual(['inline-spacing'])
|
|
1180
|
+
expect(cssSpacing('padding: +12px')).toEqual(['inline-spacing'])
|
|
1181
|
+
// …and the sub-scale escape resolves them the same way: .5rem is 8px, below the floor.
|
|
1182
|
+
expect(cssSpacing('padding: .5rem')).toEqual([])
|
|
1183
|
+
})
|
|
1184
|
+
|
|
1185
|
+
it('does NOT read a custom property whose NAME ends in a spacing word', () => {
|
|
1186
|
+
// A `\b` boundary sits inside a hyphenated identifier, so these matched as declarations. The
|
|
1187
|
+
// first is real generated output from the token layer; the second is the shape a consumer
|
|
1188
|
+
// writes. A custom property is a definition, not a rendered spacing decision.
|
|
1189
|
+
expect(cssSpacing('--vx-space-article-header-padding-bottom: 20px')).toEqual([])
|
|
1190
|
+
expect(cssSpacing('--card-padding-inline: 20px')).toEqual([])
|
|
1191
|
+
expect(cssSpacing('--card-gap: 20px')).toEqual([])
|
|
1192
|
+
// The declaration itself still flags — the fix narrows the boundary, it does not disarm it.
|
|
1193
|
+
expect(cssSpacing('padding-bottom: 20px')).toEqual(['inline-spacing'])
|
|
1194
|
+
expect(cssSpacing('padding-inline: 20px')).toEqual(['inline-spacing'])
|
|
1195
|
+
})
|
|
1196
|
+
|
|
1197
|
+
it('stays case-sensitive — neither dialect spells a property `Padding-Top`', () => {
|
|
1198
|
+
// Pins the decision documented above BOX_SIDE_KEBAB: unifying the two spellings with an `i`
|
|
1199
|
+
// flag would be the short way and would also match casings that are not properties at all.
|
|
1200
|
+
expect(cssSpacing('Padding-Top: 18px')).toEqual([])
|
|
1201
|
+
expect(cssSpacing('PADDING: 18px')).toEqual([])
|
|
1202
|
+
})
|
|
1203
|
+
|
|
1175
1204
|
it('keeps the default spacing steps in lockstep with the real scale', () => {
|
|
1176
1205
|
// The guard's copy of the xs..xl ladder drifted once already (it still read 10/12/16/20/32
|
|
1177
1206
|
// after the level-0 retune, so `p={16}` was flagged in favour of a `p="md"` worth 18, and the
|
|
@@ -1180,6 +1209,19 @@ describe('CSS applicability', () => {
|
|
|
1180
1209
|
expect(DEFAULT_GUARD_CONFIG.spacingSteps).toEqual(Object.values(SPACE_SCALE))
|
|
1181
1210
|
})
|
|
1182
1211
|
|
|
1212
|
+
it('resolves rem against the same root the token layer converts against', () => {
|
|
1213
|
+
// The guard keeps its own copy of the 16px root to stay dependency-free. The spacing-steps copy
|
|
1214
|
+
// beside it drifted in production once; this pins the relationship rather than the number, so a
|
|
1215
|
+
// token layer that ever moved off a 16px root would fail here instead of silently disagreeing.
|
|
1216
|
+
// 10px is the ceiling; 12px is the first value above it. Expressed through `pxRem` so the
|
|
1217
|
+
// literals here follow the token layer's conversion rather than restating it.
|
|
1218
|
+
expect(cssSpacing(`padding: ${pxRem(10)}`)).toEqual([])
|
|
1219
|
+
expect(cssSpacing(`padding: ${pxRem(12)}`)).toEqual(['inline-spacing'])
|
|
1220
|
+
// The px spellings of the same two values must land identically.
|
|
1221
|
+
expect(cssSpacing('padding: 10px')).toEqual([])
|
|
1222
|
+
expect(cssSpacing('padding: 12px')).toEqual(['inline-spacing'])
|
|
1223
|
+
})
|
|
1224
|
+
|
|
1183
1225
|
it('keeps the ceiling below the smallest scale stop, so it can never strand a real token', () => {
|
|
1184
1226
|
// The relationship, not the number: if a density retune moves SPACE_SCALE.xs, this still holds.
|
|
1185
1227
|
expect(cssSpacing(`gap: ${SPACE_SCALE.xs}px`)).toEqual(['inline-spacing'])
|
|
@@ -1236,6 +1278,79 @@ describe('CSS applicability', () => {
|
|
|
1236
1278
|
)
|
|
1237
1279
|
expect(kinds(f)).not.toContain('localstorage-theme')
|
|
1238
1280
|
})
|
|
1281
|
+
|
|
1282
|
+
// ── Kebab longhands and logical properties ──────────────────────────────────
|
|
1283
|
+
//
|
|
1284
|
+
// The alternation was camelCase-only, so a CSS module got the scan for `padding` and not for
|
|
1285
|
+
// `padding-top` beside it. Nothing decided that; it fell out of a pattern written for TSX inline
|
|
1286
|
+
// styles. Logical properties are included because that is what modern CSS in this repo writes.
|
|
1287
|
+
|
|
1288
|
+
it('flags kebab longhands at scale values', () => {
|
|
1289
|
+
expect(cssSpacing(`padding-top: ${SPACE_SCALE.md}px`)).toEqual(['inline-spacing'])
|
|
1290
|
+
expect(cssSpacing(`margin-bottom: ${SPACE_SCALE.lg}px`)).toEqual(['inline-spacing'])
|
|
1291
|
+
expect(cssSpacing(`row-gap: ${SPACE_SCALE.lg}px`)).toEqual(['inline-spacing'])
|
|
1292
|
+
expect(cssSpacing(`column-gap: ${SPACE_SCALE.xl}px`)).toEqual(['inline-spacing'])
|
|
1293
|
+
})
|
|
1294
|
+
|
|
1295
|
+
it('flags logical properties, including the -start/-end forms', () => {
|
|
1296
|
+
for (const prop of [
|
|
1297
|
+
'padding-block',
|
|
1298
|
+
'padding-inline',
|
|
1299
|
+
'margin-block',
|
|
1300
|
+
'margin-inline',
|
|
1301
|
+
'padding-inline-start',
|
|
1302
|
+
'margin-block-end',
|
|
1303
|
+
]) {
|
|
1304
|
+
expect(cssSpacing(`${prop}: ${SPACE_SCALE.xl}px`)).toEqual(['inline-spacing'])
|
|
1305
|
+
}
|
|
1306
|
+
})
|
|
1307
|
+
|
|
1308
|
+
it('extends the sub-scale escape to the longhands too — same rule, both spellings', () => {
|
|
1309
|
+
expect(cssSpacing('padding-top: 4px')).toEqual([])
|
|
1310
|
+
expect(cssSpacing('margin-inline: 8px')).toEqual([])
|
|
1311
|
+
})
|
|
1312
|
+
|
|
1313
|
+
it('flags the camelCase logical properties in a TSX inline style', () => {
|
|
1314
|
+
expect(
|
|
1315
|
+
kinds(checkSource('<Box style={{ paddingInlineStart: 18 }} />', PATH, DEFAULT_GUARD_CONFIG)),
|
|
1316
|
+
).toContain('inline-spacing')
|
|
1317
|
+
})
|
|
1318
|
+
|
|
1319
|
+
// ── The `0.` blind spot ─────────────────────────────────────────────────────
|
|
1320
|
+
//
|
|
1321
|
+
// The zero-guard was `(?!0\b)`, and `\b` sits between `0` and `.` — so every value starting `0.`
|
|
1322
|
+
// matched the guard meant for a bare zero and dropped out of the scan entirely.
|
|
1323
|
+
|
|
1324
|
+
it('sees values starting `0.` at all', () => {
|
|
1325
|
+
// 12px — a real spacing value that was previously invisible, not merely tolerated.
|
|
1326
|
+
expect(cssSpacing('padding: 0.75rem')).toEqual(['inline-spacing'])
|
|
1327
|
+
})
|
|
1328
|
+
|
|
1329
|
+
it('still excuses a genuine zero', () => {
|
|
1330
|
+
expect(cssSpacing('padding: 0')).toEqual([])
|
|
1331
|
+
expect(cssSpacing('padding: 0px')).toEqual([])
|
|
1332
|
+
})
|
|
1333
|
+
|
|
1334
|
+
it('resolves rem against the 16px root, so both spellings of one value agree', () => {
|
|
1335
|
+
// 0.25rem IS 4px. Accepting one spelling and flagging the other is the same arbitrariness the
|
|
1336
|
+
// kebab gap produced.
|
|
1337
|
+
expect(cssSpacing('padding: 0.25rem')).toEqual([])
|
|
1338
|
+
expect(cssSpacing('padding: 4px')).toEqual([])
|
|
1339
|
+
// The ceiling lands in the same place either way: 0.625rem = 10px, 0.75rem = 12px.
|
|
1340
|
+
expect(cssSpacing('padding: 0.625rem')).toEqual([])
|
|
1341
|
+
expect(cssSpacing('padding: 0.75rem')).toEqual(['inline-spacing'])
|
|
1342
|
+
})
|
|
1343
|
+
|
|
1344
|
+
it('leaves units it cannot resolve without layout context alone', () => {
|
|
1345
|
+
for (const value of ['2em', '5%', '3ch', '2vw']) {
|
|
1346
|
+
expect(cssSpacing(`padding: ${value}`)).toEqual(['inline-spacing'])
|
|
1347
|
+
}
|
|
1348
|
+
})
|
|
1349
|
+
|
|
1350
|
+
it('keeps the mixed-value and var() rules unchanged under the wider pattern', () => {
|
|
1351
|
+
expect(cssSpacing('padding: 4px 16px')).toEqual(['inline-spacing'])
|
|
1352
|
+
expect(cssSpacing('padding-inline: 4px var(--vx-space-stack-xs)')).toEqual([])
|
|
1353
|
+
})
|
|
1239
1354
|
})
|
|
1240
1355
|
|
|
1241
1356
|
// ── exemptRules — per-rule, per-path guard exemptions ────────────────────────
|
|
@@ -1300,3 +1415,44 @@ describe('exemptRules', () => {
|
|
|
1300
1415
|
expect(kinds(f)).toContain('raw-html-layout')
|
|
1301
1416
|
})
|
|
1302
1417
|
})
|
|
1418
|
+
|
|
1419
|
+
// ── 20. severity ─────────────────────────────────────────────────────────────
|
|
1420
|
+
|
|
1421
|
+
describe('severity', () => {
|
|
1422
|
+
it('stamps every finding, defaulting to error', () => {
|
|
1423
|
+
const f = find(`<Box style={{ color: '#ff0000' }} />`)
|
|
1424
|
+
expect(f.length).toBeGreaterThan(0)
|
|
1425
|
+
expect(f.every((v) => v.severity === 'error')).toBe(true)
|
|
1426
|
+
})
|
|
1427
|
+
|
|
1428
|
+
it('honours a per-kind consumer override without suppressing the finding', () => {
|
|
1429
|
+
// Turning a kind down must still REPORT it — severity is not an off switch. The per-kind
|
|
1430
|
+
// booleans and `exemptRules` are the off switches, and they stay separate on purpose.
|
|
1431
|
+
const cfg = { ...DEFAULT_GUARD_CONFIG, severity: { 'raw-hex': 'warn' as const } }
|
|
1432
|
+
const f = checkSource(`<Box style={{ color: '#ff0000' }} />`, PATH, cfg)
|
|
1433
|
+
const hex = f.filter((v) => v.kind === 'raw-hex')
|
|
1434
|
+
expect(hex).toHaveLength(1)
|
|
1435
|
+
expect(hex[0]?.severity).toBe('warn')
|
|
1436
|
+
})
|
|
1437
|
+
|
|
1438
|
+
it('scopes the override to the named kind only', () => {
|
|
1439
|
+
const cfg = { ...DEFAULT_GUARD_CONFIG, severity: { 'raw-hex': 'warn' as const } }
|
|
1440
|
+
const f = checkSource(`<Box p={${SPACE_SCALE.md}} style={{ color: '#ff0000' }} />`, PATH, cfg)
|
|
1441
|
+
expect(f.find((v) => v.kind === 'raw-hex')?.severity).toBe('warn')
|
|
1442
|
+
expect(f.find((v) => v.kind === 'raw-spacing')?.severity).toBe('error')
|
|
1443
|
+
})
|
|
1444
|
+
|
|
1445
|
+
it('ships no kind in its grace period today', () => {
|
|
1446
|
+
// Every shipped kind is past its grace minor. This is a LEDGER, not a constraint: when a new
|
|
1447
|
+
// kind lands warn-only, this expectation changes in the same commit, and changing it back is
|
|
1448
|
+
// the promotion. A grace entry that outlives its minor shows up here as an unexplained diff.
|
|
1449
|
+
const cfg = DEFAULT_GUARD_CONFIG
|
|
1450
|
+
const everyKind = checkSource(
|
|
1451
|
+
`<Box p={${SPACE_SCALE.md}} radius={4} style={{ color: '#ff0000', gap: 3 }} />`,
|
|
1452
|
+
PATH,
|
|
1453
|
+
cfg,
|
|
1454
|
+
)
|
|
1455
|
+
expect(everyKind.length).toBeGreaterThan(0)
|
|
1456
|
+
expect(everyKind.every((v) => v.severity === 'error')).toBe(true)
|
|
1457
|
+
})
|
|
1458
|
+
})
|
|
@@ -170,4 +170,34 @@ describe('evaluateGuardHook', () => {
|
|
|
170
170
|
)
|
|
171
171
|
expect(result.permissionDecision).toBe('deny')
|
|
172
172
|
})
|
|
173
|
+
|
|
174
|
+
it('allows a write whose only findings are warnings', () => {
|
|
175
|
+
// A grace-minor kind must not block an edit, or the grace period would be the STRICTEST gate
|
|
176
|
+
// in the toolchain: CI green, hook refusing the write.
|
|
177
|
+
const result = evaluateGuardHook(
|
|
178
|
+
{
|
|
179
|
+
tool_name: 'Write',
|
|
180
|
+
tool_input: { file_path: 'src/Page.tsx', file_text: "const c = '#abc123'" },
|
|
181
|
+
},
|
|
182
|
+
{ ...DEFAULT_GUARD_CONFIG, severity: { 'raw-hex': 'warn' } },
|
|
183
|
+
)
|
|
184
|
+
expect(result.permissionDecision).toBe('allow')
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
it('denies on the error when a payload mixes warn and error kinds, listing only the error', () => {
|
|
188
|
+
const result = evaluateGuardHook(
|
|
189
|
+
{
|
|
190
|
+
tool_name: 'Write',
|
|
191
|
+
tool_input: {
|
|
192
|
+
file_path: 'src/Page.tsx',
|
|
193
|
+
file_text: "const C = () => <div style={{ color: '#abc123', gap: 12 }} />",
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
{ ...DEFAULT_GUARD_CONFIG, severity: { 'raw-hex': 'warn' } },
|
|
197
|
+
)
|
|
198
|
+
expect(result.permissionDecision).toBe('deny')
|
|
199
|
+
expect(result.reason).toContain('inline-spacing')
|
|
200
|
+
// The warn-tier kind is absent from the deny reason — it is not what blocked the write.
|
|
201
|
+
expect(result.reason).not.toContain('raw-hex')
|
|
202
|
+
})
|
|
173
203
|
})
|
package/src/guard/guard-hook.ts
CHANGED
|
@@ -106,7 +106,12 @@ export function evaluateGuardHook(
|
|
|
106
106
|
return { permissionDecision: 'allow' }
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
-
|
|
109
|
+
// Errors only. A `warn` kind is inside its grace minor — it must not block a write, or the
|
|
110
|
+
// grace period would be the strictest gate in the toolchain rather than the softest: `sync` and
|
|
111
|
+
// CI would pass while the PreToolUse hook refused the edit outright.
|
|
112
|
+
const findings = checkSource(target.text, target.relPath, cfg).filter(
|
|
113
|
+
(f) => f.severity === 'error',
|
|
114
|
+
)
|
|
110
115
|
if (findings.length === 0) return { permissionDecision: 'allow' }
|
|
111
116
|
|
|
112
117
|
const shown = findings.slice(0, 10)
|