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 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | 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 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 120x 250x 250x 250x 250x 250x 250x 1x 1x 1x 250x 250x 250x 250x 250x 2x 2x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 118x 118x 118x 250x 118x 118x 107x 107x 107x 107x 107x 11x 11x 11x 250x 250x 250x 11x 11x 11x 250x 250x 120x 120x 120x 11x 11x 11x 11x 11x | /** * @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 logger = require( 'debug' ); var RESERVED_KEYWORDS_EMPTY_BLOCK_STATEMENT = require( './reserved_keywords_empty_block_statement.js' ); // eslint-disable-line id-length // VARIABLES // var debug = logger( 'repl:completer:walk:find_last' ); var RE_CASE_KEYWORD = /^c$|^ca$|^cas$|^case$/; var RE_DEFAULT_KEYWORD = /^d$|^de$|^def$|^defa$|^defau$|^defaul$|^default$/; // MAIN // /** * Walks an abstract syntax tree (AST) in order to find the identifier or member expression being completed. * * ## Notes * * - Returned object fields: * * - `node`: AST node. If unable to resolve an identifier or member expression AST node, this field is the last visited node. * * - `context`: specifies a completion "context" and, thus, associated identifier restrictions. Possible values: * * - `'*'`: all available context identifiers should be completion candidates. * - `'none'`: no available context identifiers should be completion candidates. * - `'NewExpression'`: only identifiers/members which are compatible with `new` expressions should be completion candidates. * - `'SwitchCase'`: only the `case` keyword should be a completion candidate. * * - `filter`: identifier filter. * * - `keywords`: an array containing applicable keywords given an AST node context. * * @private * @param {Node} node - AST node from which to begin walking * @returns {Object} object containing walk results */ function walk( node ) { // eslint-disable-line max-lines-per-function var FLG; var out; var n; FLG = true; out = { 'node': null, 'context': '*', 'filter': '', 'keywords': [] }; // Walk the tree in order to find the last node (i.e., the identifier or member expression to be completed)... debug( 'Walking the AST to find the identifier or member expression to be completed...' ); while ( FLG && node.type !== 'Identifier' && node.type !== 'MemberExpression' ) { debug( 'Node type: %s', node.type ); switch ( node.type ) { case 'ArrayExpression': // `[ <|>` || `[ foo<|>` || `[ 1, 2, <|>` || `[ 1, 2, foo<|>` || etc if ( node.elements.length === 0 ) { FLG = false; break; } node = node.elements[ node.elements.length-1 ]; break; case 'ArrowFunctionExpression': // `() => <|>` || `() => { <|>` node = node.body; break; case 'AssignmentExpression': // `x = <|>` || `x = foo<|>` node = node.right; break; case 'BinaryExpression': // `x | <|>` || `x | foo<|>` node = node.right; break; case 'BlockStatement': if ( node.body.length ) { node = node.body[ node.body.length-1 ]; break; } out.keywords.push.apply( out.keywords, RESERVED_KEYWORDS_EMPTY_BLOCK_STATEMENT ); // eslint-disable-line max-len FLG = false; break; case 'CallExpression': // `foo( bar, <|>` if ( node.arguments.length ) { debug( 'Detected attempt to complete a call expression argument.' ); node = node.arguments[ node.arguments.length-1 ]; break; } // `foo( <|>` debug( 'Detected attempt to complete a call expression argument.' ); FLG = false; break; case 'ConditionalExpression': // `( foo ) ? <|>` if ( node.end === node.consequent.end ) { debug( 'Detected attempt to complete within a conditional expression consequent.' ); node = node.consequent; break; } // `( foo ) ? bar : <|>` debug( 'Detected attempt to complete within a conditional expression alternate.' ); node = node.alternate; break; case 'DoWhileStatement': // `do { <|>` if ( node.end === node.body.end ) { node = node.body; break; } // `do {} while ( <|>` debug( 'Detected attempt to complete within a do-while loop test condition.' ); node = node.test; break; 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 ( foo<TAB>` will be parsed as a `ForStatement`: debug( 'Detected attempt to complete within a for-in loop condition expression.' ); 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 ( foo<TAB>` will be parsed as a `ForStatement`: debug( 'Detected attempt to complete within a for-of loop condition expression.' ); node = node.right; break; case 'ForStatement': // Determine from where in the statement we are trying to TAB complete... if ( node.body.type !== 'EmptyStatement' ) { node = node.body; break; } // `for ( i = 0; i < 10; <|>` if ( node.update ) { debug( 'Detected attempt to complete within a for loop update expression.' ); node = node.update; break; } // `for ( i = 0; <|>` if ( node.test ) { debug( 'Detected attempt to complete within a for loop test expression.' ); node = node.test; break; } // By elimination, must be `init` (e.g., `for ( <|>`): debug( 'Detected attempt to complete within a for loop initialization expression.' ); node = node.init; out.keywords.push( 'let', 'var' ); break; case 'FunctionDeclaration': // `function foo( <|>` if ( node.body.start === node.body.end ) { // Apparent attempt to complete from the parameter list which does not make sense: debug( 'Detected attempt to complete a function parameter name, which is not supported.' ); out.context = 'none'; FLG = false; break; } // `function foo() { <|>` node = node.body; break; case 'FunctionExpression': // `foo = function( <|>` if ( node.body.start === node.body.end ) { // Apparent attempt to complete from the parameter list which does not make sense: debug( 'Detected attempt to complete a function parameter name, which is not supported.' ); out.context = 'none'; FLG = false; break; } // `foo = function() { <|>` node = node.body; break; case 'IfStatement': // Determine from where in the statement we are trying to TAB complete... // `if ( foo ) { <|>` if ( node.alternate ) { debug( 'Detected attempt to complete within an if statement alternate.' ); node = node.alternate; break; } // `if ( foo ) {} else { <|>` if ( node.consequent.type !== 'EmptyStatement' ) { debug( 'Detected attempt to complete within an if statement consequent.' ); node = node.consequent; break; } // By elimination, must be `test` (e.g., `if ( <|>`): debug( 'Detected attempt to complete within an if statement test condition.' ); node = node.test; break; case 'LogicalExpression': // `x || <|>` || `x || foo<|>` node = node.right; break; case 'NewExpression': // `new <|>` || `new Foo<|>` if ( node.end === node.callee.end ) { debug( 'Detected attempt to complete a new expression callee name.' ); out.context = 'NewExpression'; node = node.callee; break; } // `new Foo( bar, <|>` if ( node.arguments.length ) { debug( 'Detected attempt to complete a new expression argument.' ); node = node.arguments[ node.arguments.length-1 ]; break; } // `new Foo( <|>` debug( 'Detected attempt to complete a new expression argument.' ); out.context = '*'; FLG = false; break; case 'ObjectExpression': // `{<|>` if ( node.properties.length === 0 ) { FLG = false; break; } // `{ 'a': 1, ...<|>` || `{ 'a': 1, ...foo<|>` node = node.properties[ node.properties.length-1 ]; break; case 'ReturnStatement': // `return foo<|>` if ( node.argument ) { node = node.argument; break; } // `return <|>` FLG = false; break; case 'SequenceExpression': // `x, foo<|>` node = node.expressions[ node.expressions.length-1 ]; break; case 'SpreadElement': // `[...<|>` || `[...foo<|>` node = node.argument; break; case 'SwitchCase': if ( node.test === null ) { // `switch ( foo ) { ca<|>` || `switch ( foo ) { def<|>` if ( node.consequent.length ) { node = node.consequent[ node.consequent.length-1 ].expression; // eslint-disable-line max-len out.context = 'SwitchCase'; out.filter = node.name; FLG = false; if ( RE_CASE_KEYWORD.test( out.filter ) ) { debug( 'Detected attempt to complete the `case` keyword.' ); out.keywords.push( 'case' ); } else if ( RE_DEFAULT_KEYWORD.test( out.filter ) ) { debug( 'Detected attempt to complete the `default` keyword.' ); out.keywords.push( 'default' ); } break; } // // Apparent attempt to trigger completion of a context variable (e.g., `switch( foo ) { default: <|>` out.keywords.push( 'break' ); FLG = false; break; } // `switch ( foo ) { case <|>` if ( node.end === node.test.end ) { debug( 'Detected attempted to complete within a switch case test.' ); node = node.test; break; } if ( node.consequent.length ) { n = node.consequent; node = n[ n.length-1 ]; // `switch ( foo ) { case bar: break; ca<|>` if ( n.length > 1 && n[ n.length-2 ].type === 'BreakStatement' && n[ n.length-1 ].type === 'ExpressionStatement' && n[ n.length-1 ].expression.type === 'Identifier' ) { out.keywords.push( 'case', 'default' ); } // `switch ( foo ) { case bar: break; <|>` else if ( node.type === 'BreakStatement' ) { out.keywords.push( 'case', 'default' ); } break; } // Apparent attempt to trigger completion of a context variable (e.g., `switch ( foo ) { case bar: <|>`): out.keywords.push( 'break' ); FLG = false; break; case 'SwitchStatement': // `switch ( <|>` || `switch ( foo<|>` if ( node.end === node.discriminant.end ) { debug( 'Detected attempt to complete within a switch statement discriminant.' ); node = node.discriminant; break; } // `switch ( foo ) { <|>` if ( node.cases.length === 0 ) { debug( 'Detected attempt to complete the `case` keyword.' ); out.context = 'SwitchCase'; out.filter = ''; out.keywords.push( 'case', 'default' ); FLG = false; break; } // `switch ( foo ) { case 'bar': <|>` debug( 'Detected attempt to complete within a switch statement case.' ); node = node.cases[ node.cases.length-1 ]; break; case 'TemplateLiteral': // ``<|> if ( node.expressions.length === 0 ) { FLG = false; break; } node = node.expressions[ node.expressions.length-1 ]; break; case 'ThrowStatement': // `throw <|>` || `throw foo<|>` node = node.argument; break; case 'TryStatement': // `try { <|>` node = node.handler.body; break; case 'UpdateExpression': // `++<|>` || `++foo<|>` node = node.argument; break; case 'VariableDeclaration': // `var x = <|>` || `var x = foo<|>` || `let x = <|>` || `let x = foo<|>` || `const x = <|>` || `const x = foo<|>` || `var x = 5, y = <|>` || etc. node = node.declarations[ node.declarations.length-1 ]; break; case 'VariableDeclarator': // `var foo<|>` if ( node.id.end === node.end ) { debug( 'Detected attempt to complete a newly created variable identifier, which is not supported.' ); out.context = 'none'; FLG = false; break; } // `var x = <|>` || `var x = foo<|>` node = node.init; break; case 'WhileStatement': // `while ( true ) { <|>` if ( node.body.type !== 'EmptyStatement' ) { node = node.body; break; } // `while ( <|>` debug( 'Detected attempt to complete within a while loop test condition.' ); node = node.test; break; case 'YieldExpression': // `yield foo<|>` if ( node.argument ) { node = node.argument; break; } // `yield <|>` FLG = false; break; default: out.context = 'none'; FLG = false; break; } } out.node = node; return out; } // EXPORTS // module.exports = walk; |