Skip to content

Commit

Permalink
fix(cdk): don't use instanceof in App (#1249)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rix0rrr authored Nov 30, 2018
1 parent 72413c1 commit a45c3bd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/cdk/lib/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export class App extends Root {
private get stacks() {
const out: { [name: string]: Stack } = { };
for (const child of this.children) {
if (!(child instanceof Stack)) {
throw new Error(`The child ${child.toString()} of Program must be a Stack`);
if (!Stack.isStack(child)) {
throw new Error(`The child ${child.toString()} of App must be a Stack`);
}

out[child.id] = child as Stack;
Expand Down
30 changes: 15 additions & 15 deletions packages/@aws-cdk/cdk/lib/cloudformation/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class Stack extends Construct {
*/
public static find(node: Construct): Stack {
let curr: Construct | undefined = node;
while (curr != null && !isStack(curr)) {
while (curr != null && !Stack.isStack(curr)) {
curr = curr.parent;
}

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

/**
* Return whether the given object is a Stack.
*
* We do attribute detection since we can't reliably use 'instanceof'.
*/
public static isStack(construct: Construct): construct is Stack {
return (construct as any)._isStack;
}

private static readonly VALID_STACK_NAME_REGEX = /^[A-Za-z][A-Za-z0-9-]*$/;

/**
Expand All @@ -72,11 +81,6 @@ export class Stack extends Construct {
*/
public readonly env: Environment;

/**
* Used to determine if this construct is a stack.
*/
public readonly isStack = true;

/**
* Logical ID generation strategy
*/
Expand All @@ -92,6 +96,11 @@ export class Stack extends Construct {
*/
public readonly name: string;

/**
* Used to determine if this construct is a stack.
*/
protected readonly _isStack = true;

/**
* Creates a new stack.
*
Expand Down Expand Up @@ -433,15 +442,6 @@ export abstract class Referenceable extends StackElement {
}
}

/**
* Return whether the given object is a Stack.
*
* We do attribute detection since we can't reliably use 'instanceof'.
*/
function isStack(construct: Construct): construct is Stack {
return (construct as any).isStack;
}

/**
* Collect all StackElements from a construct
*
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/cdk/test/cloudformation/test.stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export = {
'Stack.isStack indicates that a construct is a stack'(test: Test) {
const stack = new Stack();
const c = new Construct(stack, 'Construct');
test.ok(stack.isStack);
test.ok(!(c as any).isStack);
test.ok(Stack.isStack(stack));
test.ok(!Stack.isStack(c));
test.done();
},

Expand Down

0 comments on commit a45c3bd

Please sign in to comment.