1 | Escope ([escope](http://github.com/estools/escope)) is
|
2 | [ECMAScript](http://www.ecma-international.org/publications/standards/Ecma-262.htm)
|
3 | scope analyzer extracted from [esmangle project](http://github.com/estools/esmangle).
|
4 |
|
5 | [![Build Status](https://travis-ci.org/estools/escope.png?branch=master)](https://travis-ci.org/estools/escope)
|
6 |
|
7 | ### Example
|
8 |
|
9 | ```js
|
10 | var escope = require('escope');
|
11 | var esprima = require('esprima');
|
12 | var estraverse = require('estraverse');
|
13 |
|
14 | var ast = esprima.parse(code);
|
15 | var scopeManager = escope.analyze(ast);
|
16 |
|
17 | var currentScope = scopeManager.acquire(ast); // global scope
|
18 |
|
19 | estraverse.traverse(ast, {
|
20 | enter: function(node, parent) {
|
21 | // do stuff
|
22 |
|
23 | if (/Function/.test(node.type)) {
|
24 | currentScope = scopeManager.acquire(node); // get current function scope
|
25 | }
|
26 | },
|
27 | leave: function(node, parent) {
|
28 | if (/Function/.test(node.type)) {
|
29 | currentScope = currentScope.upper; // set to parent scope
|
30 | }
|
31 |
|
32 | // do stuff
|
33 | }
|
34 | });
|
35 | ```
|
36 |
|
37 | ### Document
|
38 |
|
39 | Generated JSDoc is [here](http://estools.github.io/escope/).
|
40 |
|
41 | ### Demos and Tools
|
42 |
|
43 | Demonstration is [here](http://mazurov.github.io/escope-demo/) by [Sasha Mazurov](https://github.com/mazurov) (twitter: [@mazurov](http://twitter.com/mazurov)). [issue](https://github.com/estools/escope/issues/14)
|
44 |
|
45 | ![Demo](https://f.cloud.github.com/assets/75759/462920/7aa6dd40-b4f5-11e2-9f07-9f4e8d0415f9.gif)
|
46 |
|
47 |
|
48 | And there are tools constructed on Escope.
|
49 |
|
50 | - [Esmangle](https://github.com/estools/esmangle) is a minifier / mangler / optimizer.
|
51 | - [Eslevels](https://github.com/mazurov/eslevels) is a scope levels analyzer and [SublimeText plugin for scope context coloring](https://github.com/mazurov/sublime-levels) is constructed on it.
|
52 | - [Esgoggles](https://github.com/keeyipchan/esgoggles) is JavaScript code browser.
|
53 |
|
54 |
|
55 | ### License
|
56 |
|
57 | Copyright (C) 2012-2013 [Yusuke Suzuki](http://github.com/Constellation)
|
58 | (twitter: [@Constellation](http://twitter.com/Constellation)) and other contributors.
|
59 |
|
60 | Redistribution and use in source and binary forms, with or without
|
61 | modification, are permitted provided that the following conditions are met:
|
62 |
|
63 | * Redistributions of source code must retain the above copyright
|
64 | notice, this list of conditions and the following disclaimer.
|
65 |
|
66 | * Redistributions in binary form must reproduce the above copyright
|
67 | notice, this list of conditions and the following disclaimer in the
|
68 | documentation and/or other materials provided with the distribution.
|
69 |
|
70 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
71 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
72 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
73 | ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
74 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
75 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
76 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
77 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
78 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
79 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|