Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/@cdklabs/typewriter/src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MemberType } from './member-type';
import { PropertySpec } from './property';
import { Property, PropertySpec } from './property';
import { IScope } from './scope';
import { Type } from './type';
import { DeclarationKind, TypeSpec } from './type-declaration';
Expand Down Expand Up @@ -44,4 +44,13 @@ export class InterfaceType extends MemberType {
public update<A extends Omit<Partial<InterfaceSpec>, 'properties' | 'methods' | 'name'>>(updates: A) {
Object.assign(this.spec, updates);
}

/**
* Adds a property to the interface
*
* Interface properties must be public.
*/
public addProperty(spec: Omit<PropertySpec, 'protected' | 'visibility'>): Property {
return super.addProperty(spec);
}
}
4 changes: 4 additions & 0 deletions packages/@cdklabs/typewriter/src/member-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export abstract class MemberType extends TypeDeclaration {
* Adds a property to the interface
*/
public addProperty(spec: PropertySpec): Property {
if (spec.protected && spec.visibility) {
throw new Error('Cannot specify both "protected" and "visibility"');
}

const prop = new Property(this, {
...spec,
});
Expand Down
14 changes: 14 additions & 0 deletions packages/@cdklabs/typewriter/src/property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,20 @@ export interface PropertySpec {
/**
* Indicates if this property is protected (otherwise it is public)
*
* @deprecated Use visibility instead.
* @default false
*/
protected?: boolean;

/**
* Indicates the visibility of this property
*
* Cannot be used together with `protected`.
*
* @default false
*/
visibility?: MemberVisibility;

/**
* Indicates if this property is abstract
*
Expand Down Expand Up @@ -106,6 +117,9 @@ export class Property extends TypeMember implements IProperty {
* The visibility of the member.
*/
public get visibility(): MemberVisibility {
if (this.spec?.visibility !== undefined) {
return this.spec.visibility;
}
if (this.spec?.protected) {
return MemberVisibility.Protected;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/@cdklabs/typewriter/src/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ export class StructType extends MemberType {

/**
* Adds a property to the interface
*
* Struct members are always public and immutable.
*/
public addProperty(spec: Omit<PropertySpec, 'immutable'>): Property {
public addProperty(spec: Omit<PropertySpec, 'immutable' | 'protected' | 'visibility'>): Property {
return super.addProperty({
...spec,
immutable: true,
Expand Down
21 changes: 20 additions & 1 deletion packages/@cdklabs/typewriter/test/class.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Block, ClassType, MemberVisibility, Module, TypeScriptRenderer } from '../src';
import { Block, ClassType, expr, MemberVisibility, Module, Type, TypeScriptRenderer } from '../src';

const renderer = new TypeScriptRenderer();
let scope: Module;
Expand Down Expand Up @@ -26,6 +26,25 @@ test('class with a private constructor', () => {
`);
});

test('class with a private property', () => {
const c = new ClassType(scope, {
name: 'MyClass',
});
c.addProperty({
name: 'myProperty',
type: Type.STRING,
visibility: MemberVisibility.Private,
initializer: expr.lit('Hello World'),
});

expect(renderer.render(scope)).toMatchInlineSnapshot(`
"/* eslint-disable prettier/prettier, @stylistic/max-len */
class MyClass {
private myProperty: string = "Hello World";
}"
`);
});

test('can update some class spec fields after initial creation', () => {
const c = new ClassType(scope, {
name: 'MyClass',
Expand Down