All files / repl/lib auto_close_pairs_walk.js

72.06% Statements 289/401
73.62% Branches 67/91
100% Functions 2/2
72.06% Lines 289/401

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 40211x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 66x 66x 66x 66x 67x 67x 58x 58x 67x 8x 66x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 44x 44x 44x 44x 44x 44x 44x 44x 44x 114x 114x 114x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x   114x 1x 1x 1x 114x 3x 3x 1x 1x 1x 2x 2x 2x 114x 2x 2x       2x 2x 2x 114x 6x 6x 6x 6x     114x 8x 8x 2x 2x 2x 6x 6x 8x 1x 1x 1x 5x 114x                 114x 3x 3x 1x 1x 1x 2x 3x 1x 1x 1x 1x 1x 114x 1x 1x 1x 114x 26x 26x 114x                 114x                 114x 6x 6x 1x 1x 1x 5x 6x 2x 2x 2x 3x 6x 2x 2x 2x 1x 1x 1x 114x 2x 2x 1x 1x 1x 1x 1x 1x 114x                 114x 4x 4x 4x 4x   114x                           114x 8x 8x 4x 4x 4x 4x 8x 3x 3x 3x 1x 1x 1x 1x 1x     114x 3x 3x 1x 1x 1x 2x 2x 2x 114x 15x 15x 11x 11x 11x 4x 4x 114x                           114x 2x 2x 1x 1x 1x 1x 1x 1x 114x 1x 1x 114x       114x       114x       114x               114x                           114x 4x 4x 1x 1x 1x 3x 3x 3x 114x   114x 9x 9x 4x 4x 4x 5x 5x 5x 4x 4x 4x 5x 1x 1x 1x 1x   114x       114x       114x       114x 3x 3x 3x 114x 3x 3x 3x 114x 2x 2x 1x 1x 1x 1x 1x 1x 114x       114x   114x 114x   44x 11x 11x 11x 11x 11x  
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 logger = require( 'debug' );
 
 
// VARIABLES //
 
var debug = logger( 'repl:auto_close_pairs:walk' );
 
 
// FUNCTIONS //
 
/**
* Returns the AST node associated with a provided cursor position.
*
* @private
* @param {Array<Node>} nodes - list of AST nodes
* @param {NonNegativeInteger} cursor - cursor position
* @returns {(Node|null)} associated AST node or null
*/
function findNode( nodes, cursor ) {
	var node;
	var i;
	for ( i = 0; i < nodes.length; i++ ) {
		node = nodes[ i ];
		if ( node.start <= cursor && cursor <= node.end ) {
			return node;
		}
	}
	return null;
}
 
 
// MAIN //
 
