1

I am trying to evaluate JavaScript code based on what a user enters into a code editor. I get their code as a single string, use babel to transform it and then split on newlines to get the code by line. I want to see if the current line evaluates to something and if so, print that out. I skip lines that do not evaluate to anything but the problem is if some writes code like the following:

1 | const x = 5;
2 | x

Ideally I would like to print something like:

1 | 
2 | 5

The problem though is that I need line 1 to evaluate line two. If I just join the lines of code and evaluate that (something like: const x = 5;\nx;) it will result in 5;

However, if a line further down does not evaluate to anything, then it will also return 5 but that is not correct because const y = 3; does not evaluate to 5. So for example the following:

1 | const x = 5;
2 | x
3 | const y = 3;

concatenated (something like: const x = 5;\nx\nconst y = 3;): would result in:

1 | undefined
2 | 5
3 | 5 // PROBLEM: this should also be undefined.

I have tried this solution: Context-preserving eval but it does not answer my question. Moreover, The solution throws an error in the following line when I tried it out:

// 'void (__EVAL = ${__EVAL.toString()});' causes an error
var __EVAL = s => eval(`void (__EVAL = ${__EVAL.toString()}); ${s}`);

I want to evaluate the latest statement in a block of code, given I have the code written before that, but only return a result if the current line evaluates to something. So as one final example, the following code:

1 | const x = 5;
2 | x
3 | const y = 3;
4 |
5 | const add = (a, b) => {
6 |   return a + b;
7 | };
8 | 
9 | add(x, y);

should give:

1 | 
2 | 5
3 |
4 | 
5 | 
6 | 
7 | 
8 | 
9 | 8
  • 1
    Please [edit](https://stackoverflow.com/posts/68091261/edit) your [previous question](https://stackoverflow.com/q/68091261/1048572) instead of reposting it. – Bergi Jun 23 '21 at 08:44
  • Sorry, someone closed it and SO said to ask a new question – regexAgainstTheMachine Jun 23 '21 at 09:10
  • @regexAgainstTheMachine yes, the post notices aren't great in this regard. However, they are out of our hands. As in out of the hands of regular users. When it says "post a new one" it means "post *a different* question". The policy was always to edit closed questions in order to get them reopen but the closure notices were changed and the new wording does not at all make that clear. [We have notified the people in charge about the abiguity](https://meta.stackoverflow.com/questions/394552/you-can-edit-the-question-or-post-a-new-one) but...sadly, the wording wasn't clarified. – VLAZ Jun 23 '21 at 09:28
  • Ok... SO process aside, my question has not been answered so I am still looking for help if someone can. If my question (this one or the other I posted) requires clarification I can add any details necessary but it seems this question has recieved a lot of visits in a short time so it's likely of interest to more than just me. Can we work together to solve this? – regexAgainstTheMachine Jun 23 '21 at 10:59

0 Answers0