UNPKG

6.58 kBJavaScriptView Raw
1/*
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>
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8
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.
14
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.
25*/
26
27/**
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/>.
32 * <p>
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
37 * possible).
38 * <p>
39 * <em>escope</em> works on a syntax tree of the parsed source code which has
40 * to adhere to the <a
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.
44 * <p>
45 * The main interface is the {@link analyze} function.
46 * @module escope
47 */
48/* eslint no-underscore-dangle: ["error", { "allow": ["__currentScope"] }] */
49
50import assert from "assert";
51
52import ScopeManager from "./scope-manager.js";
53import Referencer from "./referencer.js";
54import Reference from "./reference.js";
55import Variable from "./variable.js";
56
57import eslintScopeVersion from "./version.js";
58
59/**
60 * Set the default options
61 * @returns {Object} options
62 */
63function defaultOptions() {
64 return {
65 optimistic: false,
66 directive: false,
67 nodejsScope: false,
68 impliedStrict: false,
69 sourceType: "script", // one of ['script', 'module', 'commonjs']
70 ecmaVersion: 5,
71 childVisitorKeys: null,
72 fallback: "iteration"
73 };
74}
75
76/**
77 * Preform deep update on option object
78 * @param {Object} target Options
79 * @param {Object} override Updates
80 * @returns {Object} Updated options
81 */
82function updateDeeply(target, override) {
83
84 /**
85 * Is hash object
86 * @param {Object} value Test value
87 * @returns {boolean} Result
88 */
89 function isHashObject(value) {
90 return typeof value === "object" && value instanceof Object && !(value instanceof Array) && !(value instanceof RegExp);
91 }
92
93 for (const key in override) {
94 if (Object.prototype.hasOwnProperty.call(override, key)) {
95 const val = override[key];
96
97 if (isHashObject(val)) {
98 if (isHashObject(target[key])) {
99 updateDeeply(target[key], val);
100 } else {
101 target[key] = updateDeeply({}, val);
102 }
103 } else {
104 target[key] = val;
105 }
106 }
107 }
108 return target;
109}
110
111/**
112 * Main interface function. Takes an Espree syntax tree and returns the
113 * analyzed scopes.
114 * @function analyze
115 * @param {espree.Tree} tree Abstract Syntax Tree
116 * @param {Object} providedOptions Options that tailor the scope analysis
117 * @param {boolean} [providedOptions.optimistic=false] the optimistic flag
118 * @param {boolean} [providedOptions.directive=false] the directive flag
119 * @param {boolean} [providedOptions.ignoreEval=false] whether to check 'eval()' calls
120 * @param {boolean} [providedOptions.nodejsScope=false] whether the whole
121 * script is executed under node.js environment. When enabled, escope adds
122 * a function scope immediately following the global scope.
123 * @param {boolean} [providedOptions.impliedStrict=false] implied strict mode
124 * (if ecmaVersion >= 5).
125 * @param {string} [providedOptions.sourceType='script'] the source type of the script. one of 'script', 'module', and 'commonjs'
126 * @param {number} [providedOptions.ecmaVersion=5] which ECMAScript version is considered
127 * @param {Object} [providedOptions.childVisitorKeys=null] Additional known visitor keys. See [esrecurse](https://github.com/estools/esrecurse)'s the `childVisitorKeys` option.
128 * @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.
129 * @returns {ScopeManager} ScopeManager
130 */
131function analyze(tree, providedOptions) {
132 const options = updateDeeply(defaultOptions(), providedOptions);
133 const scopeManager = new ScopeManager(options);
134 const referencer = new Referencer(options, scopeManager);
135
136 referencer.visit(tree);
137
138 assert(scopeManager.__currentScope === null, "currentScope should be null.");
139
140 return scopeManager;
141}
142
143export {
144
145 /** @name module:escope.version */
146 eslintScopeVersion as version,
147
148 /** @name module:escope.Reference */
149 Reference,
150
151 /** @name module:escope.Variable */
152 Variable,
153
154 /** @name module:escope.ScopeManager */
155 ScopeManager,
156
157 /** @name module:escope.Referencer */
158 Referencer,
159
160 analyze
161};
162
163/** @name module:escope.Definition */
164export { Definition } from "./definition.js";
165
166/** @name module:escope.PatternVisitor */
167export { default as PatternVisitor } from "./pattern-visitor.js";
168
169/** @name module:escope.Scope */
170export { Scope } from "./scope.js";
171
172/* vim: set sw=4 ts=4 et tw=80 : */