/**
* Walks an abstract syntax tree (AST) to determine whether to auto-close.
*
* @private
* @param {Node} ast - AST to walk
* @param {NonNegativeInteger} cursor - cursor position
* @returns {boolean} boolean indicating whether to auto-close
*/
function walk( ast, cursor ) { // eslint-disable-line max-lines-per-function
	var node;
	var tmp;
 
	debug( 'Searching for AST node associated with current cursor position...' );
	node = findNode( ast.body, cursor );
 
	debug( 'Walking the AST...' );
	while ( node && ( node.type !== 'Identifier' || node.start === node.end ) ) {
		debug( 'Node type: %s', node.type );
		switch ( node.type ) {
		case 'ArrayExpression':
			// `[<|>`
			if ( node.elements.length === 0 ) {
				debug( 'Detected the start of an array expression.' );
				return true;
			}
			// `['<|>` || `[ 1, '<|>` || `[ ['<|>, 1, 2, 3 ]` || etc
			node = findNode( node.elements, cursor );
			if ( node === null ) {
				// `${foo([<|>)}`
				return true;
			}
			break;
		case 'ArrowFunctionExpression':
			// `() => [<|>` || `() => { return '<|>`
			node = node.body;
			break;
		case 'AssignmentExpression':
			// `x[ = 5`
			if ( node.left.type === 'Identifier' && node.left.start === node.left.end ) {
				node = node.left;
				break;
			}
			// `x = [<|>` || `x = foo['<|>`
			node = node.right;
			break;
		case 'BinaryExpression':
			// `x[ | 5`
			if ( node.left.type === 'Identifier' && node.left.start === node.left.end ) {
				node = node.left;
				break;
			}
			// `x | foo[<|>`
			node = node.right;
			break;
		case 'BlockStatement':
			if ( node.body.length === 0 ) {
				debug( 'Detected the start of a block statement.' );
				return true;
			}
			node = findNode( node.body, cursor );
			break;
		case 'CallExpression':
			// `foo(<|>`
			if ( node.arguments.length === 0 ) {
				debug( 'Detected the start of a call expression.' );
				return true;
			}
			// `foo( bar, x['<|>`
			node = findNode( node.arguments, cursor );
			if ( node === null ) {
				// `${foo(<|>}`
				return true;
			}
			break;
		case 'ConditionalExpression':
			// `( foo ) ? '<|>`
			if ( node.end === node.consequent.end ) {
				node = node.consequent;
				break;
			}
			// `( foo ) ? bar : '<|>`
			node = node.alternate;
			break;
		case 'DoWhileStatement':
			// `do {<|>`
			if ( node.end === node.body.end ) {
				node = node.body;
				break;
			}
			// `do {} while (<|>`
			if ( node.test.start === node.test.end ) {
				debug( 'Detected the start of a do-while test.' );
				return true;
			}
			node = node.test;
			break;
		case 'EmptyStatement':
			// `(`
			debug( 'Detected the start of an empty statement.' );
			return true;
		case 'ExpressionStatement':
			node = node.expression;
			break;
		case 'ForInStatement':
			// `for ( k in obj ) {<|>`
			if ( node.body.type !== 'EmptyStatement' ) {
				node = node.body;
				break;
			}
			// We can omit the `left` case, as `for ( var i = '` will be parsed as a `ForStatement`:
			node = node.right;
			break;
		case 'ForOfStatement':
			// `for ( i of iter ) {<|>`
			if ( node.body.type !== 'EmptyStatement' ) {
				node = node.body;
				break;
			}
			// We can omit the `left` case, as `for ( var i = '` will be parsed as a `ForStatement`:
			node = node.right;
			break;
		case 'ForStatement':
			// `for ( i = 0; i < 10; i++ ) {<|>`
			if ( node.body.type !== 'EmptyStatement' ) {
				node = node.body;
				break;
			}
			// `for ( i = 0; i < 10; foo[<|>`
			if ( node.update ) {
				node = node.update;
				break;
			}
			// `for ( i = 0; foo[<|>`
			if ( node.test ) {
				node = node.test;
				break;
			}
			// By elimination, must be `init` (e.g., `for ( foo[<|>`):
			node = node.init;
			break;
		case 'FunctionDeclaration':
			// `function foo(<|>`
			if ( node.body.start === node.body.end ) {
				debug( 'Detected the start of a function declaration.' );
				return true;
			}
			// `function foo() {<|>`
			node = node.body;
			break;
		case 'FunctionExpression':
			// `foo = function(<|>`
			if ( node.body.start === node.body.end ) {
				debug( 'Detected the start of a function expression.' );
				return true;
			}
			// `foo = function() {<|>`
			node = node.body;
			break;
		case 'Identifier':
			if ( node.start === node.end ) {
				debug( 'Detected an "empty" identifier.' );
				return true;
			}
			return false;
		case 'IfStatement':
			// `if ( foo ) {<|>`
			if ( node.alternate ) {
				node = node.alternate;
				break;
			}
			// `if ( foo ) {} else {<|>`
			if ( node.consequent.type !== 'EmptyStatement' ) {
				node = node.consequent;
				break;
			}
			// By elimination, must be `test` (e.g., `if (<|>`):
			node = node.test;
			break;
		case 'Literal':
			// `'<|>` || `"<|>`
			if ( node.start === node.end-1 ) {
				debug( 'Detected the start of a string literal.' );
				return true;
			}
			// `foo('<|>)`
			if ( node.start === cursor-1 ) {
				debug( 'Detected the start of a string literal.' );
				return true;
			}
			// `/'<|>` || `/'<|>/`
			if ( node.regex ) {
				debug( 'Detected a regular expression literal.' );
				return true;
			}
			// `'foo[<|>`
			return false;
		case 'LogicalExpression':
			// `x[<|> || true`
			if ( node.left.type === 'Identifier' && node.left.start === node.left.end ) {
				node = node.left;
				break;
			}
			// `x || [<|>`
			node = node.right;
			break;
		case 'MemberExpression':
			// `x['<|>` || `x['<|>] = 5`
			if ( node.property.start === node.property.end || node.property.type === 'Literal' ) {
				debug( 'Detected the start of a member expression.' );
				return true;
			}
			node = node.property;
			break;
		case 'NewExpression':
			// `new (<|>`
			if ( node.end === node.callee.end ) {
				node = node.callee;
				break;
			}
			// `new Foo(<|>`
			if ( node.arguments.length === 0 ) {
				debug( 'Detected the start of a new expression.' );
				return true;
			}
			// `new Foo( bar, '<|>`
			node = findNode( node.arguments, cursor );
			break;
		case 'ObjectExpression':
			// `{<|>`
			if ( node.properties.length === 0 ) {
				debug( 'Detected the start of an object expression.' );
				return true;
			}
			// `{ 'a': 1, 'b': '<|>`
			node = findNode( node.properties, cursor );
			break;
		case 'Property':
			// `{'<|>}`
			return true;
		case 'ReturnStatement':
			// `return '<|>`
			node = node.argument;
			break;
		case 'SequenceExpression':
			// `x, '<|>`
			node = findNode( node.expressions, cursor );
			break;
		case 'SpreadElement':
			// `[...foo['<|>`
			node = node.argument;
			break;
		case 'SwitchCase':
			// `switch ( foo ) { case '<|>`
			if ( node.end === node.test.end ) {
				node = node.test;
				break;
			}
			node = findNode( node.consequent, cursor );
			break;
		case 'SwitchStatement':
			// `switch ( '<|>` || `switch ( foo[ '<|>`
			if ( node.end === node.discriminant.end ) {
				node = node.discriminant;
				break;
			}
			// `switch ( foo ) {<|>`
			if ( node.cases.length === 0 ) {
				debug( 'Detected the start of a switch statement.' );
				return true;
			}
			// `switch ( foo ) { case 'bar': '<|>`
			node = findNode( node.cases, cursor );
			break;
		case 'TaggedTemplateExpression':
			tmp = findNode( [ node.tag ], cursor );
			if ( tmp && !findNode( [ node.quasi ], cursor ) ) {
				node = node.tag;
				break;
			}
			debug( 'Detected the start of a tagged template expression.' );
			node = node.quasi;
			break;
		case 'TemplateElement':
			return false;
		case 'TemplateLiteral':
			// `<|>
			if ( node.start === node.end-1 ) {
				debug( 'Detected the start of a template literal.' );
				return true;
			}
			if ( node.expressions.length > 0 ) {
				tmp = findNode( node.expressions, cursor );
				if ( tmp ) {
					node = tmp;
					break;
				}
			}
			node = findNode( node.quasis, cursor-1 );
			if ( node === null ) {
				return true;
			}
			break;
		case 'ThrowStatement':
			// `throw new errs[ '<|>` || `throw new Error( '`
			node = node.argument;
			break;
		case 'TryStatement':
			// `try { y = foo( '<|>`
			node = node.handler.body;
			break;
		case 'UpdateExpression':
			// `++foo['<|>`
			node = node.argument;
			break;
		case 'VariableDeclaration':
			// `var x = '<|>` || `var x = {'<|>` || `let x = ['<|>` || `let x = {'<|>` || `const x = '<|>` || `const x = {'<|>` || `var x = 5, y = '<|>` || etc.
			node = findNode( node.declarations, cursor );
			break;
		case 'VariableDeclarator':
			// `var x = [<|>` || `var x = {<|>` || etc
			node = node.init;
			break;
		case 'WhileStatement':
			// `while ( true ) {<|>`
			if ( node.body.type !== 'EmptyStatement' ) {
				node = node.body;
				break;
			}
			// `while (<|>` || `while ( foo[<|>`
			node = node.test;
			break;
		case 'YieldExpression':
			// `yield [<|>` || `yield {<|>` || etc
			node = node.argument;
			break;
		default:
			return false;
		}
	}
	return false;
}
 
 
// EXPORTS //
 
module.exports = walk;