import { namesOf } from "../create-feature.ts"; /** * 1112/08-02. Feature ids the template's own tests and drills create in a * copy of the repository. The copy holds the product's features, and * create:feature refuses an id a feature there already has, or one with the * same prefix. A template test that used the manual's example * (`invoice-feature`) or a plain business word (`order-feature`) therefore * failed FE-GATE-005 and FE-GATE-008 in every product that had built a * feature by that name. Each id here begins with the prefix the template * reserves for its probes; docs/manual/reference/create-feature.md tells a * product not to use it. */ export const TEMPLATE_PROBE_PREFIX = "typecheck-probe-"; /** The feature the manual's code blocks are written for (docs/manual/01-add-a-feature.md). */ export const MANUAL_EXAMPLE_FEATURE_ID = "invoice-feature"; /** What tests/unit/manual-code-blocks.test.ts creates and renames the blocks to. */ export const MANUAL_PROBE_FEATURE_ID = `${TEMPLATE_PROBE_PREFIX}${MANUAL_EXAMPLE_FEATURE_ID}`; /** The sample tests/unit/template-feature-removal-documents.test.ts lists and deletes. */ export const LISTED_SAMPLE_PROBE_FEATURE_ID = `${TEMPLATE_PROBE_PREFIX}listed-sample-feature`; /** What FE-GATE-020's scaffold drill (scripts/test-feature-scaffold.ts, test:sample-removal) creates. */ export const SCAFFOLD_PROBE_FEATURE_ID = `${TEMPLATE_PROBE_PREFIX}scaffold-feature`; /** The scaffold drill's `--with-create --keyed` feature. */ export const SCAFFOLD_WRITE_PROBE_FEATURE_ID = `${TEMPLATE_PROBE_PREFIX}scaffold-write-feature`; function escape(text: string): string { return text.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&"); } /** * `text` written for feature `fromId`, with every spelling create:feature * derives from an id (`INVOICE_`, `Invoice`, `invoice-`, `invoice`) moved to * the spelling it derives from `toId`. Prose that uses the word moves too; * what matters is that the names a block defines and the names it imports * from the generated feature move together. */ export function renameFeatureText(text: string, fromId: string, toId: string): string { const from = namesOf(fromId); const to = namesOf(toId); const spellings = new Map([ [from.upper, to.upper], [from.pascal, to.pascal], [`${from.base}-`, `${to.base}-`], [from.camel, to.camel], ]); const pattern = new RegExp([...spellings.keys()].map(escape).join("|"), "gu"); return text.replace(pattern, (match) => spellings.get(match) ?? match); }