Skip to content

Commit

Permalink
fix: ProjenStruct cannot be chained (#135)
Browse files Browse the repository at this point in the history
Fixes #94
  • Loading branch information
mrgrain authored Jul 2, 2023
1 parent 3c81bb1 commit fc75d9b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 20 deletions.
20 changes: 10 additions & 10 deletions src/builder/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -154,39 +154,39 @@ 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);
}

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;
});

return this;
}

public add(...props: Property[]) {
public add(...props: Property[]): this {
for (const prop of props.reverse()) {
this._properties.set(prop.name, prop);
}

return this;
}

public update(name: string, update: Partial<Property>) {
public update(name: string, update: Partial<Property>): this {
const old = this._properties.get(name);

if (!old) {
Expand All @@ -209,18 +209,18 @@ export class Struct
return this.add(updatedProp);
}

public updateAll(update: Partial<Property>) {
public updateAll(update: Partial<Property>): 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 || []));
}
Expand Down
20 changes: 10 additions & 10 deletions src/projen/projen-struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Property>): IStructBuilder {
update(name: string, update: Partial<Property>): this {
this.builder.update(name, update);
return this;
}
updateAll(update: Partial<Property>): IStructBuilder {
updateAll(update: Partial<Property>): 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;
}
Expand Down
29 changes: 29 additions & 0 deletions test/projen/__snapshots__/projen-struct.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions test/projen/projen-struct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit fc75d9b

Please sign in to comment.