diff --git a/scripts/lib/feature-residue.ts b/scripts/lib/feature-residue.ts index 95202ea2..615dc42b 100644 --- a/scripts/lib/feature-residue.ts +++ b/scripts/lib/feature-residue.ts @@ -9,8 +9,16 @@ import path from "node:path"; * - `src/`: any file whose path or text still names the feature ID or its * folder. Platform source never names a product feature, so a name left * there is a reference the type checker cannot see (a catalog key, a - * runtime override, a link). - * - `tests/`: a file named after the feature, or one that imports from the + * runtime override, a link). The name counts as a whole name only: in the + * text, case-sensitive with no identifier character (`[A-Za-z0-9_-]`) on + * either side; in the path, a segment equal to it or whose part before the + * first `.` is. A substring search counted ten template files for + * `product-feature` (`product-features.ts`, `product-features-port.ts` and + * their importers). `src/.generated/` is derived from the feature list and + * left out. A one-word ID that is an ordinary word in the template + * (`search`) can still be found in its text. + * - `tests/`: a file named after the feature (the same path rule; the + * template's `product-feature-switch.test.tsx` is not), or one that imports from the * feature's `src/features//` or `tests/features//`. A vitest * test's text is not searched for the bare name: the template's own tests * use example IDs as data (`tests/unit/create-feature.test.ts` writes @@ -57,8 +65,13 @@ export async function productFeatureResidue( featureId: string, directory: string, ): Promise { - const names = [...new Set([featureId, directory])].map(escapeRegExp).join("|"); - const named = new RegExp(names, "iu"); + const nameSet = new Set([featureId, directory]); + const names = [...nameSet].map(escapeRegExp).join("|"); + const wholeAnyName = new RegExp(`(? + relativePath + .split("/") + .some((segment) => nameSet.has(segment) || nameSet.has(segment.split(".")[0] ?? "")); const imported = new RegExp( String.raw`(?:\bfrom\s*|\bimport\s*\(\s*|\bimport\s+|\bmock\s*\(\s*)["'][^"'\n]*/features/(?:${names})/`, "u", @@ -75,13 +88,14 @@ export async function productFeatureResidue( const relative = (file: string) => path.relative(root, file).split(path.sep).join("/"); for (const file of await filesUnder(path.join(root, "src"))) { const name = relative(file); - if (named.test(`${name}\n${await readFile(file, "utf8")}`)) residue.push(name); + if (name.startsWith("src/.generated/")) continue; + if (pathNames(name) || wholeAnyName.test(await readFile(file, "utf8"))) residue.push(name); } for (const file of await filesUnder(path.join(root, "tests"))) { const name = relative(file); const text = await readFile(file, "utf8"); if ( - named.test(name) || + pathNames(name) || imported.test(text) || (wholeName !== null && playwrightSpec.test(name) && wholeName.test(text)) ) { diff --git a/tests/unit/feature-residue.test.ts b/tests/unit/feature-residue.test.ts index 7b3f374c..ec2c2511 100644 --- a/tests/unit/feature-residue.test.ts +++ b/tests/unit/feature-residue.test.ts @@ -111,7 +111,8 @@ describe("product feature residue (1105/16-04)", () => { if (/\.spec\.[cm]?[jt]sx?$/u.test(file)) specs[file] = await readFile(file, "utf8"); } expect(Object.keys(specs)).toContain("tests/e2e/accessibility.spec.ts"); - // A copy of the specs alone: `src` is searched by substring (1105/16-04). + // A copy of the specs alone: a one-word ID that the template's source uses + // as a word (`status`) is still found in `src` text (feature-residue.ts). const root = await fixture(specs); for (const id of ["product-feature", "messages", "status", "reports"]) { @@ -144,4 +145,30 @@ describe("product feature residue (1105/16-04)", () => { "tests/unit/x.test.ts", ]); }); + + // 1112 round9 (1105/16-04 remainder). `src` was searched by case-insensitive + // substring over path and text, so a legitimate ID such as + // `product-feature` counted ten template files (`product-features.ts`, + // `product-features-port.ts` and their importers) as residue. + it("does not count the template's own source and tests for an ID that longer names contain", async () => { + expect(await productFeatureResidue(process.cwd(), "product-feature", "product-feature")).toEqual([]); + }); + + it("counts a source path or text that names the ID as a whole name, and ignores the generated index", async () => { + const root = await fixture({ + "src/contracts/leftover.ts": 'export const OWNER = "product-feature";\n', + "src/features/product-feature/stale.ts": "export const STALE = 1;\n", + "src/contracts/product-feature-port.ts": "export const PORT = 1;\n", + "src/contracts/product-features.ts": 'export const KEY = "data-product-feature";\n', + "src/contracts/title-case.ts": 'export const TITLE = "Product-Feature";\n', + "src/.generated/product-feature-index.ts": 'export const IDS = ["product-feature"];\n', + }); + + // A longer file name that starts with the ID is not the feature's file; + // the template's own tests/component/product-feature-switch.test.tsx is one. + expect(await productFeatureResidue(root, "product-feature", "product-feature")).toEqual([ + "src/contracts/leftover.ts", + "src/features/product-feature/stale.ts", + ]); + }); });