Skip to content

Commit

Permalink
#94, code review and test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ajvincent committed Jul 7, 2024
1 parent 9c1cb72 commit 649d459
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 19 deletions.
5 changes: 2 additions & 3 deletions stage_2_integration/source/bootstrap/adjustForOverloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
forEachStructureChild,
} from "ts-morph";

import {
import type {
IterableElement
} from "type-fest";

Expand Down Expand Up @@ -128,8 +128,7 @@ class CallableDescription<

type LastCallableWrapper<
DeclarationStructure extends FunctionDeclarationStructure | MethodDeclarationStructure | ConstructorDeclarationStructure
>
= {
> = {
lastCallable: CallableDescription<DeclarationStructure> | undefined;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
forEachStructureChild,
} from "ts-morph";

import { IterableElement } from "type-fest";
import type { IterableElement } from "type-fest";

//#region move structure overloads inside their parent structures

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
ConstructorDeclaration,
FunctionDeclaration,
type ConstructorDeclaration,
type FunctionDeclaration,
type MethodDeclaration,
ModuleKind,
ModuleDeclarationKind,
Expand All @@ -11,9 +11,9 @@ import {
type SourceFile,
type SourceFileStructure,
StructureKind,
StatementStructures,
WriterFunction,
type StatementStructures,
SyntaxKind,
type WriterFunction,
} from "ts-morph";

import {
Expand Down
107 changes: 97 additions & 10 deletions stage_2_snapshot/spec-snapshot/source/bootstrap/structureToNodeMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ import fs from "fs/promises";
import path from "path";

import {
type ConstructorDeclarationOverloadStructure,
type ConstructorDeclarationStructure,
type FunctionDeclarationOverloadStructure,
type FunctionDeclarationStructure,
type MethodDeclarationOverloadStructure,
type MethodDeclarationStructure,
ModuleKind,
ModuleResolutionKind,
Node,
Project,
ProjectOptions,
ScriptTarget,
StructureKind,
Structures,
type Structures,
SyntaxKind,
} from "ts-morph";

Expand Down Expand Up @@ -101,20 +107,101 @@ it("structureToNodeMap returns an accurate Map<Structure, Node>", () => {
throw ex;
}

map.forEach((node, structure) => {
map.forEach((node: Node, structure: Structures) => {
expect(Node.hasStructure(node)).withContext(relativePathToModuleFile).toBe(true);
/* This is an expensive check, but it's important. I am checking that _every_ node with a
if (Node.hasStructure(node) === false)
return;

/* This is an expensive check (O(n^2)), but it's important. I am checking that _every_ node with a
getStructure() method returns an equivalent (or in the case of overloads, nearly so) structure
to what we get from the structure-to-node map.
Note I am excluding overloaded functions inside functions. That's just not fair for this test.
*/
if (Node.isSourceFile(node)) {
const expectedStructure = node.getStructure();
fixFunctionOverloads(expectedStructure);
const expectedStructure = node.getStructure();
switch (structure.kind) {
case StructureKind.Constructor: {
expect(expectedStructure.kind).withContext(
`with kind ${StructureKind[structure.kind]} at ${pathToModuleFile}#${sourceFile.getLineAndColumnAtPos(node.getPos()).line}`
).toBe(StructureKind.Constructor);

if ((expectedStructure as ConstructorDeclarationStructure).overloads === undefined)
delete structure.overloads;
break;
}

case StructureKind.ConstructorOverload: {
if (expectedStructure.kind !== StructureKind.ConstructorOverload)
expect(expectedStructure.kind).withContext(
`with kind ${StructureKind[structure.kind]} at ${pathToModuleFile}#${sourceFile.getLineAndColumnAtPos(node.getPos()).line}`
).toBe(StructureKind.Constructor);

const expectedOverload = (expectedStructure as ConstructorDeclarationOverloadStructure);
expectedOverload.kind = StructureKind.ConstructorOverload;
Reflect.deleteProperty(expectedOverload, "overloads");
Reflect.deleteProperty(expectedOverload, "statements");
break;
}

case StructureKind.Function: {
expect(expectedStructure.kind).withContext(
`with kind ${StructureKind[structure.kind]} at ${pathToModuleFile}#${sourceFile.getLineAndColumnAtPos(node.getPos()).line}`
).toBe(StructureKind.Function);

if ((expectedStructure as FunctionDeclarationStructure).overloads === undefined)
delete structure.overloads;
break;
}

case StructureKind.FunctionOverload: {
if (expectedStructure.kind !== StructureKind.FunctionOverload)
expect(expectedStructure.kind).withContext(
`with kind ${StructureKind[structure.kind]} at ${pathToModuleFile}#${sourceFile.getLineAndColumnAtPos(node.getPos()).line}`
).toBe(StructureKind.Function);

const expectedOverload = (expectedStructure as FunctionDeclarationOverloadStructure);
expectedOverload.kind = StructureKind.FunctionOverload;
Reflect.deleteProperty(expectedOverload, "name");
Reflect.deleteProperty(expectedOverload, "overloads");
Reflect.deleteProperty(expectedOverload, "statements");

break;
}

case StructureKind.Method: {
expect(expectedStructure.kind).withContext(
`with kind ${StructureKind[structure.kind]} at ${pathToModuleFile}#${sourceFile.getLineAndColumnAtPos(node.getPos()).line}`
).toBe(StructureKind.Method);

if ((expectedStructure as MethodDeclarationStructure).overloads === undefined)
delete structure.overloads;
break;
}

case StructureKind.MethodOverload: {
if (expectedStructure.kind !== StructureKind.MethodOverload)
expect(expectedStructure.kind).withContext(
`with kind ${StructureKind[structure.kind]} at ${pathToModuleFile}#${sourceFile.getLineAndColumnAtPos(node.getPos()).line}`
).toBe(StructureKind.Method);

const expectedOverload = (expectedStructure as MethodDeclarationOverloadStructure);
expectedOverload.kind = StructureKind.MethodOverload;
Reflect.deleteProperty(expectedOverload, "decorators");
Reflect.deleteProperty(expectedOverload, "name");
Reflect.deleteProperty(expectedOverload, "overloads");
Reflect.deleteProperty(expectedOverload, "statements");
break;
}

default:
fixFunctionOverloads(expectedStructure);
}

expect<Structures>(structure).withContext(
`at ${pathToModuleFile}#${sourceFile.getLineAndColumnAtPos(node.getPos()).line}`
expect<Structures>(structure)
.withContext(
`with kind ${StructureKind[structure.kind]} at ${pathToModuleFile}#${sourceFile.getLineAndColumnAtPos(node.getPos()).line}`
).toEqual(expectedStructure);
}

remainingKeys.delete(structure.kind);
});

Expand Down Expand Up @@ -159,4 +246,4 @@ it("structureToNodeMap returns an accurate Map<Structure, Node>", () => {
remainingKinds.sort();

expect(remainingKinds).withContext("we didn't find examples of these kinds in the fixtures").toEqual([]);
}, 1000 * 60 * 60);
});
2 changes: 1 addition & 1 deletion stage_3_snapshot/spec-snapshot/fileHashes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe("File hashes match for", () => {
await compareOneSnapshot("source/internal-exports.ts");
});

xit("distributed files", async () => {
it("distributed files", async () => {
const stage_two_dir = path.join(projectDir, "dist");
const stage_three_dir = path.join(stage_three_snapshot, "dist");

Expand Down

0 comments on commit 649d459

Please sign in to comment.