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.
@@ -4,9 +4,9 @@
4
4
  * GUARD_RULES: the closed registry of all 19 violation kinds.
5
5
  * checkSource: pure (text, relPath, cfg) → Finding[]. No FS, no walk, no console.
6
6
  */
7
- import type { Finding, GuardConfig, GuardKind } from './types'
7
+ import type { Finding, GuardConfig, GuardKind, GuardSeverity } from './types'
8
8
 
9
- export type { Finding, GuardConfig, GuardKind }
9
+ export type { Finding, GuardConfig, GuardKind, GuardSeverity }
10
10
 
11
11
  // ── Static regex consts ──────────────────────────────────────────────────────────────────────────
12
12
 
@@ -64,9 +64,37 @@ const INLINE_STYLE = /style=\{\{/
64
64
  const LAYOUT_SURFACE_PROP =
65
65
  /\b(?:display|padding|margin|gap|flex|grid|border|background|width|height)\b/
66
66
 
67
- // Spacing/sizing literals in inline style={{}} — anchored on property name; var() or 0 pass.
68
- const INLINE_SPACING =
69
- /\b(?:padding(?:Top|Bottom|Left|Right)?|margin(?:Top|Bottom|Left|Right)?|gap|rowGap|columnGap)\s*:\s*(?!var\()['"]?-?(?!0\b)\d/g
67
+ // Spacing/sizing literals — anchored on the property name; `var()` and a plain `0` pass.
68
+ //
69
+ // Composed rather than written out because it must cover BOTH spellings of the same property: the
70
+ // camelCase form a TSX inline style uses (`paddingInlineStart`) and the kebab form CSS uses
71
+ // (`padding-inline-start`). Enumerating one and not the other is how `padding: 18px` came to flag in
72
+ // a CSS module while `padding-top: 18px` beside it did not — a distinction shaped by regex history
73
+ // rather than by any decision, and invisible from the author's seat.
74
+ //
75
+ // Case-insensitivity would be the short way to unify them and is deliberately not used: it would
76
+ // also match `Padding`/`PADDING`, neither of which is a property in either dialect.
77
+ const BOX_SIDE = '(?:Top|Bottom|Left|Right|Block|Inline)(?:Start|End)?'
78
+ const BOX_SIDE_KEBAB = '(?:top|bottom|left|right|block|inline)(?:-(?:start|end))?'
79
+ const SPACING_PROP = `(?:padding|margin)(?:${BOX_SIDE}|-${BOX_SIDE_KEBAB})?|gap|rowGap|columnGap|row-gap|column-gap`
80
+ // A bare `0` needs no unit and no token, so it is never a finding — but the guard for it has to
81
+ // stop at the zero ITSELF. `(?!0\b)` did not: `\b` sits between `0` and `.`, so every value
82
+ // starting `0.` matched the guard and dropped out of the scan entirely. `0.75rem` is 12px, a real
83
+ // spacing value, and it was invisible. `(?!0(?![.\d]))` excuses only a zero that ends there.
84
+ //
85
+ // The number itself is `[-+]?(?:\d|\.\d)`, not `-?\d`: CSS lets a value carry an explicit `+` and
86
+ // omit the integer part, so `.75rem` and `+12px` are the same spacing as `0.75rem` and `12px`.
87
+ // Requiring a leading digit skipped both — the same silent-skip the zero guard above was fixed for,
88
+ // one spelling over.
89
+ //
90
+ // The property is preceded by `(?<![\w-])`, not `\b`: a `\b` boundary sits inside a hyphenated
91
+ // identifier, so every custom property whose NAME ends in a spacing word — the token layer's own
92
+ // `--vx-space-article-header-padding-bottom`, a consumer's `--card-padding-inline` — matched as if
93
+ // it were a declaration. A custom property is a definition, not a rendered spacing decision.
94
+ const INLINE_SPACING = new RegExp(
95
+ `(?<![\\w-])(?:${SPACING_PROP})\\s*:\\s*(?!var\\()['"]?[-+]?(?!0(?![.\\d]))(?:\\d|\\.\\d)`,
96
+ 'g',
97
+ )
70
98
 
71
99
  /**
72
100
  * The sub-scale micro-spacing ceiling, in px — the largest literal `inline-spacing` tolerates in a
@@ -86,6 +114,9 @@ const INLINE_SPACING =
86
114
  */
87
115
  const MICRO_SPACING_CEILING_PX = 10
88
116
 
117
+ /** The root font size `rem` values resolve against — the same 16 the token layer's `pxRem` uses. */
118
+ const ROOT_FONT_SIZE_PX = 16
119
+
89
120
  /**
90
121
  * Is this CSS declaration entirely sub-scale? True when every literal in the value is a bare px (or
91
122
  * unitless) number at or below {@link MICRO_SPACING_CEILING_PX}. `var(...)` components are dropped
@@ -93,8 +124,13 @@ const MICRO_SPACING_CEILING_PX = 10
93
124
  * already tokenized where it counts.
94
125
  *
95
126
  * Multi-value shorthands are judged as a whole: `padding: 4px 8px` passes, `padding: 4px 16px` does
96
- * not — the 16 has a scale stop and should use it. Any other unit (`rem`, `em`, `%`) fails, because
97
- * the doctrine is stated in px and a rem value is a different axis, not a micro-spacing one.
127
+ * not — the 16 has a scale stop and should use it.
128
+ *
129
+ * `rem` is resolved against the 16px root, the same conversion `pxRem` in the token layer applies,
130
+ * so `0.25rem` and `4px` get the same answer. Writing the identical spacing two ways and having the
131
+ * guard accept one is the same arbitrariness the kebab gap produced. Units that cannot be resolved
132
+ * to px without layout context — `em`, `%`, `ch`, `vw` — are not micro-spacing claims and still
133
+ * flag.
98
134
  */
99
135
  function isSubScaleCssSpacing(line: string, matchIndex: number): boolean {
100
136
  const colon = line.indexOf(':', matchIndex)
@@ -104,8 +140,10 @@ function isSubScaleCssSpacing(line: string, matchIndex: number): boolean {
104
140
  const value = (end === -1 ? rest : rest.slice(0, end)).replace(/var\([^)]*\)/g, ' ').trim()
105
141
  if (value === '') return true
106
142
  return value.split(/\s+/).every((part) => {
107
- const literal = /^-?(\d+(?:\.\d+)?)(?:px)?$/.exec(part)
108
- return literal !== null && Number(literal[1]) <= MICRO_SPACING_CEILING_PX
143
+ const literal = /^[-+]?(\d+(?:\.\d+)?|\.\d+)(px|rem)?$/.exec(part)
144
+ if (literal === null) return false
145
+ const px = literal[2] === 'rem' ? Number(literal[1]) * ROOT_FONT_SIZE_PX : Number(literal[1])
146
+ return px <= MICRO_SPACING_CEILING_PX
109
147
  })
110
148
  }
111
149
 
@@ -208,6 +246,29 @@ export const DEFAULT_GUARD_CONFIG: GuardConfig = {
208
246
  sub16InputFont: true,
209
247
  allowComment: 'theme-allow',
210
248
  exemptRules: {},
249
+ severity: {},
250
+ }
251
+
252
+ /**
253
+ * Kinds still inside their grace minor — reported, not fatal, until the next minor promotes them.
254
+ * See {@link GuardSeverity} for the doctrine and why basalt-ui in particular needs it.
255
+ *
256
+ * The value is the promotion note, so the entry carries its own expiry rather than relying on
257
+ * someone remembering. Adding a kind here is part of shipping it; removing the entry IS the
258
+ * promotion, and belongs in its own commit so the changelog says enforcement got stricter.
259
+ *
260
+ * Empty today: every shipped kind is past its grace period. The next new kind goes here first.
261
+ *
262
+ * @example
263
+ * const GRACE: Partial<Record<GuardKind, string>> = {
264
+ * 'raw-font-family': 'introduced 1.4.0 — promote to error in 1.5.0',
265
+ * }
266
+ */
267
+ const GRACE_PERIOD_KINDS: Partial<Record<GuardKind, string>> = {}
268
+
269
+ /** A kind's effective severity: consumer override first, then the grace table, then `error`. */
270
+ function severityOf(kind: GuardKind, cfg: GuardConfig): GuardSeverity {
271
+ return cfg.severity?.[kind] ?? (kind in GRACE_PERIOD_KINDS ? 'warn' : 'error')
211
272
  }
212
273
 
213
274
  // ── Path predicate ────────────────────────────────────────────────────────────────────────────────
@@ -672,7 +733,9 @@ function ruleApplies(kind: GuardKind, relPath: string): boolean {
672
733
  * if (findings.some((f) => f.kind === 'raw-hex')) { ... }
673
734
  */
674
735
  export function checkSource(text: string, relPath: string, cfg: GuardConfig): Finding[] {
675
- const findings: Finding[] = []
736
+ // Severity is stamped once at the end rather than at each of the ~20 push sites — it is a
737
+ // property of the KIND and the config, never of the match.
738
+ const findings: Omit<Finding, 'severity'>[] = []
676
739
 
677
740
  // Derive the 3 dynamic regexes via GUARD_RULES pattern builders.
678
741
  const forbiddenAccentRe = (
@@ -885,5 +948,7 @@ export function checkSource(text: string, relPath: string, cfg: GuardConfig): Fi
885
948
  // Per-rule, per-path exemption post-filter (§exemptRules) — applied once here so it uniformly
886
949
  // covers every kind regardless of whether it was emitted via the GUARD_RULES registry loop above
887
950
  // or one of the inline-handled kinds (raw-surface, raw-html-layout, sub-16-input-font, …).
888
- return findings.filter((f) => !isRuleExempt(f.kind, f.relPath, cfg.exemptRules))
951
+ return findings
952
+ .filter((f) => !isRuleExempt(f.kind, f.relPath, cfg.exemptRules))
953
+ .map((f) => ({ ...f, severity: severityOf(f.kind, cfg) }))
889
954
  }
@@ -28,6 +28,22 @@ export type GuardKind =
28
28
  | 'sub-16-input-font'
29
29
  | 'raw-font-family'
30
30
 
31
+ /**
32
+ * How hard a finding lands. `error` fails the build; `warn` reports and passes.
33
+ *
34
+ * The point of `warn` is the LANDING of a new rule, not a permanent tier. basalt-ui bans majors by
35
+ * design — 1.x absorbs breaks — so a consumer has no semver channel telling them enforcement got
36
+ * stricter, and the guard is the part of the framework that can hard-fail their build. A kind that
37
+ * arrives as an error therefore turns a routine minor upgrade into an unplanned refactor: 1.2.0
38
+ * cost the one real consumer eleven `theme-allow` comments, and 1.3.0 had them delete all eleven
39
+ * again — net zero source change, two commits, two production deploys.
40
+ *
41
+ * So: a change that makes the guard reject code it previously accepted — a new kind, or an existing
42
+ * kind reaching a new file type — ships `warn` for one minor, then promotes. The consumer sees it,
43
+ * schedules it, and upgrades on a green build.
44
+ */
45
+ export type GuardSeverity = 'warn' | 'error'
46
+
31
47
  /**
32
48
  * A structured finding — the chosen testable surface (§C.4). Replaces the old `Violation` type.
33
49
  * `relPath` + `line` + `token` + `kind` uniquely identify the violation.
@@ -41,6 +57,8 @@ export type Finding = {
41
57
  readonly line: number
42
58
  readonly token: string
43
59
  readonly kind: GuardKind
60
+ /** Resolved per kind from {@link GuardConfig.severity} over the shipped default. */
61
+ readonly severity: GuardSeverity
44
62
  }
45
63
 
46
64
  /**
@@ -136,4 +154,21 @@ export type GuardConfig = {
136
154
  * { exemptRules: { 'inline-display': ['agent'] } } // inline-display never fires under src/agent/**
137
155
  */
138
156
  readonly exemptRules: Partial<Record<GuardKind, string[]>>
157
+ /**
158
+ * Per-kind severity override. A kind absent here takes the shipped default, which is `error`
159
+ * for every kind past its grace minor — see {@link GuardSeverity} for why a grace minor exists.
160
+ *
161
+ * Two directions, both legitimate:
162
+ * • `'warn'` on an `error` kind — a consumer who wants to upgrade now and fix later, on their
163
+ * own schedule rather than the release's.
164
+ * • `'error'` on a grace-period kind — a consumer who would rather take the enforcement
165
+ * immediately than carry a warning for a minor.
166
+ *
167
+ * This is severity, NOT an off switch: every kind already has its own boolean in this config,
168
+ * and `exemptRules` scopes a kind to paths. A kind turned down to `warn` still reports.
169
+ *
170
+ * @example
171
+ * { severity: { 'inline-spacing': 'warn' } } // report, don't fail, while we migrate
172
+ */
173
+ readonly severity: Partial<Record<GuardKind, GuardSeverity>>
139
174
  }