@oicl/openbridge-webcomponents-full-bundle 2.2.0-next.15 → 2.2.0-next.17

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.
Files changed (56) hide show
  1. package/bundle/openbridge-webcomponents.bundle.js +320 -64
  2. package/bundle/openbridge-webcomponents.bundle.js.map +1 -1
  3. package/custom-elements.json +1752 -15
  4. package/dist/automation/analog-threeway-valve/analog-threeway-valve.d.ts +4 -4
  5. package/dist/automation/analog-threeway-valve/analog-threeway-valve.js.map +1 -1
  6. package/dist/automation/automation-badge/automation-badge.css_inline.js +6 -0
  7. package/dist/automation/automation-badge/automation-badge.d.ts +61 -1
  8. package/dist/automation/automation-badge/automation-badge.d.ts.map +1 -1
  9. package/dist/automation/automation-badge/automation-badge.js +83 -1
  10. package/dist/automation/automation-badge/automation-badge.js.map +1 -1
  11. package/dist/automation/automation-button/abstract-automation-button-storybook-helpers.d.ts +20 -0
  12. package/dist/automation/automation-button/abstract-automation-button-storybook-helpers.d.ts.map +1 -1
  13. package/dist/automation/automation-button/abstract-automation-button-storybook-helpers.js +1 -0
  14. package/dist/automation/automation-button/abstract-automation-button-storybook-helpers.js.map +1 -1
  15. package/dist/automation/automation-button/abstract-automation-button.d.ts +16 -4
  16. package/dist/automation/automation-button/abstract-automation-button.d.ts.map +1 -1
  17. package/dist/automation/automation-button/abstract-automation-button.js +46 -20
  18. package/dist/automation/automation-button/abstract-automation-button.js.map +1 -1
  19. package/dist/automation/automation-button/badge-layout.d.ts +34 -0
  20. package/dist/automation/automation-button/badge-layout.d.ts.map +1 -0
  21. package/dist/automation/automation-button/badge-layout.js +64 -0
  22. package/dist/automation/automation-button/badge-layout.js.map +1 -0
  23. package/dist/automation/automation-tank/automation-tank.d.ts.map +1 -1
  24. package/dist/automation/automation-tank/automation-tank.js +7 -2
  25. package/dist/automation/automation-tank/automation-tank.js.map +1 -1
  26. package/dist/automation/digital-threeway-valve/digital-threeway-valve.d.ts +4 -4
  27. package/dist/automation/digital-threeway-valve/digital-threeway-valve.js.map +1 -1
  28. package/dist/automation/electricity-battery/electricity-battery.d.ts +4 -4
  29. package/dist/automation/electricity-battery/electricity-battery.js.map +1 -1
  30. package/dist/automation/specialty-tank/abstract-specialty-tank.d.ts.map +1 -1
  31. package/dist/automation/specialty-tank/abstract-specialty-tank.js +7 -2
  32. package/dist/automation/specialty-tank/abstract-specialty-tank.js.map +1 -1
  33. package/dist/internal/global-setting.d.ts +55 -0
  34. package/dist/internal/global-setting.d.ts.map +1 -0
  35. package/dist/internal/global-setting.js +86 -0
  36. package/dist/internal/global-setting.js.map +1 -0
  37. package/package.json +3 -2
  38. package/script/check-typography-ramp.ts +144 -0
  39. package/script/typography/ramp.ts +237 -0
  40. package/script/typography-ramp.test.ts +267 -0
  41. package/src/automation/analog-threeway-valve/analog-threeway-valve.ts +4 -4
  42. package/src/automation/analog-valve/analog-valve.stories.ts +8 -0
  43. package/src/automation/automation-badge/automation-badge.css +6 -0
  44. package/src/automation/automation-badge/automation-badge.stories.ts +47 -3
  45. package/src/automation/automation-badge/automation-badge.ts +124 -1
  46. package/src/automation/automation-button/abstract-automation-button-storybook-helpers.ts +3 -0
  47. package/src/automation/automation-button/abstract-automation-button.ts +70 -23
  48. package/src/automation/automation-button/badge-layout.spec.ts +138 -0
  49. package/src/automation/automation-button/badge-layout.ts +120 -0
  50. package/src/automation/automation-tank/automation-tank.ts +11 -6
  51. package/src/automation/digital-threeway-valve/digital-threeway-valve.ts +4 -4
  52. package/src/automation/electricity-battery/electricity-battery.ts +4 -4
  53. package/src/automation/pump/pump.stories.ts +59 -0
  54. package/src/automation/specialty-tank/abstract-specialty-tank.ts +11 -6
  55. package/src/internal/global-setting.spec.ts +119 -0
  56. package/src/internal/global-setting.ts +105 -0
