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

feat: Add support for destructuring locals #452

Merged
merged 3 commits into from Sep 12, 2019

Conversation

ExE-Boss
Copy link
Collaborator

@ExE-Boss ExE-Boss commented Aug 24, 2019

Supersedes and closes #450.

Destructured locals have to be specified at compile time. All other locals need to be accessed via locals (or whatever ejs.localsName is set to).

This works for KumaScript, where the only locals are API namespaces and macro arguments $$ (all arguments array) and $0-$9, which don’t change between macros or API invocations.


review?(@mde)

@mde
Copy link
Owner

mde commented Sep 8, 2019

This is pretty great. :) Couple of issues:

  • Since this is opt-in, wouldn't it make more sense just to destructure everything in locals, as a strict-mode safe alternative to using with? Why bother asking the user to specify particular keys?

  • Could we get a test that involves multiple destructured variables, instead of just one key?

Otherwise awesome. This looks like a really useful path forward to avoid verbosity without having to resort to using with.

@ExE-Boss
Copy link
Collaborator Author

ExE-Boss commented Sep 8, 2019

This is because locals are passed at execution time, whereas the locals to destructure are specified at compile time.

Because of this, it’s impossible to know what exactly locals will have at execution time, so you have to specify variables to destructure at compile time.

And if you’re only compiling, immediately executing and discarding code, you can always just do Object.keys(locals).

@mde
Copy link
Owner

mde commented Sep 8, 2019

Oh, duh, sorry. I tend to overlook the precompile use-case. This makes good sense. If you could beef up the tests a bit, and update the README, I'll be happy to merge this, and then plug the same documentation into the EJS site.

@ExE-Boss ExE-Boss requested a review from mde September 11, 2019 19:41
Copy link
Owner

@mde mde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for this!

@mde mde merged commit e10447d into mde:master Sep 12, 2019
@ExE-Boss ExE-Boss deleted the feat/destructured-locals branch September 12, 2019 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants