0

As we all know, when we use var to declare a variable in the file level, it is accessible by all other JS files that are loaded in the same HTML page. I am now trying to transform them into ES6. Therefore, I would like to eliminate all those variable sharing.

There are a lot of people suggest to use code like Object.keys( window ); in console to find all of them. However, there are a lot more variables that weren't defined in my scripts and I would like to do this analysis statically.

NOTE: This can't be done by relying on linting tools to report "undeclared identifier". For example, below are the files for adding new members to the same global object(variable) called g. The js load order is file a and then file b. As you can see, g is a global variable. However, linting tools don't say which global variable is shared by more than one JS file.

File a

var g;
if(typeof g === 'undefined')
   g = {};
g.something = 1;

File b

var g;
if(typeof g === 'undefined')
    g = {};
g.hey = 2;
console.log(g.something); // this works

NOTE: This can't be done by traversing the window object. First, this is not done statically. Second, there are a lot of predefined objects by the browsers which are not inherited - We can't know whether they are used by any user-written JS file.

Good Good
  • 1
  • 1
  • @Justastudent Nope, it's not. Please see the edited post. – Good Good May 18 '17 at 19:47
  • I read your question as: you want to find all variables that are declared globally, thus, with `var foo` outside of function scope. This is something linting tools can do, as discussed in the linked question. Am I misinterpreting your question? I honestly don't understand your first note, could you elaborate on what the issue is? – Just a student May 18 '17 at 20:07
  • @Justastudent Edited. Please have a look. Thanks – Good Good May 18 '17 at 21:22
  • 1
    @GoodGood Linting tools won't only report undeclared identifiers, they will also report declared variables in the global scope. – Bergi May 18 '17 at 21:39
  • @bergi could you tell me which rules for which tool will give me that information? I have been trying and searching. However, I don't see the way. Thanks – Good Good May 19 '17 at 01:09
  • I think [eslint's no-implicit-globals rule](http://eslint.org/docs/rules/no-implicit-globals) should do it. That's just my first attempt though, I might find more with a bit of research. – Bergi May 19 '17 at 01:14
  • @Bergi Right! It does the job! Thank you very much! Should I delete this post? Or, should you create the answer then I accept? – Good Good May 19 '17 at 05:42
  • How about you post the full solution you used as an answer to [the other question](http://stackoverflow.com/q/29368015/1048572), then I close it as a duplicate? – Bergi May 19 '17 at 05:56
  • Wait... No... It doesn't do the job. Because what I am asking is to identify all variables that are *global* and *used by more than one js file* while with that option, `eslint` will only give me *global variables* even if they are only used in one js file. @Bergi – Good Good May 19 '17 at 19:20
  • @GoodGood I guess for that you'll need to count in how many files it is used yourself – Bergi May 20 '17 at 08:21
  • @Bergi Could you guys remove the duplicate flag? Since it's not. – Good Good May 20 '17 at 18:00
  • @Justastudent Please don't leave the duplicate flag to confuse people... It's not a duplicate. – Good Good May 24 '17 at 03:32
  • Ah, only seeing this now, you didn't ping me before. Your comment clarifies your question a lot, and makes it not a duplicate indeed. Your question as it stands still is. Could you edit it to clearly state your requirements? I'm on mobile now, will remove the flag once you edited the question and I'm on a desktop. – Just a student May 24 '17 at 05:44
  • @Justastudent Please take a look. Thanks. – Good Good May 25 '17 at 06:12

0 Answers0