1

I know this sound likes an already answered question but I looked at a link and tried the solution to no avail. Object.getOwnPropertyNames(this) returns [] and Object.getOwnPropertyNames(global) does show a lot of variables but any variables I declared in the program that is running the console interface isn't a part of this global variable. I made a repl to my console interface and to explain what I mean, running it then entering values will return an array, but I would like some list THAT HAS THESE VARIABLES

If you don't want to/can't run the repl, the code is below, save it as something and node filename to see what I mean.

The problem, again, is that I want some list that includes all the variables declared in the program(global doesn't have what I want and the variables are very accessible because when you run the program and enter values it appears but if you can find some list that has values I would be grateful)

var readline=require('readline');
const values = ['lorem ipsum', 'dolor sit amet']; //I want some list that has these variables
String.prototype.l=String.prototype.toLowerCase //shorter version of toLowerCase is now l
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: true,
  historySize: 50,
  tabSize: 2,
  completer: function(line) {var hits=[]; var hitlist=[]
    const completions = Object.getOwnPropertyNames(global)
    function search1(c,l){return c.startsWith(l)}
    function search2(c,l){return c.includes(l)&&c.startsWith(l)}
    //ordering suggestions
    hitlist.push( completions.filter(c=>search1(c,line)).sort() )
    hitlist.push( completions.filter(c=>search1(c.l(),line.l())).sort() )
    hitlist.push( completions.filter(c=>search2(c,line)).sort() )
    hitlist.push( completions.filter(c=>search2(c.l(),line.l())).sort() )
    hitlist.push( rl.history.filter(c=>search1(c,line)).sort() )
    hitlist.push( rl.history.filter(c=>search1(c.l(),line.l())).sort() )
    hitlist.push( rl.history.filter(c=>search2(c,line)).sort() )
    hitlist.push( rl.history.filter(c=>search2(c.l(),line.l())).sort() )
    //placing suggestions in one list
    hitlist.forEach(a=>a.forEach(b=>{ if(hits.indexOf(b)==-1){hits.push(b)} }))
    return [hits.length?hits:[],line]
  }
});
rl.on('line',(line)=>{try{console.log(eval(line))} catch(e){console.error(e)}})

I'm not depending on the answer being from readline im just showing WHY I want to get the in-file declared variables(for tab suggestions)

The Bomb Squad
  • 4,192
  • 1
  • 9
  • 17
  • a lot of people say this is impossible but why is it? when the console runs it proves that the variables are accessible, so where are they? where are they stored? – The Bomb Squad Dec 01 '20 at 21:46
  • You cannot access them from the JS code - also why would you want to do that? But you can easily inspect the module scope with a debugger by placing a breakpoint in the module. – Bergi Jan 15 '23 at 11:09

1 Answers1

1

You can use a Babel plugin.

This is a Babel plugin that logs information about the variables and identifiers in the scope of a program.

const { template } = require("@babel/core");
const generator = require("@babel/generator").default;

module.exports = function({ types: t }) {
  return {
     visitor: {
      Program(path) {
        t.addComment(path.container,"leading", `This is what I found:
Identifiers defined in this scope: ${JSON.stringify(Object.keys(path.scope.bindings))};
Identifiers used that are global or in prelude scope: ${JSON.stringify(Object.keys(path.scope.globals))}
All variables used at all: ${JSON.stringify(Object.keys(path.scope.references))}
`)
      }
    }
  };
};

So, Babel simply parses the file and transforms the code, and creates a comment that explains whatever it has found.

Here is an online demo. Hope, it helps.

Sunil Kumar Das
  • 372
  • 1
  • 2
  • 12
Rainb
  • 1,965
  • 11
  • 32