sam-goodwin / punchcard

Type-safe AWS infrastructure.
Apache License 2.0
507 stars 20 forks source link

Use call-site to do some hacky/awesome simplifications? #101

Open sam-goodwin opened 4 years ago

sam-goodwin commented 4 years ago

Could we use https://www.npmjs.com/package/callsite to do some tricky stuff like auto-generating CDK construct IDs from the assign variable name?

const myConstruct = new Construct(stack, 'myConstruct');
// simplify to
const myConstruct = new Construct(stack)

Could even store the current "scope" globally to remove the boilerplate entirely:

const stack = new cdk.Stack(app, 'my-stack'); // pushes scope onto a global stack
const myConstruct = new Construct(); // no boiler-plate
pocesar commented 4 years ago

IMHO, that's abusing the language features 😆 https://github.com/tj/callsite/blob/4882cd047cbc234df1d07cfe26f618ecb1a45e5e/index.js#L2-L10

sam-goodwin commented 4 years ago

Oh, absolutely! That's why it's so enticing, haha! Maybe not this particular use-case, but it might be useful for grabbing things like __dirname from the caller's context.

sam-goodwin commented 4 years ago

Oh I see, you were referring to their implementation. I think it's brilliant :P.

sam-goodwin commented 4 years ago

Might break in webpack, though.

pocesar commented 4 years ago

yeah, rewriting the global error class static method then rewriting it back, reminds me of DOJO/mootools era, long time ago 😄