@ksp-gonogo/ui-kit 1.0.0 → 1.1.0-rc.50
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/dist/{chunk-FIYXARZ7.js → chunk-OS7L3ATF.js} +1233 -790
- package/dist/chunk-OS7L3ATF.js.map +1 -0
- package/dist/chunk-ZG7NEDVU.js +7 -0
- package/dist/chunk-ZG7NEDVU.js.map +1 -0
- package/dist/guards.js +1 -0
- package/dist/guards.js.map +1 -1
- package/dist/index.d.ts +773 -35
- package/dist/index.js +3575 -1353
- package/dist/index.js.map +1 -1
- package/dist/nullDisplay.d.ts +16 -0
- package/dist/nullDisplay.js +8 -0
- package/dist/nullDisplay.js.map +1 -0
- package/dist/reads-index.json +126 -0
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +4 -3
- package/dist/testing.js.map +1 -1
- package/dist/tokens.css +131 -48
- package/dist/{units-DjIjtO5v.d.ts → units-BmcQi-WX.d.ts} +26 -4
- package/package.json +8 -3
- package/dist/chunk-FIYXARZ7.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/nullDisplay.ts"],"sourcesContent":["// styleguide-emdash.test.ts allows the em dash here and nowhere else.\n/**\n * The null token, the dash that means \"no value to show\": nothing has arrived\n * yet, or the game confirmed there is none. It does not say which, so a widget\n * that has to tell them apart reads the reading's `state` and says so in\n * words. Useful for showing the operator an empty figure wherever only a string\n * fits: a formatter's return value, a template fallback, an attribute value.\n * Used by the tape and the band instruments, and by the Uplink page generator\n * for a table cell with nothing in it. For a bare JSX node use `NullValue`.\n * It is also published as `@ksp-gonogo/ui-kit/null-display`, a module with no\n * imports, so a Node process can read it where the root barrel cannot load.\n *\n * @category Unit\n */\nexport const NULL_DISPLAY = \"—\";\n"],"mappings":";AAcO,IAAM,eAAe;","names":[]}
|
package/dist/guards.js
CHANGED
package/dist/guards.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/guards.ts"],"sourcesContent":["import { readdirSync, readFileSync, statSync } from \"node:fs\";\nimport { join, relative, sep } from \"node:path\";\n\n/**\n * Build-time guards an Uplink runs against its own source, from one test file\n * in its client.\n *\n * Published as `@ksp-gonogo/ui-kit/guards`, separate from `./testing` because\n * it reads the filesystem (`node:fs`), which would break a browser test runner.\n *\n * ## Why\n *\n * A widget that writes\n *\n * `${closingSpeed.toFixed(1)} m/s`\n *\n * type-checks and renders, but the symbol cannot be dimmed, cannot be kept off\n * a line break, is announced as the letters \"m\", \"slash\", \"s\", and does not\n * follow when the value's ladder changes rung. `<Unit>` solves all four.\n *\n * ## Using it\n *\n * One test file, anywhere your test runner will find it:\n *\n * ```ts\n * import { expectNoHandTypedUnits } from \"@ksp-gonogo/ui-kit/guards\";\n * import { it } from \"vitest\";\n *\n * it(\"renders units through <Unit>, not by typing the symbol\", () => {\n * expectNoHandTypedUnits({ dir: \"src\" });\n * });\n * ```\n *\n * On an existing codebase with offenders already in it, seed a `baseline` and\n * lower it as you convert: a guessed conversion is worse than a hand-typed\n * symbol, because it renders a confident wrong label.\n */\n\n/**\n * The unit symbols {@link findHandTypedUnits} looks for by default (`m/s`,\n * `kPa`, `kg`, `%` and so on). Useful for catching a widget that types a unit\n * beside a value the operator reads. The list is a chosen set and not every\n * unit: a symbol nobody types beside a number, or one short enough to turn up\n * in ordinary prose, is left out.\n *\n * Pass your own through `symbols` when your Uplink registers a unit of its own.\n *\n * @category Testing\n */\nexport const HAND_TYPED_SYMBOLS: readonly string[] = [\n \"m/s²\",\n \"m/s\",\n \"km/h\",\n \"km\",\n \"Mm\",\n \"Gm\",\n \"mm\",\n \"cm\",\n \"kPa\",\n \"MPa\",\n \"kN\",\n \"MN\",\n \"kW\",\n \"MW\",\n \"GW\",\n \"kg\",\n \"°C\",\n \"°\",\n \"%\",\n \"m\",\n \"s\",\n \"t\",\n \"N\",\n \"W\",\n \"f\",\n \"sci\",\n \"rep\",\n \"Mit\",\n \"deg\",\n \"rad\",\n /*\n * Matching is case-sensitive, since single letters like `m` and `W` would\n * otherwise match prose and CSS, so a unit conventionally written both ways\n * (`rpm`, `RPM`) is listed in both spellings.\n */\n \"rpm\",\n \"RPM\",\n \"dB\",\n \"rad/s\",\n \"bit/s\",\n \"J/s\",\n \"N·m\",\n \"m²\",\n \"Pa\",\n \"kg/m³\",\n \"g/m³\",\n];\n\n/**\n * A CSS length or colour is not a readout. `[:(={]` covers a declaration\n * (`width: ...`), a call (`translate(...)`) and an attribute (`offset={...}`);\n * the colour functions cover `hsl(${h}deg ${s}% ${l}%)`.\n */\nconst CSS_PROPERTY =\n /(width|height|left|top|right|bottom|transform|translate|inset|margin|padding|gap|flex|stroke|offset|dasharray|dashoffset|hsl|hsla|rgb|rgba)\\s*[:(={]/i;\n\n/**\n * One place in a client's source where a unit symbol is typed next to a number.\n * Useful for reporting to the author where a value the operator reads has its\n * unit typed by hand. Returned by {@link findHandTypedUnits}.\n *\n * @category Testing\n */\nexport interface HandTypedUnit {\n /** Path relative to the scanned directory, with `/` separators. */\n file: string;\n /** 1-based, so it is clickable in a terminal. */\n line: number;\n /** The offending source line, trimmed. */\n source: string;\n /** The symbol that matched. */\n symbol: string;\n}\n\n/**\n * The directory, symbols, allowances and exclusions for a scan for hand-typed\n * units. Useful for adapting the scan to an Uplink's own layout and units.\n * Taken by {@link findHandTypedUnits} and {@link expectNoHandTypedUnits}.\n *\n * @category Testing\n */\nexport interface HandTypedUnitOptions {\n /** Directory to scan. Defaults to `src` under the current directory. */\n dir?: string;\n /** Override the symbols looked for. See {@link HAND_TYPED_SYMBOLS}. */\n symbols?: readonly string[];\n /**\n * Per-file allowance, keyed by the path as it appears in a finding. A file\n * at its entry passes, and a file below it throws, so an allowance left\n * higher than needed is reported rather than left open.\n */\n baseline?: Readonly<Record<string, number>>;\n /**\n * Extra paths to skip, matched against the relative path. Tests, snapshots,\n * `node_modules` and build output are already skipped.\n */\n ignore?: (file: string) => boolean;\n}\n\nconst SKIP_DIRS = new Set([\n \"node_modules\",\n \"dist\",\n \"build\",\n \"coverage\",\n \".git\",\n \".turbo\",\n \"__snapshots__\",\n]);\n\nconst SOURCE = /\\.(ts|tsx|js|jsx)$/;\n\n/**\n * Blank out comments, keeping line structure so reported line numbers still\n * point at the source, so prose about the rule is not a breach of it. Not a\n * parser: over-blanking would at worst hide a symbol inside a string literal.\n */\nfunction stripComments(source: string): string[] {\n const out: string[] = [];\n let inBlock = false;\n for (const line of source.split(\"\\n\")) {\n let kept = \"\";\n let i = 0;\n while (i < line.length) {\n if (inBlock) {\n if (line.startsWith(\"*/\", i)) {\n inBlock = false;\n i += 2;\n } else {\n i += 1;\n }\n continue;\n }\n if (line.startsWith(\"/*\", i)) {\n inBlock = true;\n i += 2;\n continue;\n }\n if (line.startsWith(\"//\", i)) break;\n kept += line[i];\n i += 1;\n }\n out.push(kept);\n }\n return out;\n}\n\nfunction isTest(file: string): boolean {\n return file.includes(\".test.\") || file.includes(\".spec.\");\n}\n\nfunction walk(root: string, dir: string, out: string[]): void {\n for (const entry of readdirSync(dir)) {\n const full = join(dir, entry);\n if (statSync(full).isDirectory()) {\n if (!SKIP_DIRS.has(entry)) walk(root, full, out);\n continue;\n }\n if (SOURCE.test(entry)) out.push(relative(root, full).split(sep).join(\"/\"));\n }\n}\n\n/**\n * The pattern every offender takes: a `${...}` interpolation, then optionally\n * one space, then a unit symbol ending at a non-word boundary.\n *\n * Longest symbol first, so `m/s` cannot be matched as a bare `m` and reported\n * with the wrong symbol.\n */\nfunction patternFor(symbols: readonly string[]): RegExp {\n const alternatives = [...symbols].sort((a, b) => b.length - a.length);\n const escaped = alternatives.map((s) =>\n s.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\"),\n );\n return new RegExp(`\\\\}\\\\s?(${escaped.join(\"|\")})([^A-Za-z0-9_/]|\\`|$)`);\n}\n\n/**\n * Every place under `dir` where a unit symbol is typed next to a number, in the\n * source of the widgets that show values to the operator. Useful for reporting\n * hand-typed units in a form of the author's choosing. Comments and test files\n * are not scanned.\n *\n * It returns the findings and never throws. {@link expectNoHandTypedUnits} is\n * the assertion built on it.\n *\n * @category Testing\n */\nexport function findHandTypedUnits(\n options: HandTypedUnitOptions = {},\n): HandTypedUnit[] {\n const dir = options.dir ?? \"src\";\n const pattern = patternFor(options.symbols ?? HAND_TYPED_SYMBOLS);\n const files: string[] = [];\n walk(dir, dir, files);\n\n const found: HandTypedUnit[] = [];\n for (const file of files) {\n if (isTest(file)) continue;\n if (options.ignore?.(file)) continue;\n const raw = readFileSync(join(dir, file), \"utf8\").split(\"\\n\");\n stripComments(raw.join(\"\\n\")).forEach((code, index) => {\n const match = pattern.exec(code);\n if (match === null) return;\n if (CSS_PROPERTY.test(code)) return;\n found.push({\n file,\n line: index + 1,\n // The raw line, not the blanked form the scan matched against.\n source: raw[index].trim(),\n symbol: match[1],\n });\n });\n }\n return found;\n}\n\n/**\n * Fails a test when a widget's source types a unit symbol next to a number,\n * with the fix in the error message. Useful for keeping every unit the operator\n * reads rendered from the value it describes. A file at its `baseline` entry\n * passes, and a file below its entry throws, so an allowance left higher than\n * needed is reported.\n *\n * @example\n * ```ts\n * import { expectNoHandTypedUnits } from \"@ksp-gonogo/ui-kit/guards\";\n * import { it } from \"vitest\";\n *\n * it(\"renders units through <Unit>, not by typing the symbol\", () => {\n * expectNoHandTypedUnits({ dir: \"src\" });\n * });\n * ```\n *\n * @category Testing\n * @categoryDescription Testing\n * Checks that a widget never types a unit symbol by hand, so every unit it\n * shows comes from the value it describes.\n */\nexport function expectNoHandTypedUnits(\n options: HandTypedUnitOptions = {},\n): void {\n const baseline = options.baseline ?? {};\n const found = findHandTypedUnits(options);\n\n const counts: Record<string, number> = {};\n for (const one of found) counts[one.file] = (counts[one.file] ?? 0) + 1;\n\n const over = found.filter(\n (one) => (counts[one.file] ?? 0) > (baseline[one.file] ?? 0),\n );\n if (over.length > 0) {\n throw new Error(\n `A unit symbol was typed next to a number in ${over.length} place(s). ` +\n \"Render <Unit value={x} /> instead, so the symbol keeps its styling, \" +\n \"follows the value's ladder, and is announced as a word rather than \" +\n \"as letters.\\n\\nWhere a string is genuinely required: speakQuantity \" +\n \"for an accessible name, writeQuantity for visible text that is \" +\n \"MEASURED (an SVG <text>, a canvas label), and nothing else.\\n\\n\" +\n over\n .map((one) => ` ${one.file}:${one.line} ${one.source}`)\n .join(\"\\n\"),\n );\n }\n\n const stale = Object.keys(baseline).filter(\n (file) => (counts[file] ?? 0) < baseline[file],\n );\n if (stale.length > 0) {\n throw new Error(\n \"These are below their baseline, which is good news. Lower or remove \" +\n \"the entry so the gain is locked in, otherwise the symbol is free to \" +\n `come back:\\n${stale\n .map((f) => ` ${f}: now ${counts[f] ?? 0}, baseline ${baseline[f]}`)\n .join(\"\\n\")}`,\n );\n }\n}\n"],"mappings":";;;AAAA,SAAS,aAAa,cAAc,gBAAgB;AACpD,SAAS,MAAM,UAAU,WAAW;AAgD7B,IAAM,qBAAwC;AAAA,EACnD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAOA,IAAM,eACJ;AA6CF,IAAM,YAAY,oBAAI,IAAI;AAAA,EACxB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAED,IAAM,SAAS;A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AI,IAAI,IAAI,IAAI,KAAK,IAAI,MAAM,EAAE,EACvD,KAAK,IAAI;AAAA,IAChB;AAAA,EACF;AAEA,QAAM,QAAQ,OAAO,KAAK,QAAQ,EAAE;AAAA,IAClC,CAAC,UAAU,OAAO,IAAI,KAAK,KAAK,SAAS,IAAI;AAAA,EAC/C;AACA,MAAI,MAAM,SAAS,GAAG;AACpB,UAAM,IAAI;AAAA,MACR;AAAA,EAEiB,MACZ,IAAI,CAAC,MAAM,KAAK,CAAC,SAAS,OAAO,CAAC,KAAK,CAAC,cAAc,SAAS,CAAC,CAAC,EAAE,EACnE,KAAK,IAAI,CAAC;AAAA,IACjB;AAAA,EACF;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/guards.ts"],"sourcesContent":["import { readdirSync, readFileSync, statSync } from \"node:fs\";\nimport { join, relative, sep } from \"node:path\";\n\n/**\n * Build-time guards an Uplink runs against its own source, from one test file\n * in its client.\n *\n * Published as `@ksp-gonogo/ui-kit/guards`, separate from `./testing` because\n * it reads the filesystem (`node:fs`), which would break a browser test runner.\n *\n * ## Why\n *\n * A widget that writes\n *\n * `${closingSpeed.toFixed(1)} m/s`\n *\n * type-checks and renders, but the symbol cannot be dimmed, cannot be kept off\n * a line break, is announced as the letters \"m\", \"slash\", \"s\", and does not\n * follow when the value's ladder changes rung. `<Unit>` solves all four.\n *\n * ## Using it\n *\n * One test file, anywhere your test runner will find it:\n *\n * ```ts\n * import { expectNoHandTypedUnits } from \"@ksp-gonogo/ui-kit/guards\";\n * import { it } from \"vitest\";\n *\n * it(\"renders units through <Unit>, not by typing the symbol\", () => {\n * expectNoHandTypedUnits({ dir: \"src\" });\n * });\n * ```\n *\n * On an existing codebase with offenders already in it, seed a `baseline` and\n * lower it as you convert: a guessed conversion is worse than a hand-typed\n * symbol, because it renders a confident wrong label.\n */\n\n/**\n * The unit symbols {@link findHandTypedUnits} looks for by default (`m/s`,\n * `kPa`, `kg`, `%` and so on). Useful for catching a widget that types a unit\n * beside a value the operator reads. The list is a chosen set and not every\n * unit: a symbol nobody types beside a number, or one short enough to turn up\n * in ordinary prose, is left out.\n *\n * Pass your own through `symbols` when your Uplink registers a unit of its own.\n *\n * @category Testing\n */\nexport const HAND_TYPED_SYMBOLS: readonly string[] = [\n \"m/s²\",\n \"m/s\",\n \"km/h\",\n \"km\",\n \"Mm\",\n \"Gm\",\n \"mm\",\n \"cm\",\n \"kPa\",\n \"MPa\",\n \"kN\",\n \"MN\",\n \"kW\",\n \"MW\",\n \"GW\",\n \"kg\",\n \"°C\",\n \"°\",\n \"%\",\n \"m\",\n \"s\",\n \"t\",\n \"N\",\n \"W\",\n \"f\",\n \"sci\",\n \"rep\",\n \"Mit\",\n \"deg\",\n \"rad\",\n /*\n * Matching is case-sensitive, since single letters like `m` and `W` would\n * otherwise match prose and CSS, so a unit conventionally written both ways\n * (`rpm`, `RPM`) is listed in both spellings.\n */\n \"rpm\",\n \"RPM\",\n \"dB\",\n \"rad/s\",\n \"bit/s\",\n \"Hz\",\n \"J/s\",\n \"N·m\",\n \"m²\",\n \"Pa\",\n \"kg/m³\",\n \"g/m³\",\n];\n\n/**\n * A CSS length or colour is not a readout. `[:(={]` covers a declaration\n * (`width: ...`), a call (`translate(...)`) and an attribute (`offset={...}`);\n * the colour functions cover `hsl(${h}deg ${s}% ${l}%)`.\n */\nconst CSS_PROPERTY =\n /(width|height|left|top|right|bottom|transform|translate|inset|margin|padding|gap|flex|stroke|offset|dasharray|dashoffset|hsl|hsla|rgb|rgba)\\s*[:(={]/i;\n\n/**\n * One place in a client's source where a unit symbol is typed next to a number.\n * Useful for reporting to the author where a value the operator reads has its\n * unit typed by hand. Returned by {@link findHandTypedUnits}.\n *\n * @category Testing\n */\nexport interface HandTypedUnit {\n /** Path relative to the scanned directory, with `/` separators. */\n file: string;\n /** 1-based, so it is clickable in a terminal. */\n line: number;\n /** The offending source line, trimmed. */\n source: string;\n /** The symbol that matched. */\n symbol: string;\n}\n\n/**\n * The directory, symbols, allowances and exclusions for a scan for hand-typed\n * units. Useful for adapting the scan to an Uplink's own layout and units.\n * Taken by {@link findHandTypedUnits} and {@link expectNoHandTypedUnits}.\n *\n * @category Testing\n */\nexport interface HandTypedUnitOptions {\n /** Directory to scan. Defaults to `src` under the current directory. */\n dir?: string;\n /** Override the symbols looked for. See {@link HAND_TYPED_SYMBOLS}. */\n symbols?: readonly string[];\n /**\n * Per-file allowance, keyed by the path as it appears in a finding. A file\n * at its entry passes, and a file below it throws, so an allowance left\n * higher than needed is reported rather than left open.\n */\n baseline?: Readonly<Record<string, number>>;\n /**\n * Extra paths to skip, matched against the relative path. Tests, snapshots,\n * `node_modules` and build output are already skipped.\n */\n ignore?: (file: string) => boolean;\n}\n\nconst SKIP_DIRS = new Set([\n \"node_modules\",\n \"dist\",\n \"build\",\n \"coverage\",\n \".git\",\n \".turbo\",\n \"__snapshots__\",\n]);\n\nconst SOURCE = /\\.(ts|tsx|js|jsx)$/;\n\n/**\n * Blank out comments, keeping line structure so reported line numbers still\n * point at the source, so prose about the rule is not a breach of it. Not a\n * parser: over-blanking would at worst hide a symbol inside a string literal.\n */\nfunction stripComments(source: string): string[] {\n const out: string[] = [];\n let inBlock = false;\n for (const line of source.split(\"\\n\")) {\n let kept = \"\";\n let i = 0;\n while (i < line.length) {\n if (inBlock) {\n if (line.startsWith(\"*/\", i)) {\n inBlock = false;\n i += 2;\n } else {\n i += 1;\n }\n continue;\n }\n if (line.startsWith(\"/*\", i)) {\n inBlock = true;\n i += 2;\n continue;\n }\n if (line.startsWith(\"//\", i)) break;\n kept += line[i];\n i += 1;\n }\n out.push(kept);\n }\n return out;\n}\n\nfunction isTest(file: string): boolean {\n return file.includes(\".test.\") || file.includes(\".spec.\");\n}\n\nfunction walk(root: string, dir: string, out: string[]): void {\n for (const entry of readdirSync(dir)) {\n const full = join(dir, entry);\n if (statSync(full).isDirectory()) {\n if (!SKIP_DIRS.has(entry)) walk(root, full, out);\n continue;\n }\n if (SOURCE.test(entry)) out.push(relative(root, full).split(sep).join(\"/\"));\n }\n}\n\n/**\n * The pattern every offender takes: a `${...}` interpolation, then optionally\n * one space, then a unit symbol ending at a non-word boundary.\n *\n * Longest symbol first, so `m/s` cannot be matched as a bare `m` and reported\n * with the wrong symbol.\n */\nfunction patternFor(symbols: readonly string[]): RegExp {\n const alternatives = [...symbols].sort((a, b) => b.length - a.length);\n const escaped = alternatives.map((s) =>\n s.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\"),\n );\n return new RegExp(`\\\\}\\\\s?(${escaped.join(\"|\")})([^A-Za-z0-9_/]|\\`|$)`);\n}\n\n/**\n * Every place under `dir` where a unit symbol is typed next to a number, in the\n * source of the widgets that show values to the operator. Useful for reporting\n * hand-typed units in a form of the author's choosing. Comments and test files\n * are not scanned.\n *\n * It returns the findings and never throws. {@link expectNoHandTypedUnits} is\n * the assertion built on it.\n *\n * @category Testing\n */\nexport function findHandTypedUnits(\n options: HandTypedUnitOptions = {},\n): HandTypedUnit[] {\n const dir = options.dir ?? \"src\";\n const pattern = patternFor(options.symbols ?? HAND_TYPED_SYMBOLS);\n const files: string[] = [];\n walk(dir, dir, files);\n\n const found: HandTypedUnit[] = [];\n for (const file of files) {\n if (isTest(file)) continue;\n if (options.ignore?.(file)) continue;\n const raw = readFileSync(join(dir, file), \"utf8\").split(\"\\n\");\n stripComments(raw.join(\"\\n\")).forEach((code, index) => {\n const match = pattern.exec(code);\n if (match === null) return;\n if (CSS_PROPERTY.test(code)) return;\n found.push({\n file,\n line: index + 1,\n // The raw line, not the blanked form the scan matched against.\n source: raw[index].trim(),\n symbol: match[1],\n });\n });\n }\n return found;\n}\n\n/**\n * Fails a test when a widget's source types a unit symbol next to a number,\n * with the fix in the error message. Useful for keeping every unit the operator\n * reads rendered from the value it describes. A file at its `baseline` entry\n * passes, and a file below its entry throws, so an allowance left higher than\n * needed is reported.\n *\n * @example\n * ```ts\n * import { expectNoHandTypedUnits } from \"@ksp-gonogo/ui-kit/guards\";\n * import { it } from \"vitest\";\n *\n * it(\"renders units through <Unit>, not by typing the symbol\", () => {\n * expectNoHandTypedUnits({ dir: \"src\" });\n * });\n * ```\n *\n * @category Testing\n * @categoryDescription Testing\n * Checks that a widget never types a unit symbol by hand, so every unit it\n * shows comes from the value it describes.\n */\nexport function expectNoHandTypedUnits(\n options: HandTypedUnitOptions = {},\n): void {\n const baseline = options.baseline ?? {};\n const found = findHandTypedUnits(options);\n\n const counts: Record<string, number> = {};\n for (const one of found) counts[one.file] = (counts[one.file] ?? 0) + 1;\n\n const over = found.filter(\n (one) => (counts[one.file] ?? 0) > (baseline[one.file] ?? 0),\n );\n if (over.length > 0) {\n throw new Error(\n `A unit symbol was typed next to a number in ${over.length} place(s). ` +\n \"Render <Unit value={x} /> instead, so the symbol keeps its styling, \" +\n \"follows the value's ladder, and is announced as a word rather than \" +\n \"as letters.\\n\\nWhere a string is genuinely required: speakQuantity \" +\n \"for an accessible name, writeQuantity for visible text that is \" +\n \"MEASURED (an SVG <text>, a canvas label), and nothing else.\\n\\n\" +\n over\n .map((one) => ` ${one.file}:${one.line} ${one.source}`)\n .join(\"\\n\"),\n );\n }\n\n const stale = Object.keys(baseline).filter(\n (file) => (counts[file] ?? 0) < baseline[file],\n );\n if (stale.length > 0) {\n throw new Error(\n \"These are below their baseline, which is good news. Lower or remove \" +\n \"the entry so the gain is locked in, otherwise the symbol is free to \" +\n `come back:\\n${stale\n .map((f) => ` ${f}: now ${counts[f] ?? 0}, baseline ${baseline[f]}`)\n .join(\"\\n\")}`,\n );\n }\n}\n"],"mappings":";;;AAAA,SAAS,aAAa,cAAc,gBAAgB;AACpD,SAAS,MAAM,UAAU,WAAW;AAgD7B,IAAM,qBAAwC;AAAA,EACnD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAC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