Skip to content

Commit

Permalink
#43, new SetAccessorDeclarationImpl requires a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
ajvincent committed Jan 13, 2024
1 parent 2c89649 commit f4eb0d6
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 10 deletions.
2 changes: 1 addition & 1 deletion stage_1_bootstrap/build/hooks/structure/addStaticClone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default function addStaticClone(
}

classFieldsStatements.set(
ClassFieldStatementsMap.FIELD_HEAD_SUPER_CALL,
"(body)",
ClassMembersMap.keyFromMember(cloneMethod),
[
`const target = new ${classDecl.name!}(${constructorArgs.join(", ")});`,
Expand Down
66 changes: 66 additions & 0 deletions stage_1_bootstrap/build/hooks/structure/specialCases.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
StructureKind,
VariableDeclarationKind,
} from "ts-morph";

import StructureDictionaries, {
Expand All @@ -10,6 +11,13 @@ import {
StructureImplMeta
} from "#stage_one/build/structureMeta/DataClasses.js";

import {
LiteralTypedStructureImpl,
ParameterDeclarationImpl,
VariableDeclarationImpl,
VariableStatementImpl,
} from "#stage_one/prototype-snapshot/exports.js";

import ClassFieldStatementsMap from "#stage_one/build/utilities/public/ClassFieldStatementsMap.js";
import ClassMembersMap from "#stage_one/build/utilities/public/ClassMembersMap.js";

Expand All @@ -24,6 +32,10 @@ export default function structureSpecialCases(
case "IndexSignatureDeclarationImpl":
fixKeyTypeField(parts, dictionaries);
break;

case "SetAccessorDeclarationImpl":
addParameterToSetAccessorCtor(parts, dictionaries);
break;
}
return Promise.resolve();
}
Expand Down Expand Up @@ -77,3 +89,57 @@ function fixKeyTypeField(
]
);
}

function addParameterToSetAccessorCtor(
parts: StructureParts,
dictionaries: StructureDictionaries
): void
{
parts.importsManager.addImports({
pathToImportedModule: dictionaries.publicExports.absolutePathToExportFile,
isPackageImport: false,
importNames: ["ParameterDeclarationImpl"],
isDefaultImport: false,
isTypeOnly: false
});

const setterParam = new ParameterDeclarationImpl("setterParameter");
setterParam.typeStructure = new LiteralTypedStructureImpl("ParameterDeclarationImpl");

const ctor = parts.classMembersMap.getAsKind<StructureKind.Constructor>(
"constructor", StructureKind.Constructor
)!;
ctor.parameters.push(setterParam);

parts.classFieldsStatements.set("setterParameter", "constructor", [
"this.parameters.push(setterParameter);"
]);

const cloneStatements = parts.classFieldsStatements.get(
"(body)", "static clone"
)!;

const valueParamStatement = new VariableStatementImpl();
valueParamStatement.declarationKind = VariableDeclarationKind.Const;
const valueParamDeclaration = new VariableDeclarationImpl("valueParam");
valueParamDeclaration.type = "ParameterDeclarationImpl";
valueParamDeclaration.initializer = `new ParameterDeclarationImpl("value");`;
valueParamStatement.declarations.push(valueParamDeclaration);

cloneStatements.splice(
0, 1,
// harder to read, I know, but also a valuable unit test
valueParamStatement,
`const hasSourceParameter = source.parameters && source.parameters.length > 0;`,

// `const target = new ${classDecl.name!}(${constructorArgs.join(", ")});`,
(cloneStatements[0] as string).replace("source.name", "source.name, valueParam")
);

parts.classFieldsStatements.set("valueParam", "static clone", [
`if (hasSourceParameter) {
// copy-fields included copying the existing parameter, so we have to drop our artificial one
target.parameters.shift();
}`
]);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//#region preamble
import { ParameterDeclarationImpl } from "../exports.js";
import {
type AbstractableNodeStructureFields,
AbstractableNodeStructureMixin,
Expand Down Expand Up @@ -78,20 +79,36 @@ export default class SetAccessorDeclarationImpl
readonly kind: StructureKind.SetAccessor = StructureKind.SetAccessor;
readonly isStatic: boolean;

constructor(isStatic: boolean, name: string) {
constructor(
isStatic: boolean,
name: string,
setterParameter: ParameterDeclarationImpl,
) {
super();
this.isStatic = isStatic;
this.name = name;
this.parameters.push(setterParameter);
}

public static clone(
source: OptionalKind<SetAccessorDeclarationStructure>,
): SetAccessorDeclarationImpl {
const valueParam: ParameterDeclarationImpl = new ParameterDeclarationImpl(
"value",
);
const hasSourceParameter =
source.parameters && source.parameters.length > 0;
const target = new SetAccessorDeclarationImpl(
source.isStatic ?? false,
source.name,
valueParam,
);
this[COPY_FIELDS](source, target);
if (hasSourceParameter) {
// copy-fields included copying the existing parameter, so we have to drop our artificial one
target.parameters.shift();
}

return target;
}

Expand Down
12 changes: 5 additions & 7 deletions stage_2_fullset/spec-snapshot/source/toolbox/ClassMembersMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ describe("ClassMembersMap", () => {
method3 = new MethodDeclarationImpl(true, "three");
method4 = new MethodDeclarationImpl(false, "four");

const value_five = new ParameterDeclarationImpl("five");
value_five.typeStructure = "string";

getter5 = new GetAccessorDeclarationImpl(false, "five");
setter5 = new SetAccessorDeclarationImpl(false, "five");
{
const param = new ParameterDeclarationImpl("value");
param.typeStructure = "string";
setter5.parameters.push(param);
}
setter5 = new SetAccessorDeclarationImpl(false, "five", value_five);
});

it("ClassMembersMap allows us to organize class members by kind", () => {
Expand Down Expand Up @@ -133,7 +131,7 @@ describe("ClassMembersMap", () => {
expect(method4.statements).toEqual([`console.log(this.one);`]);

expect(getter5.statements).toEqual([`return this.#five;`]);
expect(setter5.statements).toEqual([`this.#five = value;`]);
expect(setter5.statements).toEqual([`this.#five = five;`]);
});

it("ClassMembersMap static methods give us keys we can use in the map", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
import {
GetAccessorDeclarationImpl,
MethodSignatureImpl,
ParameterDeclarationImpl,
PropertySignatureImpl,
SetAccessorDeclarationImpl,
TypeMembersMap,
Expand All @@ -17,7 +18,7 @@ it("TypeMembersMap allows us to organize type members by kind", () => {

const method3 = new MethodSignatureImpl("three");
const getter4 = new GetAccessorDeclarationImpl(false, "four");
const setter4 = new SetAccessorDeclarationImpl(false, "four");
const setter4 = new SetAccessorDeclarationImpl(false, "four", new ParameterDeclarationImpl("value"));

memberMap.addMembers([
prop1, prop2, method3, getter4, setter4
Expand Down

0 comments on commit f4eb0d6

Please sign in to comment.