Skip to content

Commit a45c3bd

Browse files
authored
fix(cdk): don't use instanceof in App (#1249)
Make the Stack instance test no longer use instanceof, which doesn't work properly in case of multiple installed copies of the library. This does not resolve but helps with #1245.
1 parent 72413c1 commit a45c3bd

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

packages/@aws-cdk/cdk/lib/app.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export class App extends Root {
2121
private get stacks() {
2222
const out: { [name: string]: Stack } = { };
2323
for (const child of this.children) {
24-
if (!(child instanceof Stack)) {
25-
throw new Error(`The child ${child.toString()} of Program must be a Stack`);
24+
if (!Stack.isStack(child)) {
25+
throw new Error(`The child ${child.toString()} of App must be a Stack`);
2626
}
2727

2828
out[child.id] = child as Stack;

packages/@aws-cdk/cdk/lib/cloudformation/stack.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class Stack extends Construct {
3535
*/
3636
public static find(node: Construct): Stack {
3737
let curr: Construct | undefined = node;
38-
while (curr != null && !isStack(curr)) {
38+
while (curr != null && !Stack.isStack(curr)) {
3939
curr = curr.parent;
4040
}
4141

@@ -58,6 +58,15 @@ export class Stack extends Construct {
5858
construct.addMetadata('aws:cdk:physical-name', physicalName);
5959
}
6060

61+
/**
62+
* Return whether the given object is a Stack.
63+
*
64+
* We do attribute detection since we can't reliably use 'instanceof'.
65+
*/
66+
public static isStack(construct: Construct): construct is Stack {
67+
return (construct as any)._isStack;
68+
}
69+
6170
private static readonly VALID_STACK_NAME_REGEX = /^[A-Za-z][A-Za-z0-9-]*$/;
6271

6372
/**
@@ -72,11 +81,6 @@ export class Stack extends Construct {
7281
*/
7382
public readonly env: Environment;
7483

75-
/**
76-
* Used to determine if this construct is a stack.
77-
*/
78-
public readonly isStack = true;
79-
8084
/**
8185
* Logical ID generation strategy
8286
*/
@@ -92,6 +96,11 @@ export class Stack extends Construct {
9296
*/
9397
public readonly name: string;
9498

99+
/**
100+
* Used to determine if this construct is a stack.
101+
*/
102+
protected readonly _isStack = true;
103+
95104
/**
96105
* Creates a new stack.
97106
*
@@ -433,15 +442,6 @@ export abstract class Referenceable extends StackElement {
433442
}
434443
}
435444

436-
/**
437-
* Return whether the given object is a Stack.
438-
*
439-
* We do attribute detection since we can't reliably use 'instanceof'.
440-
*/
441-
function isStack(construct: Construct): construct is Stack {
442-
return (construct as any).isStack;
443-
}
444-
445445
/**
446446
* Collect all StackElements from a construct
447447
*

packages/@aws-cdk/cdk/test/cloudformation/test.stack.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ export = {
4545
'Stack.isStack indicates that a construct is a stack'(test: Test) {
4646
const stack = new Stack();
4747
const c = new Construct(stack, 'Construct');
48-
test.ok(stack.isStack);
49-
test.ok(!(c as any).isStack);
48+
test.ok(Stack.isStack(stack));
49+
test.ok(!Stack.isStack(c));
5050
test.done();
5151
},
5252

0 commit comments

Comments
 (0)