Skip to content

Commit

Permalink
#76, convergence between stages two and three, part 6.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajvincent committed Mar 4, 2024
1 parent f827313 commit 211168c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 28 deletions.
22 changes: 12 additions & 10 deletions stage_2_generation/build/hooks/classProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
CodeBlockWriter,
JSDocStructure,
StructureKind,
WriterFunction
} from "ts-morph";

import {
Expand All @@ -25,6 +24,8 @@ import {
TypeStructureKind,
TypeStructures,
UnionTypedStructureImpl,
stringOrWriterFunction,
StatementStructureImpls,
} from "#stage_one/prototype-snapshot/exports.js";

import StructureDictionaries, {
Expand Down Expand Up @@ -127,26 +128,27 @@ function addStructureFieldArray(
valueInUnion => valueInUnion.structureName && dictionaries.structures.has(valueInUnion.structureName)
);

let statement: WriterFunction;
let statements: (stringOrWriterFunction | StatementStructureImpls)[] = [];

if (hasAStructure) {
if (propertyValue.otherTypes.length === 2) {
statement = write_cloneRequiredAndOptionalArray(
statements.push(write_cloneRequiredAndOptionalArray(
structureName, dictionaries, parts, propertyValue, propertyKey
);
));
}
else {
statement = write_cloneStructureArray(
statements.push(write_cloneStructureArray(
structureName, dictionaries, parts, propertyValue, propertyKey
);
));
}
}
else if (propertyKey === "statements") {
statement = write_cloneStatementsArray(
statements = write_cloneStatementsArray(
structureName, dictionaries, parts, propertyValue, propertyKey
);
}
else {
statement = (writer: CodeBlockWriter): void => {
statements.push((writer: CodeBlockWriter): void => {
writer.write(`if (Array.isArray(source.${propertyKey})) `);
writer.block(() => {
writer.write(`target.${propertyKey}.push(...source.${propertyKey});`);
Expand All @@ -155,10 +157,10 @@ function addStructureFieldArray(
writer.block(() => {
writer.write(`target.${propertyKey}.push(source.${propertyKey});`)
});
};
});
}

parts.classFieldsStatements.set(propertyKey, COPY_FIELDS_NAME, [statement]);
parts.classFieldsStatements.set(propertyKey, COPY_FIELDS_NAME, statements);
parts.classFieldsStatements.set(
propertyKey, ClassFieldStatementsMap.GROUP_INITIALIZER_OR_PROPERTY, ["[]"]
);
Expand Down
64 changes: 48 additions & 16 deletions stage_2_generation/build/utilities/write_cloneArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import assert from "node:assert/strict";
import {
CodeBlockWriter,
StructureKind,
VariableDeclarationKind,
WriterFunction,
} from 'ts-morph';

Expand All @@ -12,9 +13,13 @@ import {
MethodDeclarationImpl,
ParameterDeclarationImpl,
ParenthesesTypedStructureImpl,
type StatementStructureImpls,
TypeStructureClassesMap,
TypeStructureKind,
UnionTypedStructureImpl,
VariableDeclarationImpl,
VariableStatementImpl,
type stringOrWriterFunction,
} from "#stage_one/prototype-snapshot/exports.js";

import StructureDictionaries, {
Expand All @@ -32,6 +37,7 @@ import type {
} from '../structureMeta/DataClasses.js';

import pairedWrite from "./pairedWrite.js";
import ConstantTypeStructures from "./ConstantTypeStructures.js";

export function write_cloneRequiredAndOptionalArray(
structureName: string,
Expand Down Expand Up @@ -224,7 +230,7 @@ export function write_cloneStatementsArray(
parts: DecoratorParts | StructureParts,
propertyValue: PropertyValue,
propertyKey: PropertyName
): WriterFunction {
): (StatementStructureImpls | stringOrWriterFunction)[] {
void (propertyKey);
void (propertyValue);

Expand Down Expand Up @@ -280,6 +286,7 @@ export function write_cloneStatementsArray(
returnType = TypeStructureClassesMap.clone(unionType) as UnionTypedStructureImpl;
}

// this is a hack, since we should be defining the method and its statements elsewhere!
const cloneStatementMethod = new MethodDeclarationImpl("#cloneStatement");
cloneStatementMethod.isStatic = true;
const sourceParam = new ParameterDeclarationImpl("source");
Expand All @@ -290,24 +297,49 @@ export function write_cloneStatementsArray(
cloneStatementMethod.statements.push(writer => {
writer.write(`if (typeof source !== "object") `);
writer.block(() => writer.write("return source;"));
});
cloneStatementMethod.statements.push(writer => {
writer.writeLine(`return StructureClassesMap.clone<StatementStructures, StatementStructureImpls>(source);`);
});

parts.classMembersMap.addMembers([cloneStatementMethod]);

return (writer: CodeBlockWriter) => {
// this is one time where it's just faster and clearer to write the code than to spell out the contents in structures
writer.write(`
let statementsArray: (StatementStructureImpls | stringOrWriterFunction)[] = [];
if (Array.isArray(source.statements)) {
statementsArray = source.statements as (StatementStructureImpls | stringOrWriterFunction)[];
}
else if (source.statements !== undefined) {
statementsArray = [source.statements];
}
target.statements.push(
...statementsArray.map(statement => StatementedNodeStructureMixin.#cloneStatement(statement))
);
`);
};
const statements: (StatementStructureImpls | stringOrWriterFunction)[] = [];

const statementsArrayLet = new VariableStatementImpl;
statements.push(statementsArrayLet);
statementsArrayLet.declarationKind = VariableDeclarationKind.Let;

const statementsArrayDecl = new VariableDeclarationImpl("statementsArray");
statementsArrayDecl.typeStructure = new ArrayTypedStructureImpl(
new ParenthesesTypedStructureImpl(
new UnionTypedStructureImpl([
new LiteralTypedStructureImpl("StatementStructureImpls"),
ConstantTypeStructures.stringOrWriterFunction
])
)
);
statementsArrayDecl.initializer = "[]";
statementsArrayLet.declarations.push(statementsArrayDecl);

// trying to make this consistent with later work... this actually makes whitespace consistent
statements.push(
(writer: CodeBlockWriter) => {
writer.write(`if (Array.isArray(source.statements)) {
statementsArray = source.statements as (StatementStructureImpls | stringOrWriterFunction)[];
}`);
},
(writer: CodeBlockWriter): void => {
writer.write(`else if (source.statements !== undefined) {
statementsArray = [source.statements];
}`);
},
(writer: CodeBlockWriter): void => {
writer.write(`target.statements.push(
...statementsArray.map(statement => StatementedNodeStructureMixin.#cloneStatement(statement))
);`);
}
)

return statements;
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export default function StatementedNodeStructureMixin(
target: StatementedNodeStructureMixin & Structures,
): void {
super[COPY_FIELDS](source, target);

let statementsArray: (
| StatementStructureImpls
| stringOrWriterFunction
Expand All @@ -66,6 +65,7 @@ export default function StatementedNodeStructureMixin(
} else if (source.statements !== undefined) {
statementsArray = [source.statements];
}

target.statements.push(
...statementsArray.map((statement) =>
StatementedNodeStructureMixin.#cloneStatement(statement),
Expand All @@ -79,6 +79,7 @@ export default function StatementedNodeStructureMixin(
if (typeof source !== "object") {
return source;
}

return StructureClassesMap.clone<
StatementStructures,
StatementStructureImpls
Expand Down
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 @@ -44,7 +44,7 @@ describe("File hashes match for the", () => {
await compareSnapshots("source/bootstrap");
});

xit("decorators", async () => {
it("standard decorators", async () => {
await compareSnapshots("source/decorators/standard");
});

Expand Down

0 comments on commit 211168c

Please sign in to comment.