Skip to content
Use acorn to detect global variables in JavaScript
JavaScript
Branch: master
Clone or download
Pull request Compare This branch is 3 commits ahead, 28 commits behind ForbesLindesay:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example
test
.gitignore
.travis.yml
LICENSE
README.md
index.js
package.json

README.md

acorn-globals

Detect global variables in JavaScript using acorn

Build Status Dependency Status NPM version

Installation

npm install acorn-globals

Usage

detect.js

var fs = require('fs');
var detect = require('acorn-globals');

var src = fs.readFileSync(__dirname + '/input.js', 'utf8');

var options = {};
var scope = detect(src, options);
console.dir(scope);

input.js

var x = 5;
var y = 3, z = 2;

w.foo();
w = 2;

RAWR=444;
RAWR.foo();

BLARG=3;

foo(function () {
    var BAR = 3;
    process.nextTick(function (ZZZZZZZZZZZZ) {
        console.log('beep boop');
        var xyz = 4;
        x += 10;
        x.zzzzzz;
        ZZZ=6;
    });
    function doom () {
    }
    ZZZ.foo();

});

console.log(xyz);

output:

$ node example/detect.js
[ { name: 'BLARG', nodes: [ [Object] ] },
  { name: 'RAWR', nodes: [ [Object], [Object] ] },
  { name: 'ZZZ', nodes: [ [Object], [Object] ] },
  { name: 'console', nodes: [ [Object], [Object] ] },
  { name: 'foo', nodes: [ [Object] ] },
  { name: 'process', nodes: [ [Object] ] },
  { name: 'w', nodes: [ [Object], [Object] ] },
  { name: 'xyz', nodes: [ [Object] ] } ]

Options

  • includeFileVars

    Default: false

    Include variables declared in the file scope of the JavaScript file. This is useful when using modules that are not automatically wrapped in a closure.

  • includeFunctionDeclarations

    Default: false

    Include function definitions in the list of globals. Use with includeFileVars.

License

MIT

You can’t perform that action at this time.