Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: renaming property does not remove old property #157

Merged
merged 1 commit into from
Oct 5, 2023
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
6 changes: 5 additions & 1 deletion src/builder/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -200,6 +200,10 @@ export class Struct implements IStructBuilder, HasProperties, HasFullyQualifiedN
},
};

if (updatedProp.name !== name) {
this.omit(name);
}

return this.add(updatedProp);
}

Expand Down
12 changes: 12 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.

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