@@ -0,0 +1,86 @@
1
+ // src/internal/global-setting.ts
2
+ /**
3
+ * One app-wide setting the components render from.
4
+ *
5
+ * The application sets it once through a module setter such as
6
+ * `setAutomationBadgeLayout()` or `setAlertStandard()`. Every connected host
7
+ * that holds a controller for it re-renders on each change; a host detached
8
+ * while the value changed catches up on its next connect, and one reattached
9
+ * unchanged renders nothing extra. Setting the same value again is a no-op.
10
+ *
11
+ * It is a plain subscriber set rather than a signal: the value rarely changes
12
+ * and derives nothing, and the library stays off a pre-standard polyfill. A
13
+ * setter built on it can move to a signal later without changing its signature.
14
+ *
15
+ * ```ts
16
+ * const alertStandard = new GlobalSetting<AlertStandard | undefined>(undefined);
17
+ * export const setAlertStandard = (s?: AlertStandard) => alertStandard.set(s);
18
+ *
19
+ * class ObcAlertIcon extends LitElement {
20
+ * private readonly standard = alertStandard.controller(this);
21
+ * render() { return draw(this.standard.value); }
22
+ * }
23
+ * ```
24
+ */
25
+ var GlobalSetting = class {
26
+ constructor(value) {
27
+ this.value = value;
28
+ this.currentVersion = 0;
29
+ this.hosts = /* @__PURE__ */ new Set();
30
+ }
31
+ get() {
32
+ return this.value;
33
+ }
34
+ /** Stores the value and re-renders every connected host. */
35
+ set(value) {
36
+ if (Object.is(value, this.value)) return;
37
+ this.value = value;
38
+ this.currentVersion++;
39
+ for (const host of this.hosts) host.requestUpdate();
40
+ }
41
+ /** Re-renders `host` whenever the value changes; create it once, in the host's constructor. */
42
+ controller(host) {
43
+ return new GlobalSettingController(host, this);
44
+ }
45
+ /** Counts the changes, so a controller can tell whether its host rendered the current value. */
46
+ get version() {
47
+ return this.currentVersion;
48
+ }
49
+ attach(host) {
50
+ this.hosts.add(host);
51
+ }
52
+ detach(host) {
53
+ this.hosts.delete(host);
54
+ }
55
+ };
56
+ /** Keeps one host rendered with the current value of a `GlobalSetting`. */
57
+ var GlobalSettingController = class {
58
+ constructor(host, setting) {
59
+ this.host = host;
60
+ this.setting = setting;
61
+ this.renderedVersion = setting.version;
62
+ this.renderingVersion = setting.version;
63
+ host.addController(this);
64
+ }
65
+ get value() {
66
+ return this.setting.get();
67
+ }
68
+ /** A value set while the host was detached never reached it. */
69
+ hostConnected() {
70
+ this.setting.attach(this.host);
71
+ if (this.renderedVersion !== this.setting.version) this.host.requestUpdate();
72
+ }
73
+ hostDisconnected() {
74
+ this.setting.detach(this.host);
75
+ }
76
+ /** The version a render starts from; a change while it runs detached must not count as rendered. */
77
+ hostUpdate() {
78
+ this.renderingVersion = this.setting.version;
79
+ }
80
+ hostUpdated() {
81
+ this.renderedVersion = this.renderingVersion;
82
+ }
83
+ };
84
+ export { GlobalSetting, GlobalSettingController };
85
+
86
+ //# sourceMappingURL=global-setting.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"global-setting.js","names":[],"sources":["../../src/internal/global-setting.ts"],"sourcesContent":["import type {ReactiveController, ReactiveControllerHost} from 'lit';\n\n/**\n * One app-wide setting the components render from.\n *\n * The application sets it once through a module setter such as\n * `setAutomationBadgeLayout()` or `setAlertStandard()`. Every connected host\n * that holds a controller for it re-renders on each change; a host detached\n * while the value changed catches up on its next connect, and one reattached\n * unchanged renders nothing extra. Setting the same value again is a no-op.\n *\n * It is a plain subscriber set rather than a signal: the value rarely changes\n * and derives nothing, and the library stays off a pre-standard polyfill. A\n * setter built on it can move to a signal later without changing its signature.\n *\n * ```ts\n * const alertStandard = new GlobalSetting<AlertStandard | undefined>(undefined);\n * export const setAlertStandard = (s?: AlertStandard) => alertStandard.set(s);\n *\n * class ObcAlertIcon extends LitElement {\n * private readonly standard = alertStandard.controller(this);\n * render() { return draw(this.standard.value); }\n * }\n * ```\n */\nexport class GlobalSetting<T> {\n private currentVersion = 0;\n private readonly hosts = new Set<ReactiveControllerHost>();\n\n constructor(private value: T) {}\n\n get(): T {\n return this.value;\n }\n\n /** Stores the value and re-renders every connected host. */\n set(value: T): void {\n if (Object.is(value, this.value)) {\n return;\n }\n this.value = value;\n this.currentVersion++;\n for (const host of this.hosts) {\n host.requestUpdate();\n }\n }\n\n /** Re-renders `host` whenever the value changes; create it once, in the host's constructor. */\n controller(host: ReactiveControllerHost): GlobalSettingController<T> {\n return new GlobalSettingController(host, this);\n }\n\n /** Counts the changes, so a controller can tell whether its host rendered the current value. */\n get version(): number {\n return this.currentVersion;\n }\n\n attach(host: ReactiveControllerHost): void {\n this.hosts.add(host);\n }\n\n detach(host: ReactiveControllerHost): void {\n this.hosts.delete(host);\n }\n}\n\n/** Keeps one host rendered with the current value of a `GlobalSetting`. */\nexport class GlobalSettingController<T> implements ReactiveController {\n private renderedVersion: number;\n private renderingVersion: number;\n\n constructor(\n private readonly host: ReactiveControllerHost,\n private readonly setting: GlobalSetting<T>\n ) {\n this.renderedVersion = setting.version;\n this.renderingVersion = setting.version;\n host.addController(this);\n }\n\n get value(): T {\n return this.setting.get();\n }\n\n /** A value set while the host was detached never reached it. */\n hostConnected(): void {\n this.setting.attach(this.host);\n if (this.renderedVersion !== this.setting.version) {\n this.host.requestUpdate();\n }\n }\n\n hostDisconnected(): void {\n this.setting.detach(this.host);\n }\n\n /** The version a render starts from; a change while it runs detached must not count as rendered. */\n hostUpdate(): void {\n this.renderingVersion = this.setting.version;\n }\n\n hostUpdated(): void {\n this.renderedVersion = this.renderingVersion;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAyBA,IAAa,gBAAb,MAA8B;CAI5B,YAAY,OAAkB;EAAV,KAAA,QAAA;EAHK,KAAA,iBAAA;EACA,KAAA,wBAAA,IAAI,IAA4B;CAE1B;CAE/B,MAAS;EACP,OAAO,KAAK;CACd;;CAGA,IAAI,OAAgB;EAClB,IAAI,OAAO,GAAG,OAAO,KAAK,KAAK,GAC7B;EAEF,KAAK,QAAQ;EACb,KAAK;EACL,KAAK,MAAM,QAAQ,KAAK,OACtB,KAAK,cAAc;CAEvB;;CAGA,WAAW,MAA0D;EACnE,OAAO,IAAI,wBAAwB,MAAM,IAAI;CAC/C;;CAGA,IAAI,UAAkB;EACpB,OAAO,KAAK;CACd;CAEA,OAAO,MAAoC;EACzC,KAAK,MAAM,IAAI,IAAI;CACrB;CAEA,OAAO,MAAoC;EACzC,KAAK,MAAM,OAAO,IAAI;CACxB;AACF;;AAGA,IAAa,0BAAb,MAAsE;CAIpE,YACE,MACA,SACA;EAFiB,KAAA,OAAA;EACA,KAAA,UAAA;EAEjB,KAAK,kBAAkB,QAAQ;EAC/B,KAAK,mBAAmB,QAAQ;EAChC,KAAK,cAAc,IAAI;CACzB;CAEA,IAAI,QAAW;EACb,OAAO,KAAK,QAAQ,IAAI;CAC1B;;CAGA,gBAAsB;EACpB,KAAK,QAAQ,OAAO,KAAK,IAAI;EAC7B,IAAI,KAAK,oBAAoB,KAAK,QAAQ,SACxC,KAAK,KAAK,cAAc;CAE5B;CAEA,mBAAyB;EACvB,KAAK,QAAQ,OAAO,KAAK,IAAI;CAC/B;;CAGA,aAAmB;EACjB,KAAK,mBAAmB,KAAK,QAAQ;CACvC;CAEA,cAAoB;EAClB,KAAK,kBAAkB,KAAK;CAC9B;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oicl/openbridge-webcomponents-full-bundle",
3
- "version": "2.2.0-next.15",
3
+ "version": "2.2.0-next.17",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -143,7 +143,7 @@
143
143
  "typecheck:tooling": "tsc -p tsconfig.tooling.json",
