2 Copyright (C) 2012-2014 Yusuke Suzuki <utatane.tea@gmail.com>
3 Copyright (C) 2013 Alex Seville <hi@alexanderseville.com>
4 Copyright (C) 2014 Thiago de Arruda <tpadilha84@gmail.com>
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
19 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Escope (<a href="http://github.com/estools/escope">escope</a>) is an <a
29 * href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">ECMAScript</a>
30 * scope analyzer extracted from the <a
31 * href="http://github.com/estools/esmangle">esmangle project</a/>.
33 * <em>escope</em> finds lexical scopes in a source program, i.e. areas of that
34 * program where different occurrences of the same identifier refer to the same
35 * variable. With each scope the contained variables are collected, and each
36 * identifier reference in code is linked to its corresponding variable (if
39 * <em>escope</em> works on a syntax tree of the parsed source code which has
41 * href="https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API">
42 * Mozilla Parser API</a>. E.g. <a href="https://github.com/eslint/espree">espree</a> is a parser
43 * that produces such syntax trees.
45 * The main interface is the {@link analyze} function.
50 /* eslint no-underscore-dangle: ["error", { "allow": ["__currentScope"] }] */
52 const assert = require("assert");
54 const ScopeManager = require("./scope-manager");
55 const Referencer = require("./referencer");
56 const Reference = require("./reference");
57 const Variable = require("./variable");
58 const Scope = require("./scope").Scope;
59 const version = require("../package.json").version;
62 * Set the default options
63 * @returns {Object} options
65 function defaultOptions() {
71 sourceType: "script", // one of ['script', 'module']
73 childVisitorKeys: null,
79 * Preform deep update on option object
80 * @param {Object} target - Options
81 * @param {Object} override - Updates
82 * @returns {Object} Updated options
84 function updateDeeply(target, override) {
88 * @param {Object} value - Test value
89 * @returns {boolean} Result
91 function isHashObject(value) {
92 return typeof value === "object" && value instanceof Object && !(value instanceof Array) && !(value instanceof RegExp);
95 for (const key in override) {
96 if (Object.prototype.hasOwnProperty.call(override, key)) {
97 const val = override[key];
99 if (isHashObject(val)) {
100 if (isHashObject(target[key])) {
101 updateDeeply(target[key], val);
103 target[key] = updateDeeply({}, val);
114 * Main interface function. Takes an Espree syntax tree and returns the
117 * @param {espree.Tree} tree - Abstract Syntax Tree
118 * @param {Object} providedOptions - Options that tailor the scope analysis
119 * @param {boolean} [providedOptions.optimistic=false] - the optimistic flag
120 * @param {boolean} [providedOptions.directive=false]- the directive flag
121 * @param {boolean} [providedOptions.ignoreEval=false]- whether to check 'eval()' calls
122 * @param {boolean} [providedOptions.nodejsScope=false]- whether the whole
123 * script is executed under node.js environment. When enabled, escope adds
124 * a function scope immediately following the global scope.
125 * @param {boolean} [providedOptions.impliedStrict=false]- implied strict mode
126 * (if ecmaVersion >= 5).
127 * @param {string} [providedOptions.sourceType='script']- the source type of the script. one of 'script' and 'module'
128 * @param {number} [providedOptions.ecmaVersion=5]- which ECMAScript version is considered
129 * @param {Object} [providedOptions.childVisitorKeys=null] - Additional known visitor keys. See [esrecurse](https://github.com/estools/esrecurse)'s the `childVisitorKeys` option.
130 * @param {string} [providedOptions.fallback='iteration'] - A kind of the fallback in order to encounter with unknown node. See [esrecurse](https://github.com/estools/esrecurse)'s the `fallback` option.
131 * @returns {ScopeManager} ScopeManager
133 function analyze(tree, providedOptions) {
134 const options = updateDeeply(defaultOptions(), providedOptions);
135 const scopeManager = new ScopeManager(options);
136 const referencer = new Referencer(options, scopeManager);
138 referencer.visit(tree);
140 assert(scopeManager.__currentScope === null, "currentScope should be null.");
147 /** @name module:escope.version */
150 /** @name module:escope.Reference */
153 /** @name module:escope.Variable */
156 /** @name module:escope.Scope */
159 /** @name module:escope.ScopeManager */
165 /* vim: set sw=4 ts=4 et tw=80 : */