1

I have an interesting problem.

I've detected global variable height in my project.
Project has hundreds of files, so height variable occurs thousands of times.
Obviously, I had forgotten var operator somewhere.

How can I track this global variable origin?

UPD:
As I already said, variable occurs thousands of times.
And I don't really know how it can look like:

// forgotten var:
height = 'something';

// or forgotten comma
var a = 1,
    b = 2 
    height = 3;

So I definitely can not search for it manually.
Any suggestions?

Legotin
  • 2,378
  • 1
  • 18
  • 28
  • 1
    You could investigate using the developer tools in chrome. – evolutionxbox Apr 27 '16 at 22:21
  • @evolutionxbox I wonder, how exactly? – Legotin Apr 27 '16 at 22:22
  • Possible duplicate of [How to know in which file that defined a js global var in chrome console?](http://stackoverflow.com/questions/11644937/how-to-know-in-which-file-that-defined-a-js-global-var-in-chrome-console) – FiringSquadWitness Apr 27 '16 at 22:22
  • `grep -e 'height\s*='`? – Paul Apr 27 '16 at 22:25
  • Dear @Paulpro, thank you for suggestion. Your regexp found hundreds of matches, including `some_other_height` variables too. It's not really helpful. – Legotin Apr 27 '16 at 22:32
  • If you have other vairables that end in height, you can add a boundary to make it to work for you: `grep -e '\bheight\s*=`. That should find everywhere `height` is assigned a value (which is what creates an implicit global). The forgotten `var` and forgotten comma options you posted wouldn't create a global, they would throw a `ReferenceError: height is not defined`. – Paul Apr 27 '16 at 22:34
  • @Paulpro this regexp definitely better, but it still found matches like `style.height`. Besides, no one said that after the variable was a "=" sign. – Legotin Apr 27 '16 at 22:40
  • @legotin An equals sign is the only possible way to create a global variable accidentally. The only other way would be assigning it as a property of the global object, which would be pretty hard to do by accident. – Paul Apr 27 '16 at 22:42
  • There are also static tools, such as JSHINT, that can help. – Jeremy J Starcher Apr 27 '16 at 22:42
  • You could try `grep -e '\(^\|\s\)height\s*='`. That should also filter out the `style.height` and such. – Paul Apr 27 '16 at 22:43
  • @Paulpro you're right. I've corrected cases where a it could occur. – Legotin Apr 27 '16 at 22:44
  • @Paulpro you overdone with your last regexp, it finds nothing now :) – Legotin Apr 27 '16 at 22:48
  • We might have differently flavours of `grep`. Maybe try: `grep -e '(^|\s)height\s*=` ? – Paul Apr 27 '16 at 22:49
  • It works! I found it! @Paulpro <3 – Legotin Apr 27 '16 at 22:54
  • Glad I could help :) – Paul Apr 27 '16 at 23:00

1 Answers1

1

An equals sign is the only possible way to create a global variable accidentally. The only other way would be assigning it as a property of the global object, which wouldn't likely be done by accident. Therefore you can search through your files for assignments to height using something like this:

grep -e '(^|\s)height\s*=' -R *

From those assignments you can determine where height gets created, and what scope it needs to be declared in.

By the way, in 'use strict'; mode, JavaScript would throw an error instead of creating an implicit global, which could be more desirable.

Paul
  • 139,544
  • 27
  • 275
  • 264