diff --git a/src/builder/struct.ts b/src/builder/struct.ts index 874ee25..8f85b4a 100644 --- a/src/builder/struct.ts +++ b/src/builder/struct.ts @@ -184,7 +184,7 @@ export class Struct implements IStructBuilder, HasProperties, HasFullyQualifiedN const old = this._properties.get(name); if (!old) { - throw `Unable top update property '${name}' in '${this._base.fqn}: Property does not exists, please use \`add\`.'`; + throw `Unable to update property '${name}' in '${this._base.fqn}: Property does not exists, please use \`add\`.'`; } const updatedProp = { @@ -200,6 +200,10 @@ export class Struct implements IStructBuilder, HasProperties, HasFullyQualifiedN }, }; + if (updatedProp.name !== name) { + this.omit(name); + } + return this.add(updatedProp); } diff --git a/test/projen/__snapshots__/projen-struct.test.ts.snap b/test/projen/__snapshots__/projen-struct.test.ts.snap index dec1f50..5887bc4 100644 --- a/test/projen/__snapshots__/projen-struct.test.ts.snap +++ b/test/projen/__snapshots__/projen-struct.test.ts.snap @@ -58,6 +58,18 @@ export interface MyInterface { " `; +exports[`can rename a prop 1`] = ` +"// ~~ Generated by projen. To modify, edit .projenrc.js and run "npx projen". + +/** + * MyInterface + */ +export interface MyInterface { + readonly newProp?: boolean; +} +" +`; + exports[`can update props 1`] = ` "// ~~ Generated by projen. To modify, edit .projenrc.js and run "npx projen". diff --git a/test/projen/projen-struct.test.ts b/test/projen/projen-struct.test.ts index f804353..714cfb1 100644 --- a/test/projen/projen-struct.test.ts +++ b/test/projen/projen-struct.test.ts @@ -254,6 +254,30 @@ test('can updateAll props', () => { expect(renderedFile).toMatchSnapshot(); }); +test('can rename a prop', () => { + // ARRANGE + const project = new TestProject(); + + // ACT + const struct = new ProjenStruct(project, { name: 'MyInterface' }); + struct.add( + { + name: 'oldProp', + type: { primitive: PrimitiveType.Boolean }, + optional: true, + }, + ); + struct.rename('oldProp', 'newProp'); + + // PREPARE + const renderedFile = synthSnapshot(project)['src/MyInterface.ts']; + + // ASSERT + expect(renderedFile).not.toContain('oldProp'); + expect(renderedFile).toContain('newProp?: boolean'); + expect(renderedFile).toMatchSnapshot(); +}); + test('can import type from the same package at the top level', () => { // ARRANGE const project = new TestProject();