/* Copyright (C) 2012-2014 Yusuke Suzuki Copyright (C) 2013 Alex Seville Copyright (C) 2014 Thiago de Arruda Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /** * Escope (escope) is an ECMAScript * scope analyzer extracted from the esmangle project. *

* escope finds lexical scopes in a source program, i.e. areas of that * program where different occurrences of the same identifier refer to the same * variable. With each scope the contained variables are collected, and each * identifier reference in code is linked to its corresponding variable (if * possible). *

* escope works on a syntax tree of the parsed source code which has * to adhere to the * Mozilla Parser API. E.g. esprima is a parser * that produces such syntax trees. *

* The main interface is the {@link analyze} function. * @module escope */ /*jslint bitwise:true */ import assert from 'assert'; import ScopeManager from './scope-manager'; import Referencer from './referencer'; import Reference from './reference'; import Variable from './variable'; import Scope from './scope'; import { version } from '../package.json'; function defaultOptions() { return { optimistic: false, directive: false, nodejsScope: false, sourceType: 'script', // one of ['script', 'module'] ecmaVersion: 5 }; } function updateDeeply(target, override) { var key, val; function isHashObject(target) { return typeof target === 'object' && target instanceof Object && !(target instanceof RegExp); } for (key in override) { if (override.hasOwnProperty(key)) { val = override[key]; if (isHashObject(val)) { if (isHashObject(target[key])) { updateDeeply(target[key], val); } else { target[key] = updateDeeply({}, val); } } else { target[key] = val; } } } return target; } /** * Main interface function. Takes an Esprima syntax tree and returns the * analyzed scopes. * @function analyze * @param {esprima.Tree} tree * @param {Object} providedOptions - Options that tailor the scope analysis * @param {boolean} [providedOptions.optimistic=false] - the optimistic flag * @param {boolean} [providedOptions.directive=false]- the directive flag * @param {boolean} [providedOptions.ignoreEval=false]- whether to check 'eval()' calls * @param {boolean} [providedOptions.nodejsScope=false]- whether the whole * script is executed under node.js environment. When enabled, escope adds * a function scope immediately following the global scope. * @param {string} [providedOptions.sourceType='script']- the source type of the script. one of 'script' and 'module' * @param {number} [providedOptions.ecmaVersion=5]- which ECMAScript version is considered * @return {ScopeManager} */ export function analyze(tree, providedOptions) { var scopeManager, referencer, options; options = updateDeeply(defaultOptions(), providedOptions); scopeManager = new ScopeManager(options); referencer = new Referencer(scopeManager); referencer.visit(tree); assert(scopeManager.__currentScope === null, 'currentScope should be null.'); return scopeManager; } export { /** @name module:escope.version */ version, /** @name module:escope.Reference */ Reference, /** @name module:escope.Variable */ Variable, /** @name module:escope.Scope */ Scope, /** @name module:escope.ScopeManager */ ScopeManager }; /* vim: set sw=4 ts=4 et tw=80 : */