Skip to content

Commit

Permalink
#36, convert type node: Intersection, Tuple, TypeArgumented, Union.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajvincent committed Jan 10, 2024
1 parent a167694 commit a9d6e13
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 4 deletions.
17 changes: 15 additions & 2 deletions stage_1_integration/bootstrap/convertTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,23 @@ export default function convertTypeNode(
TupleTypeStructureImpl |
TypeArgumentedTypeStructureImpl |
undefined
) = undefined;
);

if (Node.isUnionTypeNode(typeNode)) {
parentStructure = new UnionTypeStructureImpl;
childTypeNodes = typeNode.getTypeNodes();
}
else if (Node.isIntersectionTypeNode(typeNode)) {
parentStructure = new IntersectionTypeStructureImpl;
childTypeNodes = typeNode.getTypeNodes();
}
else if (Node.isTupleTypeNode(typeNode)) {
parentStructure = new TupleTypeStructureImpl;
childTypeNodes = typeNode.getElements();
}

// identifiers, type-argumented type nodes
if (Node.isTypeReference(typeNode)) {
else if (Node.isTypeReference(typeNode)) {
const objectType = buildStructureForEntityName(typeNode.getTypeName());

childTypeNodes = typeNode.getTypeArguments();
Expand Down
17 changes: 15 additions & 2 deletions stage_2_fullset/snapshot/source/bootstrap/convertTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,23 @@ export default function convertTypeNode(
TupleTypeStructureImpl |
TypeArgumentedTypeStructureImpl |
undefined
) = undefined;
);

if (Node.isUnionTypeNode(typeNode)) {
parentStructure = new UnionTypeStructureImpl;
childTypeNodes = typeNode.getTypeNodes();
}
else if (Node.isIntersectionTypeNode(typeNode)) {
parentStructure = new IntersectionTypeStructureImpl;
childTypeNodes = typeNode.getTypeNodes();
}
else if (Node.isTupleTypeNode(typeNode)) {
parentStructure = new TupleTypeStructureImpl;
childTypeNodes = typeNode.getElements();
}

// identifiers, type-argumented type nodes
if (Node.isTypeReference(typeNode)) {
else if (Node.isTypeReference(typeNode)) {
const objectType = buildStructureForEntityName(typeNode.getTypeName());

childTypeNodes = typeNode.getTypeArguments();
Expand Down
66 changes: 66 additions & 0 deletions stage_2_fullset/spec-snapshot/source/bootstrap/convertTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import {
} from "ts-morph";

import {
IntersectionTypeStructureImpl,
ParenthesesTypeStructureImpl,
StringTypeStructureImpl,
TupleTypeStructureImpl,
TypeArgumentedTypeStructureImpl,
UnionTypeStructureImpl,
TypeStructures
} from "#stage_two/snapshot/source/exports.js";

Expand All @@ -25,6 +29,7 @@ import type {
} from "#stage_two/snapshot/source/bootstrap/types/conversions.js";

describe("convertTypeNode generates correct type structures, with type", () => {
//#region set-up, tear-down
let declaration: VariableDeclaration;
let structure: string | TypeStructures | null;

Expand Down Expand Up @@ -84,6 +89,9 @@ const A: string;
node => StructuresClassesMap.clone(node.getStructure())
);
}
//#endregion set-up, teardown

//#region literals

describe("string keyword", () => {
function stringSpec(s: string): void {
Expand Down Expand Up @@ -144,4 +152,62 @@ const A: string;
expect(failMessage).toBeUndefined();
expect(failNode).toBeNull();
});

//#endregion literals

//#region complex types
it("Intersection: of string and number", () => {
setTypeStructure("string & number", failCallback);
expect(structure).toBeInstanceOf(IntersectionTypeStructureImpl);
if (structure instanceof IntersectionTypeStructureImpl) {
expect(structure.childTypes.length).toBe(2);
expect(structure.childTypes[0]).toBe("string");
expect(structure.childTypes[1]).toBe("number");
}
expect(failMessage).toBe(undefined);
expect(failNode).toBe(null);
});

it("Tuple: [string, number]", () => {
setTypeStructure("[string, number]", failCallback);
expect(structure).toBeInstanceOf(TupleTypeStructureImpl);
if (structure instanceof TupleTypeStructureImpl) {
expect(structure.childTypes.length).toBe(2);
expect(structure.childTypes[0]).toBe("string");
expect(structure.childTypes[1]).toBe("number");
}
expect(failMessage).toBe(undefined);
expect(failNode).toBe(null);
});

it(`TypeArgumented: Pick<NumberStringType, "repeatForward">`, () => {
setTypeStructure(`Pick<NumberStringType, "repeatForward">`, failCallback);
expect(structure).toBeInstanceOf(TypeArgumentedTypeStructureImpl);
if (structure instanceof TypeArgumentedTypeStructureImpl) {
expect(structure.objectType).toBe("Pick");
expect(structure.childTypes.length).toBe(2);
expect(structure.childTypes[0]).toBe("NumberStringType");
expect(structure.childTypes[1]).toBeInstanceOf(StringTypeStructureImpl);
expect((structure.childTypes[1] as StringTypeStructureImpl).stringValue).toBe("repeatForward");
}
expect(failMessage).toBe(undefined);
expect(failNode).toBe(null);
});

it(`Union: of string and number`, () => {
setTypeStructure("string | number", failCallback);
expect(structure).toBeInstanceOf(UnionTypeStructureImpl);
if (structure instanceof UnionTypeStructureImpl) {
expect(structure.childTypes.length).toBe(2);
expect(structure.childTypes[0]).toBe("string");
expect(structure.childTypes[1]).toBe("number");
}
expect(failMessage).toBe(undefined);
expect(failNode).toBe(null);
});

xit("Class implements, interface extends", () => {
expect(1).toBe(2);
});
//#region complex types
});

0 comments on commit a9d6e13

Please sign in to comment.