From 42886742dc8db3bd6d9d22478f170a222a099f33 Mon Sep 17 00:00:00 2001 From: "Alexander J. Vincent" Date: Thu, 21 Mar 2024 21:30:55 -0700 Subject: [PATCH] #61, printing the Map interfaces. --- package.json | 2 +- .../use-cases/StringStringMap.ts | 135 ------------------ use-cases/build/StringStringMap.ts | 19 ++- .../build/{ => utilities}/sharedProject.ts | 10 +- .../build/utilities/typescript-builtins.ts | 41 ++++++ utilities/source/typescript-builtins.ts | 58 -------- 6 files changed, 68 insertions(+), 197 deletions(-) delete mode 100644 stage_3_snapshot/spec-snapshot/use-cases/StringStringMap.ts rename use-cases/build/{ => utilities}/sharedProject.ts (85%) create mode 100644 use-cases/build/utilities/typescript-builtins.ts delete mode 100644 utilities/source/typescript-builtins.ts diff --git a/package.json b/package.json index 5a49eb70..e2aba400 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "debug-stage-three-integration": "TSMS_STAGE='three' TSMS_DEBUG='stage_3_integration' node --import ./register-hooks.js ./build.ts", "debug-stage-three-test": "TSMS_STAGE='three' TSMS_DEBUG='stage_three_test' node --import ./register-hooks.js ./build.ts", "use-cases": "TSMS_STAGE='use-cases' node --import ./register-hooks.js ./build.ts", - "debug-use-cases": "TSMS_STAGE='use-cases' TSMS_DEBUG='use-cases' --import ./register-hooks.js ./build.ts", + "debug-use-cases": "TSMS_STAGE='use-cases' TSMS_DEBUG='use-cases' node --import ./register-hooks.js ./build.ts", "tsc-tasks": "tsc --project ./utilities/source/tasks/tsconfig.json", "postinstall": "patch-package", "is-repo-clean": "node --import ./register-hooks.js ./utilities/source/assertRepoIsClean.ts" diff --git a/stage_3_snapshot/spec-snapshot/use-cases/StringStringMap.ts b/stage_3_snapshot/spec-snapshot/use-cases/StringStringMap.ts deleted file mode 100644 index d8a4f977..00000000 --- a/stage_3_snapshot/spec-snapshot/use-cases/StringStringMap.ts +++ /dev/null @@ -1,135 +0,0 @@ -import path from "path"; - -import { - InterfaceDeclaration, - ModuleKind, - ModuleResolutionKind, - Project, - type ProjectOptions, - ScriptTarget, - StructureKind, - type SourceFile, -} from "ts-morph"; - -import { - ClassDeclarationImpl, - InterfaceDeclarationImpl, - LiteralTypeStructureImpl, - type TypeMemberImpl, - TypeMembersMap, - VoidTypeNodeToTypeStructureConsole, - getTypeAugmentedStructure, - ParameterDeclarationImpl, -} from "#stage_two/snapshot/source/exports.js"; - -import tempDirWithCleanup from "#utilities/source/tempDirWithCleanup.js"; - -import getTypeScriptNodes from "#utilities/source/typescript-builtins.js"; - -xit("Use cases: A two-keyed (string, string) map class", async () => { - const { - tempDir, - resolve: tempDirResolver, - promise: tempDirCleanup - } = await tempDirWithCleanup(); - - try { - let classModuleFile: SourceFile; - { - const pathToClassModule = path.join(tempDir, "StringStringMap.ts"); - const TSC_CONFIG: ProjectOptions = { - "compilerOptions": { - "lib": ["es2022"], - "module": ModuleKind.ESNext, - "target": ScriptTarget.ESNext, - "moduleResolution": ModuleResolutionKind.NodeNext, - "sourceMap": true, - "declaration": true, - }, - skipAddingFilesFromTsConfig: true, - skipFileDependencyResolution: true, - }; - - const project = new Project(TSC_CONFIG); - classModuleFile = project.createSourceFile(pathToClassModule, ` - interface StringStringKey { - readonly firstKey: string, - readonly secondKey: string - } - - class StringStringMap { - static #hashKeys(firstKey: string, secondKey: string): string { - return JSON.stringify({firstKey, secondKey}); - } - - static #parseKeys(hashedKey: string): [string, string] - { - const { firstKey, secondKey } = JSON.parse(hashedKey) as StringStringKey; - return [firstKey, secondKey]; - } - - readonly #hashMap = new Map; - } - `.trim()); - } - - const StringStringMap = getTypeAugmentedStructure( - classModuleFile.getClassOrThrow("StringStringMap"), - VoidTypeNodeToTypeStructureConsole, - true - ).rootStructure as ClassDeclarationImpl; - void(StringStringMap); - - let MapTypeMembers: TypeMembersMap; - { - const MapInterfaceNodes = getTypeScriptNodes( - sourceFile => sourceFile.getInterfaces().filter(ifc => ifc.getName() === "Map") - ).map(entry => entry[1]); - const MapInterfaceDecls = MapInterfaceNodes.map( - node => getTypeAugmentedStructure( - node, VoidTypeNodeToTypeStructureConsole, true - ).rootStructure as InterfaceDeclarationImpl - ); - - const tempTypeEntries: [string, TypeMemberImpl][] = MapInterfaceDecls.map( - decl => Array.from(TypeMembersMap.fromMemberedObject(decl).entries()) - ).flat(); - - MapTypeMembers = new TypeMembersMap(tempTypeEntries); - } - void(MapTypeMembers); - - // convert method parameters - MapTypeMembers.arrayOfKind(StructureKind.MethodSignature).forEach(method => { - /* blocked on type structure traversal: - forEach parameters[0].typeStructure - [Symbol.iterator] returnTypeStructure - entries returnTypeStructure - keys returnTypeStructure - */ - - // convert this to use traversal - - // eslint-disable-next-line @typescript-eslint/prefer-for-of - for (let i = 0; i < method.parameters.length; i++) { - const param: ParameterDeclarationImpl = method.parameters[i]; - if (param.typeStructure === LiteralTypeStructureImpl.get("K")) { - const firstKey = new ParameterDeclarationImpl("firstKey"); - firstKey.typeStructure = LiteralTypeStructureImpl.get("string"); - - const secondKey = new ParameterDeclarationImpl("secondKey"); - secondKey.typeStructure = LiteralTypeStructureImpl.get("string"); - method.parameters.splice(i, 1, firstKey, secondKey); - break; - } - } - /* - console.log(JSON.stringify(method, null, 2)); - */ - }); - } - finally { - tempDirResolver(undefined); - await tempDirCleanup; - } -}); diff --git a/use-cases/build/StringStringMap.ts b/use-cases/build/StringStringMap.ts index 78b1fe68..b87136e8 100644 --- a/use-cases/build/StringStringMap.ts +++ b/use-cases/build/StringStringMap.ts @@ -1,11 +1,18 @@ import path from "path"; + +import { + type InterfaceDeclaration, + type SourceFile, +} from "ts-morph"; + import { distDir, project, removeDistFile, //getExistingSourceFile, -} from "./sharedProject.js"; -import { SourceFile } from "ts-morph"; +} from "./utilities/sharedProject.js"; + +import getTypeScriptNodes from "./utilities/typescript-builtins.js"; export default async function buildStringStringMap(): Promise { @@ -40,6 +47,14 @@ export default class StringStringMap { `.trim() ); + /* What are we dealing with? */ + const MapInterfaceNodes = getTypeScriptNodes( + sourceFile => sourceFile.getInterfaces().filter(ifc => ifc.getName() === "Map") + ).map(entry => entry[1]); + for (const node of MapInterfaceNodes) { + console.log(node.print()); + } + await moduleFile.save(); return Promise.resolve(); } diff --git a/use-cases/build/sharedProject.ts b/use-cases/build/utilities/sharedProject.ts similarity index 85% rename from use-cases/build/sharedProject.ts rename to use-cases/build/utilities/sharedProject.ts index 41c1c863..23e0f1ba 100644 --- a/use-cases/build/sharedProject.ts +++ b/use-cases/build/utilities/sharedProject.ts @@ -5,9 +5,11 @@ import url from "url"; import { ModuleKind, ModuleResolutionKind, + type Node, Project, type ProjectOptions, ScriptTarget, + type Structures, SourceFile, } from "ts-morph"; @@ -24,8 +26,13 @@ const TSC_CONFIG: ProjectOptions = { skipFileDependencyResolution: true, }; -export const stageDir = path.normalize(path.join(url.fileURLToPath(import.meta.url), "../../")); +export interface NodeWithStructures extends Node { + getStructure(): Structures; +} + +export const stageDir = path.normalize(path.join(url.fileURLToPath(import.meta.url), "../../../")); export const distDir = path.join(stageDir, "dist"); +export const projectDir = path.dirname(stageDir); export const project = new Project(TSC_CONFIG); @@ -46,3 +53,4 @@ export function getExistingSourceFile( { return project.getSourceFile(absolutePathToFile) ?? project.addSourceFileAtPath(absolutePathToFile); } + diff --git a/use-cases/build/utilities/typescript-builtins.ts b/use-cases/build/utilities/typescript-builtins.ts new file mode 100644 index 00000000..46dc647f --- /dev/null +++ b/use-cases/build/utilities/typescript-builtins.ts @@ -0,0 +1,41 @@ +import fs from "fs/promises"; +import path from "path"; + +import { + type SourceFile +} from "ts-morph"; + +import { + NodeWithStructures, + project, + projectDir, +} from "./sharedProject.js"; + +const TYPESCRIPT_LIBS = path.join(projectDir, "node_modules/typescript/lib"); +const fileNames = (await fs.readdir(TYPESCRIPT_LIBS)).filter(f => /^lib\..*\.d.ts$/.test(f)).map(f => path.join(TYPESCRIPT_LIBS, f)); +const sourceFiles: readonly SourceFile[] = project.addSourceFilesAtPaths(fileNames); + +export default function getTypeScriptNodes< + NodeKind extends NodeWithStructures +> +( + callback: (sourceFile: SourceFile) => NodeKind[] +): [string, NodeKind][] +{ + return sourceFiles.map( + sourceFile => processSourceFile(sourceFile, callback) + ).flat(); +} + +function processSourceFile< + NodeKind extends NodeWithStructures +> +( + sourceFile: SourceFile, + callback: (sourceFile: SourceFile) => NodeKind[] +): [string, NodeKind][] +{ + const nodes = callback(sourceFile); + const pathToSourceFile = sourceFile.getFilePath(); + return nodes.map(node => [pathToSourceFile, node]); +} diff --git a/utilities/source/typescript-builtins.ts b/utilities/source/typescript-builtins.ts deleted file mode 100644 index 80b06682..00000000 --- a/utilities/source/typescript-builtins.ts +++ /dev/null @@ -1,58 +0,0 @@ -import fs from "fs/promises"; -import path from "path"; -import { - SourceFile -} from "ts-morph"; - -import { - performance -} from "perf_hooks"; - -import type { - NodeWithStructures -} from "./_types/NodeWithStructures.js"; - -import { - type ModuleSourceDirectory, - projectDir -} from "./AsyncSpecModules.js"; - -import getTS_SourceFile from "./getTS_SourceFile.js"; - -const TYPESCRIPT_LIBS: ModuleSourceDirectory = { - isAbsolutePath: true, - pathToDirectory: path.join(projectDir, "node_modules/typescript/lib") -}; - -const start = performance.now(); -const files = (await fs.readdir(TYPESCRIPT_LIBS.pathToDirectory)).filter(f => /^lib\..*\.d.ts$/.test(f)); -const sourceFiles: SourceFile[] = files.map( - file => getTS_SourceFile(TYPESCRIPT_LIBS, file) -); -const end = performance.now(); -console.log("time to load TypeScript lib files: " + (end - start) + "ms"); - -export default function getTypeScriptNodes< - NodeKind extends NodeWithStructures -> -( - callback: (sourceFile: SourceFile) => NodeKind[] -): [string, NodeKind][] -{ - return sourceFiles.map( - sourceFile => processSourceFile(sourceFile, callback) - ).flat(); -} - -function processSourceFile< - NodeKind extends NodeWithStructures -> -( - sourceFile: SourceFile, - callback: (sourceFile: SourceFile) => NodeKind[] -): [string, NodeKind][] -{ - const nodes = callback(sourceFile); - const pathToSourceFile = sourceFile.getFilePath(); - return nodes.map(node => [pathToSourceFile, node]); -}