diff --git a/src/builder/struct.ts b/src/builder/struct.ts index af65a43..5fa0b2a 100644 --- a/src/builder/struct.ts +++ b/src/builder/struct.ts @@ -144,7 +144,7 @@ export class Struct ); } - public filter(predicate: (prop: Property) => boolean) { + public filter(predicate: (prop: Property) => boolean): this { for (const propertyKey of this._properties.keys()) { if (!predicate(this._properties.get(propertyKey)!)) { this._properties.delete(propertyKey); @@ -154,11 +154,11 @@ export class Struct return this; } - public only(...keep: string[]) { + public only(...keep: string[]): this { return this.filter((prop) => keep.includes(prop.name)); } - public omit(...remove: string[]) { + public omit(...remove: string[]): this { for (const prop of remove) { this._properties.delete(prop); } @@ -166,11 +166,11 @@ export class Struct return this; } - public withoutDeprecated() { + public withoutDeprecated(): this { return this.filter((prop) => null == prop.docs?.deprecated); } - public allOptional() { + public allOptional(): this { this._properties.forEach((property) => { property.optional = true; }); @@ -178,7 +178,7 @@ export class Struct return this; } - public add(...props: Property[]) { + public add(...props: Property[]): this { for (const prop of props.reverse()) { this._properties.set(prop.name, prop); } @@ -186,7 +186,7 @@ export class Struct return this; } - public update(name: string, update: Partial) { + public update(name: string, update: Partial): this { const old = this._properties.get(name); if (!old) { @@ -209,18 +209,18 @@ export class Struct return this.add(updatedProp); } - public updateAll(update: Partial) { + public updateAll(update: Partial): this { for (const propertyKey of this._properties.keys()) { this.update(propertyKey, update); } return this; } - public rename(from: string, to: string) { + public rename(from: string, to: string): this { return this.update(from, { name: to }); } - public mixin(...sources: HasProperties[]) { + public mixin(...sources: HasProperties[]): this { for (const source of sources.reverse()) { this.add(...(source.properties || [])); } diff --git a/src/projen/projen-struct.ts b/src/projen/projen-struct.ts index da2b32c..30d7e25 100644 --- a/src/projen/projen-struct.ts +++ b/src/projen/projen-struct.ts @@ -98,43 +98,43 @@ export class ProjenStruct public get spec() { return this.builder.spec; } - filter(predicate: (prop: Property) => boolean): IStructBuilder { + filter(predicate: (prop: Property) => boolean): this { this.builder.filter(predicate); return this; } - only(...keep: string[]): IStructBuilder { + only(...keep: string[]): this { this.builder.only(...keep); return this; } - omit(...remove: string[]): IStructBuilder { + omit(...remove: string[]): this { this.builder.omit(...remove); return this; } - withoutDeprecated(): IStructBuilder { + withoutDeprecated(): this { this.builder.withoutDeprecated(); return this; } - allOptional(): IStructBuilder { + allOptional(): this { this.builder.allOptional(); return this; } - add(...props: Property[]): IStructBuilder { + add(...props: Property[]): this { this.builder.add(...props); return this; } - update(name: string, update: Partial): IStructBuilder { + update(name: string, update: Partial): this { this.builder.update(name, update); return this; } - updateAll(update: Partial): IStructBuilder { + updateAll(update: Partial): this { this.builder.updateAll(update); return this; } - rename(from: string, to: string): IStructBuilder { + rename(from: string, to: string): this { this.builder.rename(from, to); return this; } - mixin(...sources: HasProperties[]): IStructBuilder { + mixin(...sources: HasProperties[]): this { this.builder.mixin(...sources); return this; } diff --git a/test/projen/__snapshots__/projen-struct.test.ts.snap b/test/projen/__snapshots__/projen-struct.test.ts.snap index 42b4d05..f9f18aa 100644 --- a/test/projen/__snapshots__/projen-struct.test.ts.snap +++ b/test/projen/__snapshots__/projen-struct.test.ts.snap @@ -160,3 +160,32 @@ export interface MyBaseInterface { } " `; + +exports[`can use the result of a chain 1`] = ` +"// ~~ Generated by projen. To modify, edit .projenrc.js and run "npx projen". + +/** + * MyInterface + */ +export interface MyInterface { + /** + * The name of the projenrc file. + * @default ".projenrc.ts" + * @stability experimental + */ + readonly filename?: string; + /** + * A directory tree that may contain *.ts files that can be referenced from your projenrc typescript file. + * @default "projenrc" + * @stability experimental + */ + readonly projenCodeDir?: string; + /** + * Whether to use \`SWC\` for ts-node. + * @default false + * @stability experimental + */ + readonly swc?: boolean; +} +" +`; diff --git a/test/projen/projen-struct.test.ts b/test/projen/projen-struct.test.ts index 4b81a1e..0706fe1 100644 --- a/test/projen/projen-struct.test.ts +++ b/test/projen/projen-struct.test.ts @@ -36,6 +36,28 @@ test('can mixin a struct', () => { expect(renderedFile).toContain('filename'); }); +test('can use the result of a chain', () => { + // ARRANGE + const project = new TestProject(); + const base = new ProjenStruct(project, { + name: 'MyInterface', + }); + const other = new ProjenStruct(project, { + name: 'MyOtherInterface', + }).mixin(Struct.fromFqn('projen.typescript.ProjenrcOptions')); + + // ACT + base.mixin(other); + + // PREPARE + const renderedFile = synthSnapshot(project)['src/MyInterface.ts']; + + // ASSERT + expect(renderedFile).toMatchSnapshot(); + expect(renderedFile).toContain('projenCodeDir'); + expect(renderedFile).toContain('filename'); +}); + test('can mixin another projen struct', () => { // ARRANGE const project = new TestProject();