File: resolve_local_scopes.js

package info (click to toggle)
node-stdlib 0.0.96%2Bds1%2B~cs0.0.429-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 421,476 kB
  • sloc: javascript: 1,562,831; ansic: 109,702; lisp: 49,823; cpp: 27,224; python: 7,871; sh: 6,807; makefile: 6,089; fortran: 3,102; awk: 387
file content (245 lines) | stat: -rw-r--r-- 5,940 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
* @license Apache-2.0
*
* Copyright (c) 2019 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var walk = require( 'acorn-walk' ).ancestor;
var appendUnique = require( './append_unique.js' );
var isScope = require( './is_scope.js' );
var isBlockScope = require( './is_block_scope.js' );


// VARIABLES //

var VISITORS = {
	'VariableDeclaration': VariableDeclaration,
	'FunctionDeclaration': FunctionDeclaration,
	'Function': declareFunction,
	'ClassDeclaration': ClassDeclaration,
	'TryStatement': TryStatement,
	'ImportDefaultSpecifier': ModuleImportSpecifier,
	'ImportSpecifier': ModuleImportSpecifier,
	'ImportNamespaceSpecifier': ModuleImportSpecifier
};


// FUNCTIONS //

/**
* Resolves identifiers arising from a function declaration, such as function parameters and the function name.
*
* @private
* @param {Node} node - function declaration AST node
*/
function declareFunction( node ) {
	var i;

	node.locals = node.locals || [];
	for ( i = 0; i < node.params.length; i++ ) {
		declarationPattern( node.params[ i ], node );
	}
	if ( node.id ) {
		appendUnique( node.locals, node.id.name );
	}
}

/**
* Resolves identifiers based on the variable declaration pattern.
*
* @private
* @param {Node} node - AST node
* @param {Node} parent - parent AST node
* @throws {Error} unrecognized pattern type
*/
function declarationPattern( node, parent ) {
	var i;

	switch ( node.type ) { // eslint-disable-line padded-blocks

	// Actual identifier name:
	case 'Identifier':
		appendUnique( parent.locals, node.name );
		break;

	// `var { a, b } = { 'a': 10, 'b': 20 }` || `var { ...o } = {}`
	case 'ObjectPattern':
		for ( i = 0; i < node.properties.length; i++ ) {
			declarationPattern( node.properties[ i ].value || node.properties[ i ].argument, parent ); // eslint-disable-line max-len
		}
		break;

	// `var [ x, y ] = [ 10, 20 ]`
	case 'ArrayPattern':
		for ( i = 0; i < node.elements.length; i++ ) {
			if ( node.elements[ i ] ) {
				declarationPattern( node.elements[ i ], parent );
			}
		}
		break;

	// `var [ x, y, ...z ] = [ 10, 20, 30, 40, 50 ]` || `var [ ...z ] = []`
	case 'RestElement':
		declarationPattern( node.argument, parent );
		break;

	// `var [ x=5, y=7] = [ 1 ]`
	case 'AssignmentPattern':
		declarationPattern( node.left, parent );
		break;

	default:
		throw new Error( 'internal error. Unrecognized pattern type: `' + node.type + '`.' );
	}
}

/**
* Callback invoked upon encountering a `VariableDeclaration` AST node.
*
* @private
* @param {Node} node - AST node
* @param {Array} parents - array of parent AST nodes
*/
function VariableDeclaration( node, parents ) {
	var parent;
	var i;

	// Case: `var x`
	if ( node.kind === 'var' ) {
		for ( i = parents.length-1; i >= 0; i-- ) {
			if ( isScope( parents[ i ] ) ) {
				parent = parents[ i ];
				break;
			}
		}
	}
	// Case: `let x` || `const x`
	else {
		for ( i = parents.length-1; i >= 0; i-- ) {
			if ( isBlockScope( parents[ i ] ) ) {
				parent = parents[ i ];
				break;
			}
		}
	}
	parent.locals = parent.locals || [];
	for ( i = 0; i < node.declarations.length; i++ ) {
		declarationPattern( node.declarations[ i ].id, parent );
	}
}

/**
* Callback invoked upon encountering a `FunctionDeclaration` AST node.
*
* @private
* @param {Node} node - AST node
* @param {Array} parents - array of parent AST nodes
*/
function FunctionDeclaration( node, parents ) {
	var parent;
	var i;
	for ( i = parents.length-2; i >= 0; i-- ) {
		if ( isScope( parents[ i ] ) ) {
			parent = parents[ i ];
			break;
		}
	}
	parent.locals = parent.locals || [];
	if ( node.id ) {
		appendUnique( parent.locals, node.id.name );
	}
	declareFunction( node );
}

/**
* Callback invoked upon encountering a `ClassDeclaration` AST node.
*
* @private
* @param {Node} node - AST node
* @param {Array} parents - array of parent AST nodes
*/
function ClassDeclaration( node, parents ) {
	var parent;
	var i;
	for ( i = parents.length-2; i >= 0; i-- ) {
		if ( isBlockScope( parents[ i ] ) ) {
			parent = parents[ i ];
			break;
		}
	}
	parent.locals = parent.locals || [];
	if ( node.id ) {
		appendUnique( parent.locals, node.id.name );
	}
}

/**
* Callback invoked upon encountering a `TryStatement` AST node.
*
* @private
* @param {Node} node - AST node
* @param {Array} parents - array of parent AST nodes
*/
function TryStatement( node ) {
	if ( node.handler ) {
		node.handler.locals = node.handler.locals || [];
		appendUnique( node.handler.locals, node.handler.param.name );
	}
}

/**
* Callback invoked upon encountering a module `import` specifier AST node.
*
* @private
* @param {Node} node - AST node
* @param {Array} parents - array of parent AST nodes
*/
function ModuleImportSpecifier( node, parents ) {
	parents[ 0 ].locals = parents[ 0 ].locals || [];
	appendUnique( parents[ 0 ].locals, node.local.name );
}


// MAIN //

/**
* Resolves local scopes within an abstract syntax tree (AST).
*
* ## Notes
*
* -   This function modifies the provided AST by adding a `locals` property to select AST nodes.
*
* @private
* @param {AST} ast - abstract syntax tree (AST)
* @throws {TypeError} must provide a program AST node
* @returns {AST} input AST
*/
function resolveScopes( ast ) {
	if ( ast.type !== 'Program' ) {
		throw new TypeError( 'invalid argument. Must provide a program AST node.' );
	}
	ast.locals = [];
	walk( ast, VISITORS );
	return ast;
}


// EXPORTS //

module.exports = resolveScopes;