-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#13, ConstructorDeclarationImpl.fromSignature()
- Loading branch information
Showing
6 changed files
with
285 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
stage_1_bootstrap/build/hooks/structure/addDeclarationFromSignature.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* | ||
import { | ||
StructureKind, | ||
VariableDeclarationKind, | ||
} from "ts-morph"; | ||
*/ | ||
|
||
import StructureDictionaries, { | ||
type StructureParts | ||
} from "#stage_one/build/StructureDictionaries.js"; | ||
|
||
import { | ||
StructureImplMeta | ||
} from "#stage_one/build/structureMeta/DataClasses.js"; | ||
|
||
import { | ||
LiteralTypedStructureImpl, | ||
MethodDeclarationImpl, | ||
ParameterDeclarationImpl, | ||
/* | ||
UnionTypedStructureImpl, | ||
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"; | ||
import { Scope } from "ts-morph"; | ||
//import ConstantTypeStructures from "#stage_one/build/utilities/ConstantTypeStructures.js"; | ||
|
||
export default function addDeclarationFromSignature( | ||
name: string, | ||
meta: StructureImplMeta, | ||
dictionaries: StructureDictionaries | ||
): Promise<void> | ||
{ | ||
const parts = dictionaries.structureParts.get(meta)!; | ||
switch (parts.classDecl.name) { | ||
case "ConstructorDeclarationImpl": | ||
convertConstructor(parts, dictionaries); | ||
break; | ||
case "MethodDeclarationImpl": | ||
/* | ||
declarationFromSignature( | ||
parts, | ||
dictionaries.structureParts.get("MethodSignatureImpl"), | ||
dictionaries | ||
); | ||
*/ | ||
break; | ||
case "PropertyDeclarationImpl": | ||
/* | ||
declarationFromSignature( | ||
parts, | ||
dictionaries.structureParts.get("PropertySignatureImpl"); | ||
dictionaries | ||
); | ||
*/ | ||
break; | ||
} | ||
return Promise.resolve(); | ||
} | ||
|
||
function convertConstructor( | ||
parts: StructureParts, | ||
dictionaries: StructureDictionaries | ||
): void | ||
{ | ||
addImport(parts, dictionaries, false, false, "cloneStructureOrStringArray"); | ||
addImport(parts, dictionaries, true, true, "JSDocImpl"); | ||
addImport(parts, dictionaries, true, true, "ParameterDeclarationImpl"); | ||
addImport(parts, dictionaries, true, true, "TypeParameterDeclarationImpl"); | ||
addImport(parts, dictionaries, false, false, "TypeStructureClassesMap"); | ||
const signatureImplClass = "ConstructSignatureDeclarationImpl"; | ||
|
||
const groupName = createFromSignatureMethod(signatureImplClass, parts, dictionaries); | ||
parts.classFieldsStatements.set( | ||
ClassFieldStatementsMap.FIELD_HEAD_SUPER_CALL, groupName, [ | ||
`const declaration = new ${parts.classDecl.name};` | ||
] | ||
); | ||
parts.classFieldsStatements.set( | ||
ClassFieldStatementsMap.FIELD_TAIL_FINAL_RETURN, groupName, [ | ||
`return declaration;` | ||
] | ||
); | ||
|
||
parts.classFieldsStatements.set("docs", groupName, [ | ||
`declaration.docs.push(...cloneStructureOrStringArray< | ||
JSDocImpl, | ||
StructureKind.JSDoc, | ||
JSDocImpl | ||
>( | ||
signature.docs as (string | JSDocImpl)[], | ||
StructureKind.JSDoc | ||
) as JSDocImpl[]);`, | ||
]); | ||
|
||
parts.classFieldsStatements.set("leadingTrivia", groupName, [ | ||
`declaration.leadingTrivia.push(...signature.leadingTrivia);`, | ||
]); | ||
parts.classFieldsStatements.set("trailingTrivia", groupName, [ | ||
`declaration.trailingTrivia.push(...signature.trailingTrivia);`, | ||
]); | ||
|
||
parts.classFieldsStatements.set("typeParameters", groupName, [ | ||
`declaration.typeParameters.push( | ||
...StructuresClassesMap.cloneArray( | ||
signature.typeParameters as TypeParameterDeclarationImpl[] | ||
) as TypeParameterDeclarationImpl[], | ||
);` | ||
]); | ||
parts.classFieldsStatements.set("parameters", groupName, [ | ||
`declaration.parameters.push( | ||
...StructuresClassesMap.cloneArray( | ||
signature.parameters as ParameterDeclarationImpl[] | ||
) as ParameterDeclarationImpl[] | ||
);`, | ||
]); | ||
parts.classFieldsStatements.set("returnType", groupName, [ | ||
`if (signature.returnTypeStructure) { | ||
declaration.returnTypeStructure = TypeStructureClassesMap.clone(signature.returnTypeStructure); | ||
}`, | ||
]); | ||
} | ||
|
||
function createFromSignatureMethod( | ||
sourceType: string, | ||
parts: StructureParts, | ||
dictionaries: StructureDictionaries | ||
): string | ||
{ | ||
addImport(parts, dictionaries, true, true, sourceType); | ||
|
||
const method = new MethodDeclarationImpl("fromSignature"); | ||
method.isStatic = true; | ||
method.scope = Scope.Public; | ||
{ | ||
const param = new ParameterDeclarationImpl("signature"); | ||
param.typeStructure = new LiteralTypedStructureImpl(sourceType); | ||
method.parameters.push(param); | ||
} | ||
method.returnType = parts.classDecl.name; | ||
parts.classMembersMap.addMembers([method]); | ||
return ClassMembersMap.keyFromMember(method); | ||
} | ||
|
||
function addImport( | ||
parts: StructureParts, | ||
dictionaries: StructureDictionaries, | ||
isPublic: boolean, | ||
isTypeOnly: boolean, | ||
name: string | ||
): void | ||
{ | ||
const exportManager = isPublic ? dictionaries.publicExports : dictionaries.internalExports; | ||
parts.importsManager.addImports({ | ||
pathToImportedModule: exportManager.absolutePathToExportFile, | ||
isPackageImport: false, | ||
isDefaultImport: false, | ||
importNames: [name], | ||
isTypeOnly | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
stage_2_fullset/spec-snapshot/source/structures/declarationFromSignature.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { | ||
//MethodSignatureImpl | ||
ConstructorDeclarationImpl, | ||
ConstructSignatureDeclarationImpl, | ||
JSDocImpl, | ||
JSDocTagImpl, | ||
ParameterDeclarationImpl, | ||
TypeParameterDeclarationImpl, | ||
} from "#stage_two/snapshot/source/exports.js"; | ||
|
||
describe("static fromSignature() methods generally work", () => { | ||
const tag = new JSDocTagImpl("param"); | ||
tag.text = "Hi Mom"; | ||
const baseDoc = new JSDocImpl; | ||
baseDoc.description = "Hello World"; | ||
baseDoc.tags.push(tag); | ||
|
||
let doc: JSDocImpl; | ||
beforeEach(() => { | ||
doc = JSDocImpl.clone(baseDoc); | ||
}); | ||
|
||
function checkDoc(clonedDoc: JSDocImpl): void { | ||
expect(clonedDoc).not.toBe(doc); | ||
expect(clonedDoc.description).toBe("Hello World"); | ||
expect(clonedDoc.tags.length).toBe(1); | ||
expect(clonedDoc.tags[0]).not.toBe(tag); | ||
expect(clonedDoc.tags[0].tagName).toBe(tag.tagName); | ||
expect(clonedDoc.tags[0].text).toBe(tag.text); | ||
} | ||
|
||
it("on ConstructorDeclarationImpl", () => { | ||
const signature = new ConstructSignatureDeclarationImpl; | ||
signature.docs.push(doc); | ||
signature.leadingTrivia.push("// leading signature"); | ||
signature.trailingTrivia.push("// trailing signature"); | ||
|
||
signature.typeParameters.push(new TypeParameterDeclarationImpl("SignatureType")); | ||
{ | ||
const param = new ParameterDeclarationImpl("mySignature"); | ||
param.typeStructure = "SignatureType"; | ||
signature.parameters.push(param); | ||
} | ||
|
||
signature.returnTypeStructure = "symbol"; | ||
|
||
const decl: ConstructorDeclarationImpl = ConstructorDeclarationImpl.fromSignature(signature); | ||
checkDoc(decl.docs[0] as JSDocImpl); | ||
expect(decl.leadingTrivia).toEqual(signature.leadingTrivia); | ||
expect(decl.leadingTrivia).not.toBe(signature.leadingTrivia); | ||
expect(decl.trailingTrivia).toEqual(signature.trailingTrivia); | ||
expect(decl.trailingTrivia).not.toBe(signature.trailingTrivia); | ||
|
||
expect(decl.typeParameters.length).toBe(1); | ||
if (decl.typeParameters.length > 0) { | ||
const typeParam = decl.typeParameters[0]; | ||
expect(typeof typeParam).toBe("object"); | ||
if (typeof typeParam === "object") { | ||
expect(typeParam.name).toBe("SignatureType"); | ||
} | ||
expect(typeParam).not.toBe(signature.typeParameters[0]); | ||
} | ||
|
||
expect(decl.parameters.length).toBe(1); | ||
if (decl.parameters.length > 0) { | ||
const param = decl.parameters[0] as ParameterDeclarationImpl; | ||
expect(param.name).toBe("mySignature"); | ||
expect(param.typeStructure).toBe("SignatureType"); | ||
expect(param).not.toBe(signature.parameters[0] as ParameterDeclarationImpl); | ||
} | ||
}); | ||
}); |