From 45f77c00c6d4384d466c39e8eb633a76f33dd019 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Wed, 17 Jul 2024 10:29:19 -0400 Subject: [PATCH] chore: correct .gitignore --- .gitignore | 2 +- knip.json | 5 - src/creators/cases/createEvenCaseFiles.ts | 84 ++++++++++++ .../cases/createReferencesCaseFiles.ts | 125 ++++++++++++++++++ src/creators/cases/createWideCaseFiles.ts | 58 ++++++++ 5 files changed, 268 insertions(+), 6 deletions(-) create mode 100644 src/creators/cases/createEvenCaseFiles.ts create mode 100644 src/creators/cases/createReferencesCaseFiles.ts create mode 100644 src/creators/cases/createWideCaseFiles.ts diff --git a/.gitignore b/.gitignore index 04fb7da..74c1f07 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -cases/ +cases/files-* node_modules/ package-lock.json diff --git a/knip.json b/knip.json index b302642..112398f 100644 --- a/knip.json +++ b/knip.json @@ -1,11 +1,6 @@ { "$schema": "https://unpkg.com/knip@latest/schema.json", "entry": ["./src/*.ts"], - "ignore": [ - "src/creators/files/createESLintConfigFile.ts", - "src/creators/files/createStandardTSConfigFile.ts", - "src/creators/utils.ts" - ], "ignoreExportsUsedInFile": { "interface": true, "type": true }, "ignoreWorkspaces": ["cases/**"] } diff --git a/src/creators/cases/createEvenCaseFiles.ts b/src/creators/cases/createEvenCaseFiles.ts new file mode 100644 index 0000000..f7c0b3b --- /dev/null +++ b/src/creators/cases/createEvenCaseFiles.ts @@ -0,0 +1,84 @@ +import { CaseData } from "../../data.js"; +import { Structure } from "../../writing/writeStructure.js"; +import { createESLintConfigFile } from "../files/createESLintConfigFile.js"; +import { createStandardTSConfigFile } from "../files/createStandardTSConfigFile.js"; +import { range } from "../utils.js"; + +function createExampleFile(index: number) { + return [ + index > 1 && + range(1, index) + .map((i) => `export * as nested${i} from "./nested${i}/index.js";`) + .join("\n\t\t"), + ` + export async function example${index}(prefix: string) { + await Promise.resolve(); + return prefix + "" + ${index}; + } + `, + ] + .filter(Boolean) + .join("\n\n"); +} + +function createExampleDirectory(index: number): Structure { + return { + "index.ts": [createExampleFile(index), "typescript"], + ...(index > 2 && + Object.fromEntries( + range(1, index).map((i) => [ + `nested${i}`, + createExampleDirectory(i - 1), + ]), + )), + }; +} + +function createIndexFile(count: number) { + const indices = range(0, count); + + return ` + import { example0 } from "./example0/index.js"; + + export async function root() { + // Lint report: no-floating-promises + example0(""); + + // No lint report + await example0(""); + } + + ${indices.map((index) => `export { example${index} } from "./example${index}/index.js";`).join("\n\t\t")} + `; +} + +export function writeEvenCaseFiles(data: CaseData): Structure { + const topLevelWidth = Math.floor(Math.log(data.files) * 1.7); + + return { + "eslint.config.js": [ + createESLintConfigFile({ + singleRun: data.singleRun, + types: + data.types === "service" + ? "projectService" + : data.layout === "references" + ? "tsconfig.eslint.json" + : true, + }), + "typescript", + ], + src: { + "index.ts": [createIndexFile(topLevelWidth), "typescript"], + ...Object.fromEntries( + new Array(topLevelWidth) + .fill(undefined) + .map((_, index) => [ + `example${index}`, + createExampleDirectory(index), + ]), + ), + }, + "tsconfig.json": [createStandardTSConfigFile(), "json"], + }; +} diff --git a/src/creators/cases/createReferencesCaseFiles.ts b/src/creators/cases/createReferencesCaseFiles.ts new file mode 100644 index 0000000..b3d89e6 --- /dev/null +++ b/src/creators/cases/createReferencesCaseFiles.ts @@ -0,0 +1,125 @@ +import { CaseData } from "../../data.js"; +import { Structure } from "../../writing/writeStructure.js"; +import { createESLintConfigFile } from "../files/createESLintConfigFile.js"; +import { createStandardTSConfigFile } from "../files/createStandardTSConfigFile.js"; +import { range } from "../utils.js"; + +function createExampleFile(index: number) { + return [ + index > 2 && + range(1, index) + .map((i) => `export * as nested${i} from "./nested${i}/index.js";`) + .join("\n\t\t"), + ` + export async function example${index}(prefix: string) { + await Promise.resolve(); + return prefix + "" + ${index}; + } + `, + ] + .filter(Boolean) + .join("\n\n"); +} + +function createIndexFile(count: number) { + const indices = count > 1 ? range(0, count - 1) : []; + + return ` + import { example0 } from "./nested0/index.js"; + + export async function root() { + // Lint report: no-floating-promises + example0(""); + + // No lint report + await example0(""); + } + + ${indices.map((index) => `export * as nested${index} from "./nested${index}/index.js";`).join("\n\t\t")} + `; +} + +function createNestedDirectory(index: number): Structure { + return { + "index.ts": [createExampleFile(index), "typescript"], + ...(index > 2 && + Object.fromEntries( + range(1, index).map((i) => [ + `nested${i}`, + createNestedDirectory(i - 1), + ]), + )), + }; +} + +function createProjectDirectory(index: number): Structure { + return { + src: { + "index.ts": [createIndexFile(index), "typescript"], + ...(index > 2 && + Object.fromEntries( + range(0, index - 1).map((i) => [ + `nested${i}`, + createNestedDirectory(i), + ]), + )), + }, + "tsconfig.json": [ + { + extends: "../../tsconfig.build.json", + include: ["src"], + }, + "json", + ], + }; +} + +export function createReferencesCaseFiles(data: CaseData): Structure { + const topLevelWidth = Math.ceil( + Math.log(data.files) * (data.files > 1000 ? 1.6 : 1.7), + ); + const projectNames = range(0, topLevelWidth).map((i) => `project-${i}`); + + return { + "eslint.config.js": [ + createESLintConfigFile({ + singleRun: data.singleRun, + types: + data.types === "service" + ? "projectService" + : data.layout === "references" + ? "tsconfig.eslint.json" + : true, + }), + "typescript", + ], + "tsconfig.build.json": [ + { ...createStandardTSConfigFile(), include: undefined }, + "json", + ], + ...(data.types === "service" + ? { + "tsconfig.json": [ + { + include: [], + references: projectNames.map((projectName) => ({ + path: `./src/${projectName}`, + })), + }, + "json", + ], + } + : { + "tsconfig.eslint.json": [createStandardTSConfigFile(), "json"], + "tsconfig.json": [createStandardTSConfigFile(), "json"], + }), + src: { + ...Object.fromEntries( + projectNames.map((projectName, index) => [ + projectName, + createProjectDirectory(index), + ]), + ), + }, + }; +} diff --git a/src/creators/cases/createWideCaseFiles.ts b/src/creators/cases/createWideCaseFiles.ts new file mode 100644 index 0000000..24a469a --- /dev/null +++ b/src/creators/cases/createWideCaseFiles.ts @@ -0,0 +1,58 @@ +import { CaseData } from "../../data.js"; +import { Structure } from "../../writing/writeStructure.js"; +import { createESLintConfigFile } from "../files/createESLintConfigFile.js"; +import { createStandardTSConfigFile } from "../files/createStandardTSConfigFile.js"; + +function createExampleFile(index: number) { + return ` + export async function example${index}(prefix: string) { + await Promise.resolve(); + return prefix + "" + ${index}; + } + `; +} + +function createIndexFile(count: number) { + const indices = new Array(count - 1).fill(undefined).map((_, index) => index); + + return ` + ${indices.map((index) => `import { example${index} } from "./example${index}.js";`).join("\n\t\t")} + + export async function root() { + // Lint report: no-floating-promises + example0(""); + + // No lint reports + ${indices.map((index) => `await example${index}("");`).join("\n\t\t\t")} + } + `; +} + +export function writeWideCaseFiles(data: CaseData): Structure { + return { + "eslint.config.js": [ + createESLintConfigFile({ + singleRun: data.singleRun, + types: + data.types === "service" + ? "projectService" + : data.layout === "references" + ? "tsconfig.eslint.json" + : true, + }), + "typescript", + ], + src: { + "index.ts": [createIndexFile(data.files), "typescript"], + ...Object.fromEntries( + new Array(data.files - 1) + .fill(undefined) + .map((_, index) => [ + `example${index}.ts`, + [createExampleFile(index), "typescript"], + ]), + ), + }, + "tsconfig.json": [createStandardTSConfigFile(), "json"], + }; +}