From 7863ad329f42c67e847ab0b4c7b5a6e26a9e6940 Mon Sep 17 00:00:00 2001 From: shivlaks <32604953+shivlaks@users.noreply.github.com> Date: Sun, 12 May 2019 23:55:45 -0700 Subject: [PATCH] refactor: L2 constructs should not use TypeScript unions (awslint:props-no-unions) (#2430) (#2509) 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. --- tools/awslint/lib/rules/construct.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tools/awslint/lib/rules/construct.ts b/tools/awslint/lib/rules/construct.ts index fbb624d3c2a97..d1b50276dde9c 100644 --- a/tools/awslint/lib/rules/construct.ts +++ b/tools/awslint/lib/rules/construct.ts @@ -80,7 +80,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; @@ -196,3 +196,19 @@ constructLinter.add({ 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}`); + } + } +}); \ No newline at end of file