ts-morph#Scope TypeScript Examples
The following examples show how to use
ts-morph#Scope.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConfigGenerator.ts From gengen with MIT License | 6 votes |
private getProperties(controllers: Record<string, Record<string, string>>): OptionalKind<PropertyDeclarationStructure>[] {
return Object.entries(controllers).map(([controller, actions]) => {
const propertyValue = Object.entries(actions).reduce<Record<string, string>>((store, [name, path]) => {
store[name] = `'${path}'`;
return store;
}, {});
return {
name: `${controller}Service`,
scope: Scope.Public,
isStatic: true,
initializer: Writers.object(propertyValue)
};
});
}
Example #2
Source File: ModelsGenerator.ts From gengen with MIT License | 6 votes |
private getIdentities(identities: IIdentityModel[], interfaces: IInterfaceModel[]): StatementStructures[] {
return identities.map(
(z): ClassDeclarationStructure => ({
kind: StructureKind.Class,
isExported: true,
name: z.name,
ctors: [
{
parameters: [
{
name: z.property.name,
hasQuestionToken: true,
type: TypeSerializer.fromTypeName(`${z.property.type} | ${z.property.dtoType}`).toString()
}
] as OptionalKind<ParameterDeclarationStructure>[],
statements: `this.${z.property.name} = new ${z.property.type}(${z.property.name});`
}
] as OptionalKind<ConstructorDeclarationStructure>[],
properties: [{ scope: Scope.Public, name: z.property.name, type: z.property.type }, this.getGuardProperty(z.name)],
methods: [
{
scope: Scope.Public,
isStatic: true,
name: TO_DTO_METHOD,
parameters: [{ name: z.property.name, type: z.property.type }],
returnType: interfaces.find(
(i) =>
i.properties.length === 1 &&
i.properties.every((x) => x.dtoType === z.property.dtoType && x.name === z.property.name)
)?.name,
statements: `return { ${z.property.name}: ${z.property.name}.toString() };`
}
]
})
);
}
Example #3
Source File: ModelsGenerator.ts From gengen with MIT License | 6 votes |
private getObjects(objects: IObjectModel[]): ClassDeclarationStructure[] {
return objects.map((z) => ({
kind: StructureKind.Class,
isExported: true,
name: z.name,
properties: this.getObjectProperties(z),
methods: [
{
scope: Scope.Public,
isStatic: true,
name: TO_DTO_METHOD,
parameters: [{ name: 'model', type: `Partial<${z.name}>` }],
returnType: z.dtoType,
statements: (x) => {
x.writeLine('return {');
z.properties.forEach((p) =>
x.withIndentationLevel(3, () => x.writeLine(`${p.name}: ${this.getToDtoPropertyInitializer(p)},`))
);
x.writeLine('};');
}
},
{
scope: Scope.Public,
isStatic: true,
name: FROM_DTO_METHOD,
parameters: [{ name: 'dto', type: z.dtoType }],
returnType: z.name,
statements: (x) => {
x.writeLine(`const model = new ${z.name}();`);
z.properties.forEach((p) =>
x.withIndentationLevel(2, () => x.writeLine(`model.${p.name} = ${this.getFromDtoPropertyInitializer(p)};`))
);
x.writeLine('return model;');
}
}
]
}));
}
Example #4
Source File: ModelsGenerator.ts From gengen with MIT License | 6 votes |
private getObjectProperties(objectModel: IObjectModel): PropertyDeclarationStructure[] {
return [
...objectModel.properties.map(
(objectProperty): PropertyDeclarationStructure => ({
kind: StructureKind.Property,
scope: Scope.Public,
name: objectProperty.name,
type: new TypeSerializer(objectProperty).toString(),
initializer: UNDEFINED_STRING
})
),
this.getGuardProperty(objectModel.name)
];
}
Example #5
Source File: ModelsGenerator.ts From gengen with MIT License | 6 votes |
private getGuardProperty(name: string): PropertyDeclarationStructure {
return {
kind: StructureKind.Property,
scope: Scope.Private,
name: `__${lowerFirst(name)}`,
type: 'string',
hasExclamationToken: true
};
}
Example #6
Source File: AngularServicesMethodGenerator.ts From gengen with MIT License | 6 votes |
protected getMethodCodeStructures(methodModel: IMethodModel): OptionalKind<MethodDeclarationStructure> {
return {
scope: Scope.Public,
name: methodModel.name,
parameters: methodModel.parameters.map((p) => this.getParameterStatement(p)),
returnType: this.getMethodReturnType(methodModel),
statements: (writer) => {
if (methodModel.kind === MethodKind.Download) {
this.createDownloadMethod(writer, methodModel);
} else {
this.createMethod(writer, methodModel);
}
}
};
}