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(cdk): don't use instanceof in App #1249

Merged
merged 3 commits into from
Nov 30, 2018
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
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;
eladb marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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