Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

code fixes

aashubef7179b7d1b
diff --git a/docs/components/editorial/term.tsx b/docs/components/editorial/term.tsx
index 0b48883281..84cd92e3d2 100644
--- a/docs/components/editorial/term.tsx
+++ b/docs/components/editorial/term.tsx
@@ -15,7 +15,7 @@
import * as Popover from "@radix-ui/react-popover";
import Link from "next/link";
import type { ReactNode } from "react";
-import { getEntry } from "@carbon/glossary";
+import { lookupEntry } from "@carbon/glossary";
function slugify(text: string) {
return text
@@ -27,7 +27,7 @@ function slugify(text: string) {
export function Term({ id, children }: { id?: string; children: ReactNode }) {
const key = id ?? (typeof children === "string" ? slugify(children) : "");
- const entry = getEntry(key);
+ const entry = lookupEntry(key);
if (!entry) return <>{children}</>;
diff --git a/packages/field-help/src/LabelWithHelp.tsx b/packages/field-help/src/LabelWithHelp.tsx
index ec13c66271..ce69c25827 100644
--- a/packages/field-help/src/LabelWithHelp.tsx
+++ b/packages/field-help/src/LabelWithHelp.tsx
@@ -78,8 +78,8 @@ export function LabelWithHelp({
className,
tooltipDelayDuration = 200
}: LabelWithHelpProps) {
- const entry = termId !== undefined ? getEntry(termId) : undefined;
- if (!entry) return <>{children}</>;
+ if (termId === undefined) return <>{children}</>;
+ const entry = getEntry(termId);
// When the call-site provides a JSX override, it's responsible for any
// in-app routing it wants to surface — auto-appending "Learn more" would
diff --git a/packages/glossary/src/helpers.ts b/packages/glossary/src/helpers.ts
index 45bc0d626b..8ec244aecc 100644
--- a/packages/glossary/src/helpers.ts
+++ b/packages/glossary/src/helpers.ts
@@ -32,11 +32,19 @@ function getAliasIndex(): Map<string, TermId> {
return index;
}
-/** Type-safe lookup. The id can be a canonical `TermId` or a known alias
- * (typed as `string` since aliases aren't in the literal union). Returns
- * `undefined` for an unknown alias — call `hasEntry` first if you need
- * that narrowed away. */
-export function getEntry(id: TermId | string): GlossaryEntry | undefined {
+/** Canonical lookup — total over `TermId`. Every member of `keyof typeof terms`
+ * is a key of `terms`, so this can't return `undefined`; call-sites that have a
+ * literal-typed id (e.g. `LabelWithHelp` props) don't have to handle a missing
+ * entry. For free-text or alias slugs use `lookupEntry` instead. */
+export function getEntry(id: TermId): GlossaryEntry {
+ return terms[id];
+}
+
+/** Partial lookup for free-text or alias slugs — e.g. the docs MDX
+ * `<Term>purchase to order</Term>` slugifies the text and probes the glossary.
+ * Returns the canonical entry for a known alias, `undefined` for an unknown
+ * slug. App code with a `TermId` should call `getEntry` instead. */
+export function lookupEntry(id: string): GlossaryEntry | undefined {
if (Object.hasOwn(terms, id)) return terms[id as TermId];
const canonical = getAliasIndex().get(id);
return canonical ? terms[canonical] : undefined;
diff --git a/packages/glossary/src/index.ts b/packages/glossary/src/index.ts
index fa8f246b2f..f0a7c0449c 100644
--- a/packages/glossary/src/index.ts
+++ b/packages/glossary/src/index.ts
@@ -3,6 +3,7 @@ export {
glossaryEntries,
hasEntry,
listEntries,
+ lookupEntry,
type TermId,
termSlug
} from "./helpers";
diff --git a/packages/glossary/src/terms.test.ts b/packages/glossary/src/terms.test.ts
index b973e2447e..6ff1ca27a0 100644
--- a/packages/glossary/src/terms.test.ts
+++ b/packages/glossary/src/terms.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
-import { getEntry, hasEntry, termSlug } from "./helpers";
+import { getEntry, hasEntry, lookupEntry, termSlug } from "./helpers";
import { terms } from "./terms";
import type { GlossaryEntry } from "./types";
@@ -48,20 +48,26 @@ describe("glossary", () => {
expect(termSlug(" Mixed-Case TEXT ")).toBe("mixed-case-text");
});
- it("hasEntry / getEntry resolve canonical ids", () => {
+ it("getEntry is total over TermId", () => {
+ // `getEntry` is typed as `(id: TermId) => GlossaryEntry` — no undefined.
+ // Free-text/alias probes go through `lookupEntry` instead.
+ expect(getEntry("method").term).toBe("Method");
+ });
+
+ it("hasEntry / lookupEntry resolve unknown slugs to nothing", () => {
expect(hasEntry("method")).toBe(true);
expect(hasEntry("not-a-real-slug")).toBe(false);
- expect(getEntry("method")?.term).toBe("Method");
- expect(getEntry("not-a-real-slug")).toBeUndefined();
+ expect(lookupEntry("method")?.term).toBe("Method");
+ expect(lookupEntry("not-a-real-slug")).toBeUndefined();
});
- it("hasEntry / getEntry resolve aliases to the canonical entry", () => {
+ it("hasEntry / lookupEntry resolve aliases to the canonical entry", () => {
// `cogs` declares `aliases: ["cost-of-goods-sold"]`. The spelled-out slug
// is not in `terms` but must resolve to the same canonical entry — that's
// the whole point of the alias mechanism (lets MDX prose write either
// form and land on the same definition).
expect(hasEntry("cost-of-goods-sold")).toBe(true);
- expect(getEntry("cost-of-goods-sold")).toBe(getEntry("cogs"));
+ expect(lookupEntry("cost-of-goods-sold")).toBe(getEntry("cogs"));
});
it("every declared alias points at exactly one canonical entry", () => {