2

I want to evaluate all the variables inside a function before converting the function to a string.

Example:

const x = "5";

function someFunc(){
   console.log(x);
}

someFunc.toString()

Output:

function someFunc(){
   console.log(x);
}

Expected output:

function someFunc(){
   console.log("5");
}

I need it to work dynamically for any number of variables and any function without knowing the function or the variables first. I prefer a solution based on metaprogramming such as reflection, but any working solution is good, including Regex.

Any ideas on how to implement that?

2
  • Possible using eval or new Function, but let's not go there... Commented Feb 19, 2022 at 22:35
  • @youdateme I would love to hear how eval or new Function can solve this problem Commented Feb 21, 2022 at 14:49

2 Answers 2

1

I don't think this is possible, at least not exactly how you describe it.

The key blocker is that there is no way to get a list of all currently-visible variables, or their values.

So at best you'd have to change the scenario a bit: start by reading a file (or other string) containing the source code you want to process. Then use a JavaScript parser (e.g. Esprima; no endorsement, just illustration) to get an AST (abstract syntax tree). Then you can do whatever transformations you like on that AST (such as: replacing constant variables with their value), and then convert the AST back to a string (which you can then write to a file, if desired).

The above should work for simple cases like the example you gave. When you look at more complicated scenarios, you'll run into obstacles and limitations though. For example:

const x = 5;

function Case1() {
  let y = x;
  if (someCondition()) y++;
  console.log(y);  // what now?
}
function Case2() {
  for (let i = 0; i < x; i++) {
    console.log(i);  // what now?
  }
}
function Case3() {
  const y = {foo: 42, valueOf() { return "null"; }};
  console.log(y);  // what now?
}
function Case4() {
  const y = Math.random() > 0.5 ? "yes" : "no";
  console.log(y);  // what now?
}

I'm sure there are plenty more situations where this idea is difficult to put into practice.

You didn't say what your ultimate goal is, and this does have the smell of an xy question. Are you looking for optimization opportunities? A debugging environment? I strongly suspect that there's an easier way to accomplish whatever your actual goal is.

(Also, this doesn't have anything to do with V8, libuv, or Node.js; I'll drop tags accordingly.)

Sign up to request clarification or add additional context in comments.

Comments

0
const x = 5;

function someFunc(){
    // do something with x
    return x;
}

someFunc().toString();

Or

const x = 5;

function someFunc(){
    // do something with x
    return x.toString();
}

someFunc();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.