144
144
  "format": "prettier \"**/*.{cjs,mjs,html,js,json,md,ts,css}\" \"../../*.md\" \"../../docs/agents/*.md\" \"../../.github/**/*.md\" --write",
145
145
  "format:check": "prettier \"**/*.{cjs,mjs,html,js,json,md,ts,css}\" \"../../*.md\" \"../../docs/agents/*.md\" \"../../.github/**/*.md\" --check",
146
- "lint": "npm run lint:mixins && npm run lint:variables && npm run lint:palette && npm run lint:icons && npm run lint:slots && npm run lint:events && npm run lint:apg && npm run lint:agents && npm run lint:lit-analyzer && npm run lint:eslint && npm run lint:suppressions && npm run lint:comments",
146
+ "lint": "npm run lint:mixins && npm run lint:variables && npm run lint:typography && npm run lint:palette && npm run lint:icons && npm run lint:slots && npm run lint:events && npm run lint:apg && npm run lint:agents && npm run lint:lit-analyzer && npm run lint:eslint && npm run lint:suppressions && npm run lint:comments",
147
147
  "agents:sync": "tsx ./script/sync-agent-docs.ts",
148
148
  "lint:agents": "tsx ./script/sync-agent-docs.ts --check",
149
149
  "lint:mixins": "tsx ./script/check-css-mixins.ts",
