Debian Codegraph @

scope-analyzer/index.js

raw
1 /* eslint-disable no-redeclare */
2 var assert = require('assert')
3 var dash = require('dash-ast')
4 var Symbol = require('es6-symbol')
5 var getAssignedIdentifiers = require('get-assigned-identifiers')
6 var isFunction = require('estree-is-function')
7 var Binding = require('./binding')
8 var Scope = require('./scope')
9
10 var kScope = Symbol('scope')
11
12 exports.createScope = createScope
13 exports.visitScope = visitScope
14 exports.visitBinding = visitBinding
15 exports.crawl = crawl
16 exports.analyze = crawl // old name
17 exports.clear = clear
18 exports.deleteScope = deleteScope
19 exports.nearestScope = getNearestScope
20 exports.scope = getScope
21 exports.getBinding = getBinding
22
23 function set (object, property, value) {
24 Object.defineProperty(object, property, {
25 value: value,
26 enumerable: false,
27 configurable: true,
28 writable: true
29 })
30 }
31
32 // create a new scope at a node.
33 function createScope (node, bindings) {
34 assert.ok(typeof node === 'object' && node && typeof node.type === 'string', 'scope-analyzer: createScope: node must be an ast node')
35 if (!node[kScope]) {
36 var parent = getParentScope(node)
37 set(node, kScope, new Scope(parent))
38 }
39 if (bindings) {
40 for (var i = 0; i < bindings.length; i++) {
41 node[kScope].define(new Binding(bindings[i]))
42 }
43 }
44 return node[kScope]
45 }
46
47 // Separate scope and binding registration steps, for post-order tree walkers.
48 // Those will typically walk the scope-defining node _after_ the bindings that belong to that scope,
49 // so they need to do it in two steps in order to define scopes first.
50 function visitScope (node) {
51 assert.ok(typeof node === 'object' && node && typeof node.type === 'string', 'scope-analyzer: visitScope: node must be an ast node')
52 registerScopeBindings(node)
53 }
54 function visitBinding (node) {
55 assert.ok(typeof node === 'object' && node && typeof node.type === 'string', 'scope-analyzer: visitBinding: node must be an ast node')
56 if (isVariable(node)) {
57 registerReference(node)
58 }
59 }
60
61 function crawl (ast) {
62 assert.ok(typeof ast === 'object' && ast && typeof ast.type === 'string', 'scope-analyzer: crawl: ast must be an ast node')
63 dash(ast, function (node, parent) {
64 set(node, 'parent', parent)
65 visitScope(node)
66 })
67 dash(ast, visitBinding)
68
69 return ast
70 }
71
72 function clear (ast) {
73 assert.ok(typeof ast === 'object' && ast && typeof ast.type === 'string', 'scope-analyzer: clear: ast must be an ast node')
74 dash(ast, deleteScope)
75 }
76
77 function deleteScope (node) {
78 if (node) {
79 delete node[kScope]
80 }
81 }
82
83 function getScope (node) {
84 if (node && node[kScope]) {
85 return node[kScope]
86 }
87 return null
88 }
89
90 function getBinding (identifier) {
91 assert.strictEqual(typeof identifier, 'object', 'scope-analyzer: getBinding: identifier must be a node')
92 assert.strictEqual(identifier.type, 'Identifier', 'scope-analyzer: getBinding: identifier must be an Identifier node')
93
94 var scopeNode = getDeclaredScope(identifier)
95 if (!scopeNode) return null
96 var scope = getScope(scopeNode)
97 if (!scope) return null
98 return scope.getBinding(identifier.name) || scope.undeclaredBindings.get(identifier.name)
99 }
100
101 function registerScopeBindings (node) {
102 if (node.type === 'Program') {
103 createScope(node)
104 }
105 if (node.type === 'VariableDeclaration') {
106 var scopeNode = getNearestScope(node, node.kind !== 'var')
107 var scope = createScope(scopeNode)
108 node.declarations.forEach(function (decl) {
109 getAssignedIdentifiers(decl.id).forEach(function (id) {
110 scope.define(new Binding(id.name, id))
111 })
112 })
113 }
114 if (node.type === 'ClassDeclaration') {
115 var scopeNode = getNearestScope(node)
116 var scope = createScope(scopeNode)
117 if (node.id && node.id.type === 'Identifier') {
118 scope.define(new Binding(node.id.name, node.id))
119 }
120 }
121 if (node.type === 'FunctionDeclaration') {
122 var scopeNode = getNearestScope(node, false)
123 var scope = createScope(scopeNode)
124 if (node.id && node.id.type === 'Identifier') {
125 scope.define(new Binding(node.id.name, node.id))
126 }
127 }
128 if (isFunction(node)) {
129 var scope = createScope(node)
130 node.params.forEach(function (param) {
131 getAssignedIdentifiers(param).forEach(function (id) {
132 scope.define(new Binding(id.name, id))
133 })
134 })
135 }
136 if (node.type === 'FunctionExpression' || node.type === 'ClassExpression') {
137 var scope = createScope(node)
138 if (node.id && node.id.type === 'Identifier') {
139 scope.define(new Binding(node.id.name, node.id))
140 }
141 }
142 if (node.type === 'ImportDeclaration') {
143 var scopeNode = getNearestScope(node, false)
144 var scope = createScope(scopeNode)
145 getAssignedIdentifiers(node).forEach(function (id) {
146 scope.define(new Binding(id.name, id))
147 })
148 }
149 if (node.type === 'CatchClause') {
150 var scope = createScope(node)
151 if (node.param) {
152 getAssignedIdentifiers(node.param).forEach(function (id) {
153 scope.define(new Binding(id.name, id))
154 })
155 }
156 }
157 }
158
159 function getParentScope (node) {
160 var parent = node
161 while (parent.parent) {
162 parent = parent.parent
163 if (getScope(parent)) return getScope(parent)
164 }
165 }
166
167 // Get the scope that a declaration will be declared in
168 function getNearestScope (node, blockScope) {
169 var parent = node
170 while (parent.parent) {
171 parent = parent.parent
172 if (isFunction(parent)) {
173 break
174 }
175 if (blockScope && parent.type === 'BlockStatement') {
176 break
177 }
178 if (parent.type === 'Program') {
179 break
180 }
181 }
182 return parent
183 }
184
185 // Get the scope that this identifier has been declared in
186 function getDeclaredScope (id) {
187 var parent = id
188 // Jump over one parent if this is a function's name--the variables
189 // and parameters _inside_ the function are attached to the FunctionDeclaration
190 // so if a variable inside the function has the same name as the function,
191 // they will conflict.
192 // Here we jump out of the FunctionDeclaration so we can start by looking at the
193 // surrounding scope
194 if (id.parent.type === 'FunctionDeclaration' && id.parent.id === id) {
195 parent = id.parent
196 }
197 while (parent.parent) {
198 parent = parent.parent
199 if (parent[kScope] && parent[kScope].has(id.name)) {
200 break
201 }
202 }
203 return parent
204 }
205
206 function registerReference (node) {
207 var scopeNode = getDeclaredScope(node)
208 var scope = getScope(scopeNode)
209 if (scope && scope.has(node.name)) {
210 scope.add(node.name, node)
211 }
212 if (scope && !scope.has(node.name)) {
213 scope.addUndeclared(node.name, node)
214 }
215 }
216
217 function isObjectKey (node) {
218 return node.parent.type === 'Property' &&
219 node.parent.key === node &&
220 // a shorthand property may have the ===-same node as both the key and the value.
221 // we should detect the value part.
222 node.parent.value !== node
223 }
224 function isMethodDefinition (node) {
225 return node.parent.type === 'MethodDefinition' && node.parent.key === node
226 }
227 function isImportName (node) {
228 return node.parent.type === 'ImportSpecifier' && node.parent.imported === node
229 }
230
231 function isVariable (node) {
232 return node.type === 'Identifier' &&
233 !isObjectKey(node) &&
234 !isMethodDefinition(node) &&
235 (node.parent.type !== 'MemberExpression' || node.parent.object === node ||
236 (node.parent.property === node && node.parent.computed)) &&
237 !isImportName(node)
238 }