Skip to content

Commit

Permalink
refactor: L2 constructs should not use TypeScript unions (awslint:pro…
Browse files Browse the repository at this point in the history
…ps-no-unions) (#2430)

Adds a new awslint:props-no-unions rule which valides that props for L2 constructs do not use TypeScript unions. This is in accordance with the new AWS Construct Library guidelines.
  • Loading branch information
shivlaks committed May 8, 2019
1 parent e4ac767 commit 29f3240
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions tools/awslint/lib/rules/construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class ConstructReflection {
}

if (!found.isInterfaceType()) {
throw new Error(`Expecrting props struct ${this.propsFqn} to be an interface`);
throw new Error(`Expecting props struct ${this.propsFqn} to be an interface`);
}

return found;
Expand Down Expand Up @@ -193,4 +193,20 @@ constructLinter.add({
const baseFqn = `${e.ctx.classType.fqn}Base`;
e.assert(!e.ctx.sys.tryFindFqn(baseFqn), baseFqn);
}
});
});

constructLinter.add({
code: 'props-no-unions',
message: 'props should not use TypeScript unions',
eval: e => {
if (!e.ctx.propsType) { return; }
if (!e.ctx.hasPropsArgument) { return; }

// this rule only applies to L2 constructs
if (e.ctx.classType.name.startsWith('Cfn')) { return; }

for (const property of e.ctx.propsType.ownProperties) {
e.assert(!property.type.unionOfTypes, `${e.ctx.propsFqn}.${property.name}`);
}
}
});

0 comments on commit 29f3240

Please sign in to comment.