@@ -151,6 +151,7 @@
151
151
  "lint:events": "tsx ./script/check-event-leaks.ts",
152
152
  "lint:apg": "tsx ./script/check-apg-records.ts",
153
153
  "lint:variables": "tsx ./script/check-css-variables.ts",
154
+ "lint:typography": "tsx ./script/check-typography-ramp.ts",
154
155
  "lint:palette": "tsx ./script/strip-palette-export.ts --check",
155
156
  "palette:strip": "tsx ./script/strip-palette-export.ts",
156
157
  "palette:diff": "tsx ./script/diff-palette-export.ts",
@@ -0,0 +1,144 @@
1
+ /**
2
+ * Fails when a component reads a font-size or line-height token that a size
3
+ * class leaves undeclared, at 0, or smaller than the class before it, so its
4
+ * text keeps the regular size, disappears or shrinks there (see
5
+ * `typography/ramp.ts`).
6
+ *
7
+ * ```bash
8
+ * npm run lint:typography
9
+ * ```
10
+ *
11
+ * A token the design file cannot fill yet goes in `KNOWN_GAPS` with the size
12
+ * classes it gaps in and the issue that tracks it; the entry fails as stale
13
+ * once the export carries the value.
14
+ */
15
+ import * as fs from 'fs';
16
+ import * as path from 'path';
17
+ import {globby} from 'globby';
18
+ import {maskCssText} from './css/mask.js';
19
+ import {stripComments} from './source-links.js';
20
+ import {
21
+ type KnownGap,
22
+ type RampGap,
23
+ mixinTokens,
24
+ rampGaps,
25
+ readPalette,
26
+ readTokens,
27
+ } from './typography/ramp.js';
28
+
29
+ const SKYPLOT = 'src/navigation-instruments/gnss-skyplot/gnss-skyplot.css';
30
+ const GENERAL_SIZES =
31
+ 'the General styles have no medium, large or xl sizes in the design file; obc-gnss-skyplot reads them by design and keeps its regular size there (#1420)';
32
+ const UI_TITLE_XL = 'UI/Title has no XL value in the design file (#1422)';
33
+ const AUTOMATION_XL =
34
+ 'the automation styles carry their regular sizes in the XL mode of the design file, smaller than in large (#1422)';
35
+
36
+ /** Token → the size classes it gaps in, the readers that may keep reading it if only some, and why. */
37
+ const KNOWN_GAPS: Record<string, KnownGap> = {
38
+ '--global-typography-general-xs-font-size': {
39
+ classes: ['medium', 'large', 'xl'],
40
+ readers: [SKYPLOT],
41
+ reason: GENERAL_SIZES,
42
+ },
43
+ '--global-typography-general-xs-line-height': {
44
+ classes: ['medium', 'large', 'xl'],
45
+ readers: [SKYPLOT],
46
+ reason: GENERAL_SIZES,
47
+ },
48
+ '--global-typography-ui-title-font-size': {
49
+ classes: ['xl'],
50
+ reason: UI_TITLE_XL,
51
+ },
52
+ '--global-typography-ui-title-line-height': {
53
+ classes: ['xl'],
54
+ reason: UI_TITLE_XL,
55
+ },
56
+ '--automation-components-typography-unit-regular-font-size': {
57
+ classes: ['xl'],
58
+ reason: AUTOMATION_XL,
59
+ },
60
+ '--automation-components-typography-label-font-size': {
61
+ classes: ['xl'],
62
+ reason: AUTOMATION_XL,
63
+ },
64
+ '--automation-components-typography-label-line-height': {
65
+ classes: ['xl'],
66
+ reason: AUTOMATION_XL,
67
+ },
68
+ '--global-typography-instrument-value-medium-font-size': {
69
+ classes: ['xl'],
70
+ reason:
71
+ 'Instrument/Value medium is 36 px in the XL mode of the design file, smaller than 44 px in large (#1422)',
72
+ },
73
+ };
74
+
75
+ function gapText(gap: RampGap): string {
76
+ switch (gap.kind) {
77
+ case 'missing':
78
+ return 'not declared';
79
+ case 'zero':
80
+ return gap.value ?? '0';
81
+ case 'shrinks':
82
+ return `${gap.sizes?.[0]}, smaller than the ${gap.sizes?.[1]} of the class before,`;
83
+ }
84
+ }
85
+
86
+ async function run(): Promise<void> {
87
+ const root = process.cwd();
88
+ const read = (file: string) => fs.readFileSync(path.join(root, file), 'utf8');
89
+ const mixinFiles = await globby('src/mixins/*.css', {cwd: root});
90
+ const mixins = mixinTokens(mixinFiles.map((f) => maskCssText(read(f))));
91
+ const files = await globby(['src/**/*.css', 'src/**/*.ts'], {
92
+ cwd: root,
93
+ ignore: [
94
+ 'src/palettes/**',
95
+ 'src/mixins/**',
96
+ 'src/icons/**',
97
+ 'src/generated/**',
98
+ 'src/**/*.stories.ts',
99
+ 'src/**/*.spec.ts',
100
+ ],
101
+ });
102
+ const readers = new Map<string, Set<string>>();
103
+ for (const file of files.sort()) {
104
+ const text = file.endsWith('.css')
105
+ ? maskCssText(read(file))
106
+ : stripComments(read(file));
107
+ for (const token of readTokens(text, mixins)) {
108
+ readers.set(token, (readers.get(token) ?? new Set()).add(file));
109
+ }
110
+ }
111
+ const palette = readPalette(read('src/palettes/variables.css'));
112
+ const {gaps, stale} = rampGaps(readers, palette, KNOWN_GAPS);
113
+ const sizeTokens = Array.from(readers.keys()).filter(
114
+ (t) =>
115
+ /-(?:font-size|line-height)$/.test(t) && palette.classes.regular.has(t)
116
+ );
117
+
118
+ console.log(
119
+ `typography ramp: ${sizeTokens.length} size tokens read by ${files.length} files, ${Object.keys(KNOWN_GAPS).length} known gaps`
120
+ );
121
+ for (const gap of gaps) {
122
+ const where = gap.readers.slice(0, 3).join(', ');
123
+ const more = gap.readers.length > 3 ? ` +${gap.readers.length - 3}` : '';
124
+ console.error(
125
+ ` - ${gap.token} is ${gapText(gap)} in .obc-component-size-${gap.sizeClass}, read by ${where}${more}`
126
+ );
127
+ }
128
+ for (const entry of stale) {
129
+ console.error(
130
+ ` - KNOWN_GAPS lists ${entry.token}, but ${entry.detail}; narrow or delete the entry`
131
+ );
132
+ }
133
+ if (gaps.length || stale.length) {
134
+ console.error(
135
+ '\n[lint:typography] text on these tokens keeps its regular size, disappears or shrinks in that class. Keep it on a style whose variables carry every size class (IMPLEMENTATION_GUIDELINES.md § Instrument and automation fonts), or list the token in KNOWN_GAPS with the issue that tracks the design file.'
136
+ );
137
+ process.exit(1);
138
+ }
139
+ console.log(
140
+ '✅ every size token components read has a value in every size class and never shrinks.'
141
+ );
142
+ }
143
+
144
+ run();
@@ -0,0 +1,237 @@
1
+ /**
2
+ * @module TypographyRamp
3
+ * @description
4
+ * The size classes (`.obc-component-size-regular` / `-medium` / `-large` /
5
+ * `-xl`) scale text through the font-size and line-height tokens each class
6
+ * block of `src/palettes/variables.css` declares. A token a block does not
7
+ * declare keeps the regular value there, a token the export leaves at 0 hides
8
+ * the text, and a value smaller than the class before it shrinks the text as
9
+ * the class grows. The first went unnoticed until a consumer found the text
10
+ * stuck at the regular size (#1420). `rampGaps()` lists all three for the
11
+ * tokens components read, directly or through a mixin;
12
+ * `check-typography-ramp.ts` fails on them.
13
+ */
14
+ import type {Declarations} from '../palette/export.js';
15
+ import {parseDeclarations, parseTopLevelBlocks} from '../palette/export.js';
16
+
17
+ export const SIZE_CLASSES = ['regular', 'medium', 'large', 'xl'] as const;
18
+ export type SizeClass = (typeof SIZE_CLASSES)[number];
19
+
20
+ const TOKEN_RE = /--[\w-]+/g;
21
+ const SIZE_TOKEN_RE = /-(?:font-size|line-height)$/;
22
+ const MIXIN_NAME_RE = /@define-mixin\s+([\w-]+)[^;{}]*$/;
23
+ const MIXIN_USE_RE = /@mixin\s+([\w-]+)/g;
24
+ const LENGTH_RE = /^([+-]?(?:\d+\.?\d*|\.\d+))([a-z%]*)$/i;
25
+ const VAR_RE = /^var\((--[\w-]+)\)$/;
26
+
27
+ function tokensIn(text: string): Set<string> {
28
+ return new Set(text.match(TOKEN_RE) ?? []);
29
+ }
30
+
31
+ /**
32
+ * Mixin name → the custom properties its body reads, nested rules and the
33
+ * mixins it applies included.
34
+ */
35
+ export function mixinTokens(mixinSources: string[]): Map<string, Set<string>> {
36
+ const direct = new Map<string, Set<string>>();
37
+ const applies = new Map<string, string[]>();
38
+ for (const source of mixinSources) {
39
+ for (const block of parseTopLevelBlocks(source)) {
40
+ const name = block.selector.match(MIXIN_NAME_RE)?.[1];
41
+ if (!name) continue;
42
+ direct.set(name, tokensIn(block.body));
43
+ applies.set(
44
+ name,
45
+ Array.from(block.body.matchAll(MIXIN_USE_RE), (use) => use[1])
46
+ );
47
+ }
48
+ }
49
+ const resolved = new Map<string, Set<string>>();
50
+ const resolve = (name: string, seen: Set<string>): Set<string> => {
51
+ const cached = resolved.get(name);
52
+ if (cached) return cached;
53
+ const tokens = new Set(direct.get(name) ?? []);
54
+ for (const inner of applies.get(name) ?? []) {
55
+ if (seen.has(inner)) continue;
56
+ for (const t of resolve(inner, new Set([...seen, inner]))) tokens.add(t);
57
+ }
58
+ resolved.set(name, tokens);
59
+ return tokens;
60
+ };
61
+ for (const name of direct.keys()) resolve(name, new Set([name]));
62
+ return resolved;
63
+ }
64
+
65
+ /**
66
+ * The custom properties a source file reads: every name in it (inside `var()`
67
+ * or as a bare string, as `charthelpers/constants.ts` keeps them) and those of
68
+ * every mixin it applies. Mask comments first, or a name in one counts.
69
+ */
70
+ export function readTokens(
71
+ text: string,
72
+ mixins: Map<string, Set<string>>
73
+ ): Set<string> {
74
+ const tokens = tokensIn(text);
75
+ for (const m of text.matchAll(MIXIN_USE_RE)) {
76
+ for (const t of mixins.get(m[1]) ?? []) tokens.add(t);
77
+ }
78
+ return tokens;
79
+ }
80
+
81
+ export interface Palette {
82
+ /** The declarations of each size-class block. */
83
+ classes: Record<SizeClass, Declarations>;
84
+ /** Every declaration outside them, the first one winning: what a size token's `var()` points at. */
85
+ primitives: Declarations;
86
+ }
87
+
88
+ export function readPalette(variablesCss: string): Palette {
89
+ const blocks = parseDeclarations(variablesCss);
90
+ const classes = {} as Record<SizeClass, Declarations>;
91
+ const owners = new Set<Declarations>();
92
+ for (const sizeClass of SIZE_CLASSES) {
93
+ const selector = `.obc-component-size-${sizeClass}`;
94
+ const found = Array.from(blocks.entries()).find(([s]) =>
95
+ s.split(',').some((part) => part.trim() === selector)
96
+ );
97
+ if (!found) throw new Error(`variables.css has no ${selector} block`);
98
+ classes[sizeClass] = found[1];
99
+ owners.add(found[1]);
100
+ }
101
+ const primitives: Declarations = new Map();
102
+ for (const declarations of blocks.values()) {
103
+ if (owners.has(declarations)) continue;
104
+ for (const [name, value] of declarations) {
105
+ if (!primitives.has(name)) primitives.set(name, value);
106
+ }
107
+ }
108
+ return {classes, primitives};
109
+ }
110
+
111
+ export interface Length {
112
+ n: number;
113
+ unit: string;
114
+ }
115
+
116
+ /** A value as a number and its unit, following `var()` through the class block and the primitives. */
117
+ export function resolveLength(
118
+ value: string | undefined,
119
+ block: Declarations,
120
+ primitives: Declarations
121
+ ): Length | undefined {
122
+ for (let depth = 0; value !== undefined && depth < 8; depth++) {
123
+ const ref = value.match(VAR_RE);
124
+ if (!ref) break;
125
+ value = block.get(ref[1]) ?? primitives.get(ref[1]);
126
+ }
127
+ const m = value?.match(LENGTH_RE);
128
+ return m ? {n: Number(m[1]), unit: m[2].toLowerCase()} : undefined;
129
+ }
130
+
131
+ export type GapKind = 'missing' | 'zero' | 'shrinks';
132
+
133
+ export interface RampGap {
134
+ token: string;
135
+ sizeClass: SizeClass;
136
+ kind: GapKind;
137
+ /** The declared value, `undefined` when the block does not declare the token. */
138
+ value: string | undefined;
139
+ /** For `shrinks`: the resolved value here and in the class before. */
140
+ sizes?: [string, string];
141
+ readers: string[];
142
+ }
143
+
144
+ export interface KnownGap {
145
+ /** The size classes the gap is accepted in. */
146
+ classes: SizeClass[];
147
+ /** When set, the only files the gap is accepted for; any other reader is reported. */
148
+ readers?: string[];
149
+ /** Why, with the issue that tracks it. */
150
+ reason: string;
151
+ }
152
+
153
+ export interface StaleEntry {
154
+ token: string;
155
+ detail: string;
156
+ }
157
+
158
+ export interface RampReport {
159
+ gaps: RampGap[];
160
+ /** Known-gap entries that accept more than is still a gap, so they get narrowed or deleted. */
161
+ stale: StaleEntry[];
162
+ }
163
+
164
+ const show = (length: Length) => `${length.n}${length.unit}`;
165
+
166
+ /**
167
+ * Every font-size or line-height token of the size classes that a file reads
168
+ * and that a class leaves undeclared, at 0, or smaller than the class before,
169
+ * minus what `known` accepts; a known entry that accepts more than is still a
170
+ * gap comes back as stale.
171
+ */
172
+ export function rampGaps(
173
+ readers: Map<string, Set<string>>,
174
+ palette: Palette,
175
+ known: Record<string, KnownGap> = {}
176
+ ): RampReport {
177
+ const gaps: RampGap[] = [];
178
+ const stale: StaleEntry[] = [];
179
+ for (const [token, files] of readers) {
180
+ if (!SIZE_TOKEN_RE.test(token)) continue;
181
+ if (!SIZE_CLASSES.some((c) => palette.classes[c].has(token))) continue;
182
+ const entry = known[token];
183
+ const sortedFiles = Array.from(files).sort();
184
+ let previous: Length | undefined;
185
+ for (const sizeClass of SIZE_CLASSES) {
186
+ const block = palette.classes[sizeClass];
187
+ const declared = block.get(token);
188
+ const length = resolveLength(
189
+ declared ?? palette.classes.regular.get(token),
190
+ block,
191
+ palette.primitives
192
+ );
193
+ let kind: GapKind | undefined;
194
+ if (declared === undefined) kind = 'missing';
195
+ else if (length?.n === 0) kind = 'zero';
196
+ else if (
197
+ length &&
198
+ previous &&
199
+ length.unit === previous.unit &&
200
+ length.n < previous.n
201
+ ) {
202
+ kind = 'shrinks';
203
+ }
204
+ const accepted = entry?.classes.includes(sizeClass) ?? false;
205
+ if (accepted && !kind) {
206
+ stale.push({token, detail: `it has a value in ${sizeClass}`});
207
+ }
208
+ if (kind) {
209
+ const allowed = accepted ? (entry?.readers ?? sortedFiles) : [];
210
+ const extra = sortedFiles.filter((f) => !allowed.includes(f));
211
+ if (extra.length) {
212
+ gaps.push({
213
+ token,
214
+ sizeClass,
215
+ kind,
216
+ value: declared,
217
+ sizes:
218
+ kind === 'shrinks' && length && previous
219
+ ? [show(length), show(previous)]
220
+ : undefined,
221
+ readers: extra,
222
+ });
223
+ }
224
+ }
225
+ if (length) previous = length;
226
+ }
227
+ for (const reader of entry?.readers ?? []) {
228
+ if (!files.has(reader)) {
229
+ stale.push({token, detail: `${reader} no longer reads it`});
230
+ }
231
+ }
232
+ }
233
+ for (const token of Object.keys(known)) {
234
+ if (!readers.has(token)) stale.push({token, detail: 'nothing reads it'});
235
+ }
236
+ return {gaps, stale};
237
+ }