Skip to content

Commit

Permalink
chore(assertions): remove unnecessary condition for if statement in `…
Browse files Browse the repository at this point in the history
…Template` (#32028)

### Reason for this change



The following is the code in the `Template` constructor in the assertions module:

```ts
    if (!templateParsingOptions?.skipCyclicalDependenciesCheck ?? true) {
      checkTemplateForCyclicDependencies(this.template);
    }
```

However, since the left operand (`!templateParsingOptions?.skipCyclicalDependenciesCheck`) is never undefined (null), the right operand (`?? true`) should not be needed. And the `templateParsingOptions` is not optional arg.

### Description of changes



```diff
-    if (!templateParsingOptions?.skipCyclicalDependenciesCheck ?? true) {
+    if (!templateParsingOptions.skipCyclicalDependenciesCheck) {
      checkTemplateForCyclicDependencies(this.template);
    }
```

### Description of how you validated changes



A unit test.

### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
go-to-k authored Nov 5, 2024
1 parent 2f9fb1e commit 9027cd2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/assertions/lib/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class Template {

private constructor(template: { [key: string]: any }, templateParsingOptions: TemplateParsingOptions = {}) {
this.template = template as TemplateType;
if (!templateParsingOptions?.skipCyclicalDependenciesCheck ?? true) {
if (!templateParsingOptions.skipCyclicalDependenciesCheck) {
checkTemplateForCyclicDependencies(this.template);
}
}
Expand Down
23 changes: 22 additions & 1 deletion packages/aws-cdk-lib/assertions/test/template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1368,7 +1368,28 @@ describe('Template', () => {
}).toThrow(/dependency cycle/);
});

test('does not throw when given a template with cyclic dependencies if check is skipped', () => {
test('throws when given a template with cyclic dependencies if skipCyclicalDependenciesCheck is false', () => {
expect(() => {
Template.fromJSON({
Resources: {
Res1: {
Type: 'Foo',
Properties: {
Thing: { Ref: 'Res2' },
},
},
Res2: {
Type: 'Foo',
DependsOn: ['Res1'],
},
},
}, {
skipCyclicalDependenciesCheck: false,
});
}).toThrow(/dependency cycle/);
});

test('does not throw when given a template with cyclic dependencies if skipCyclicalDependenciesCheck is true', () => {
expect(() => {
Template.fromJSON({
Resources: {
Expand Down

0 comments on commit 9027cd2

Please sign in to comment.