basalt-ui 1.3.0 → 1.4.1

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,46 @@ 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 property is preceded by `(?<![\w-])`, not `\b`: a `\b` boundary sits inside a hyphenated
86
+ // identifier, so every custom property whose NAME ends in a spacing word — the token layer's own
87
+ // `--vx-space-article-header-padding-bottom`, a consumer's `--card-padding-inline` — matched as if
88
+ // it were a declaration. A custom property is a definition, not a rendered spacing decision.
89
+ //
90
+ // The number does two things at once. CSS lets a value carry an explicit `+` and omit the integer
91
+ // part, so `.75rem` and `+12px` are the same spacing as `0.75rem` and `12px`; requiring a leading
92
+ // digit skipped both — the same silent-skip the zero guard above was fixed for, one spelling over.
93
+ //
94
+ // But admitting a fractional part also admits values that are not lengths at all. A UNITLESS
95
+ // fraction is never a spacing literal: `padding: 4` in an inline style is 4px by React's convention,
96
+ // while `padding: 0.3` is a RATIO — visx's band padding (`scalePoint({ padding: 0.3 })`) found this,
97
+ // ten times over in one consumer, and the guard's own advice ("use p/m/gap with xs..xl") is
98
+ // meaningless for it. So the discriminator is the UNIT, not the digit shape: a fraction must carry
99
+ // one (`.75rem`), and a unitless number must be an integer (`padding: 4` — still 4px, still a
100
+ // finding). `0.3` matches neither and drops out. `(?=[a-z%])` covers every CSS unit without
101
+ // enumerating them, and the sign stays outside so the zero guard still sees `-0` as a zero.
102
+ const SPACING_NUMBER = '(?:\\d*\\.\\d+(?=[a-z%])|\\d+(?!\\.\\d))'
103
+ const INLINE_SPACING = new RegExp(
104
+ `(?<![\\w-])(?:${SPACING_PROP})\\s*:\\s*(?!var\\()['"]?[-+]?(?!0(?![.\\d]))${SPACING_NUMBER}`,
105
+ 'g',
106
+ )
70
107
 
71
108
  /**
72
109
  * The sub-scale micro-spacing ceiling, in px — the largest literal `inline-spacing` tolerates in a
@@ -86,6 +123,9 @@ const INLINE_SPACING =
86
123
  */
87
124
  const MICRO_SPACING_CEILING_PX = 10
88
125
 
126
+ /** The root font size `rem` values resolve against — the same 16 the token layer's `pxRem` uses. */
127
+ const ROOT_FONT_SIZE_PX = 16
128
+
89
129
  /**
90
130
  * Is this CSS declaration entirely sub-scale? True when every literal in the value is a bare px (or
91
131
  * unitless) number at or below {@link MICRO_SPACING_CEILING_PX}. `var(...)` components are dropped
@@ -93,8 +133,13 @@ const MICRO_SPACING_CEILING_PX = 10
93
133
  * already tokenized where it counts.
94
134
  *
95
135
  * 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.
136
+ * not — the 16 has a scale stop and should use it.
137
+ *
138
+ * `rem` is resolved against the 16px root, the same conversion `pxRem` in the token layer applies,
139
+ * so `0.25rem` and `4px` get the same answer. Writing the identical spacing two ways and having the
140
+ * guard accept one is the same arbitrariness the kebab gap produced. Units that cannot be resolved
141
+ * to px without layout context — `em`, `%`, `ch`, `vw` — are not micro-spacing claims and still
142
+ * flag.
98
143
  */
99
144
  function isSubScaleCssSpacing(line: string, matchIndex: number): boolean {
100
145
  const colon = line.indexOf(':', matchIndex)
@@ -104,8 +149,10 @@ function isSubScaleCssSpacing(line: string, matchIndex: number): boolean {
104
149
  const value = (end === -1 ? rest : rest.slice(0, end)).replace(/var\([^)]*\)/g, ' ').trim()
105
150
  if (value === '') return true
106
151
  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
152
+ const literal = /^[-+]?(\d+(?:\.\d+)?|\.\d+)(px|rem)?$/.exec(part)
153
+ if (literal === null) return false
154
+ const px = literal[2] === 'rem' ? Number(literal[1]) * ROOT_FONT_SIZE_PX : Number(literal[1])
155
+ return px <= MICRO_SPACING_CEILING_PX
109
156
  })
110
157
  }
111
158
 
@@ -208,6 +255,29 @@ export const DEFAULT_GUARD_CONFIG: GuardConfig = {
208
255
  sub16InputFont: true,
209
256
  allowComment: 'theme-allow',
210
257
  exemptRules: {},
258
+ severity: {},
259
+ }
260
+
261
+ /**
262
+ * Kinds still inside their grace minor — reported, not fatal, until the next minor promotes them.
263
+ * See {@link GuardSeverity} for the doctrine and why basalt-ui in particular needs it.
264
+ *
265
+ * The value is the promotion note, so the entry carries its own expiry rather than relying on
266
+ * someone remembering. Adding a kind here is part of shipping it; removing the entry IS the
267
+ * promotion, and belongs in its own commit so the changelog says enforcement got stricter.
268
+ *
269
+ * Empty today: every shipped kind is past its grace period. The next new kind goes here first.
270
+ *
271
+ * @example
272
+ * const GRACE: Partial<Record<GuardKind, string>> = {
273
+ * 'raw-font-family': 'introduced 1.4.0 — promote to error in 1.5.0',
274
+ * }
275
+ */
276
+ const GRACE_PERIOD_KINDS: Partial<Record<GuardKind, string>> = {}
277
+
278
+ /** A kind's effective severity: consumer override first, then the grace table, then `error`. */
279
+ function severityOf(kind: GuardKind, cfg: GuardConfig): GuardSeverity {
280
+ return cfg.severity?.[kind] ?? (kind in GRACE_PERIOD_KINDS ? 'warn' : 'error')
211
281
  }
212
282
 
213
283
  // ── Path predicate ────────────────────────────────────────────────────────────────────────────────
@@ -672,7 +742,9 @@ function ruleApplies(kind: GuardKind, relPath: string): boolean {
672
742
  * if (findings.some((f) => f.kind === 'raw-hex')) { ... }
673
743
  */
674
744
  export function checkSource(text: string, relPath: string, cfg: GuardConfig): Finding[] {
675
- const findings: Finding[] = []
745
+ // Severity is stamped once at the end rather than at each of the ~20 push sites — it is a
746
+ // property of the KIND and the config, never of the match.
747
+ const findings: Omit<Finding, 'severity'>[] = []
676
748
 
677
749
  // Derive the 3 dynamic regexes via GUARD_RULES pattern builders.
678
750
  const forbiddenAccentRe = (
@@ -885,5 +957,7 @@ export function checkSource(text: string, relPath: string, cfg: GuardConfig): Fi
885
957
  // Per-rule, per-path exemption post-filter (§exemptRules) — applied once here so it uniformly
886
958
  // covers every kind regardless of whether it was emitted via the GUARD_RULES registry loop above
887
959
  // 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))
960
+ return findings
961
+ .filter((f) => !isRuleExempt(f.kind, f.relPath, cfg.exemptRules))
962
+ .map((f) => ({ ...f, severity: severityOf(f.kind, cfg) }))
889
963
  }
@@ -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
  }