Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Better way to differentiate between Assignment and a returned value in JS parsing. #24

Closed
saurabhdaware opened this issue Aug 20, 2020 · 2 comments

Comments

@saurabhdaware
Copy link
Member

saurabhdaware commented Aug 20, 2020

Currently, we do super hacky things to find whether it is an assignment or a value.
https://github.com/abelljs/abell-renderer/blob/main/src/execute.js#L30-L73

If user writes {{ const a = 3 }}, we want to add variable a to sandbox and print nothing. But, if user does {{ a }}, then we want to print the value to the html.

This sounds nice and smooth for example above, but imagine this case

{{ 
  (() => {
    const a = 3;
    return a;
  })();
}}

We want this to be classified as a value rather than assignment since it outputs 3 in HTML.

There are few ways we can approach this
1. Write more specific RegExp

  • Easier to code
  • Hard to maintain

2. Tokenize everything inside JavaScript

  • Hard to code
  • Easier than RegExp method to maintain.
@saurabhdaware
Copy link
Member Author

I was thinking about just tokenizing to differentiate rather than going full parsing. If we know whether it is an assignment or value, we can directly run it in VM and get the output.

Yup the snippet mentioned in #23 won't be possible. but I think such cases are fine until we get super mature. We can always scale up when we need, scaling down will be difficult

@saurabhdaware
Copy link
Member Author

Fixed in #27

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant