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 | 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 11x 11x 11x 11x 869x 869x 869x 869x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 869x 869x 869x 869x 869x 869x 869x 869x 869x 869x 869x 869x 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 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 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 parse = require( 'acorn' ).parse; var walk = require( 'acorn-walk' ); var noop = require( '@stdlib/utils/noop' ); var objectKeys = require( '@stdlib/utils/keys' ); var isSilentCommand = require( './is_silent_command.js' ); // VARIABLES // var VISITORS = createVisitors({ 'ClassDeclaration': ClassDeclaration, 'ForOfStatement': ForOfStatement, 'FunctionDeclaration': FunctionDeclaration, 'FunctionExpression': noop, 'ArrowFunctionExpression': noop, 'MethodDefinition': noop, 'AwaitExpression': AwaitExpression, 'ReturnStatement': ReturnStatement, 'VariableDeclaration': VariableDeclaration }); // FUNCTIONS // /** * Returns an object containing visitor callbacks for recursively walking an abstract syntax tree (AST). * * @private * @param {Object} visitors - visitor callbacks (overrides) * @returns {Object} object containing visitor callbacks */ function createVisitors( visitors ) { var clbk; var type; var keys; var out; var i; keys = objectKeys( walk.base ); out = {}; for ( i = 0; i < keys.length; i++ ) { type = keys[ i ]; clbk = visitors[ type ] || walk.base[ type ]; out[ type ] = wrapVisitor( clbk ); } return out; /** * Returns a visitor callback which tracks ancestor nodes. * * @private * @param {Function} clbk - callback function * @returns {Function} visitor callback */ function wrapVisitor( clbk ) { return visitor; /** * Callback invoked upon encountering an AST node. * * @private * @param {Node} node - AST node * @param {Object} state - state * @param {Function} c - callback */ function visitor( node, state, c ) { var FLG = ( node !== state.ancestors[ state.ancestors.length-1 ] ); if ( FLG ) { state.ancestors.push( node ); } clbk( node, state, c ); // eslint-disable-line node/callback-return if ( FLG ) { state.ancestors.pop(); } } } } /** * Callback invoked upon encountering a `ClassDeclaration` AST node. * * @private * @param {Node} node - AST node * @param {Object} state - state * @param {Function} clbk - callback */ function ClassDeclaration( node, state, clbk ) { if ( state.ancestors[ state.ancestors.length-2 ] === state.body ) { state.prepend( node, 'global.'+node.id.name+'=' ); } walk.base.ClassDeclaration( node, state, clbk ); // eslint-disable-line new-cap } /** * Callback invoked upon encountering a `ForOfStatement` AST node. * * @private * @param {Node} node - AST node * @param {Object} state - state * @param {Function} clbk - callback */ function ForOfStatement( node, state, clbk ) { if ( node.await === true ) { state.hasAwait = true; } walk.base.ForOfStatement( node, state, clbk ); // eslint-disable-line new-cap } /** * Callback invoked upon encountering a `FunctionDeclaration` AST node. * * @private * @param {Node} node - AST node * @param {Object} state - state * @param {Function} clbk - callback */ function FunctionDeclaration( node, state ) { state.prepend( node, 'global.'+node.id.name+'=' ); } /** * Callback invoked upon encountering an `AwaitExpression` AST node. * * @private * @param {Node} node - AST node * @param {Object} state - state * @param {Function} clbk - callback */ function AwaitExpression( node, state, clbk ) { state.hasAwait = true; walk.base.AwaitExpression( node, state, clbk ); // eslint-disable-line new-cap } /** * Callback invoked upon encountering a `ReturnStatement` AST node. * * @private * @param {Node} node - AST node * @param {Object} state - state * @param {Function} clbk - callback */ function ReturnStatement( node, state, clbk ) { state.hasReturn = true; walk.base.ReturnStatement( node, state, clbk ); // eslint-disable-line new-cap } /** * Callback invoked upon encountering a `VariableDeclaration` AST node. * * ## Notes * * - **WARNING**: top-level `const` declarations are converted to `var` declarations. Unfortunately, as the declarations occur within an `async` function scope, we cannot simultaneously declare the constant in the global scope and bind the resolved value, as the former happens synchronously and the latter asynchronously. * * @private * @param {Node} node - AST node * @param {Object} state - state * @param {Function} clbk - callback */ function VariableDeclaration( node, state, clbk ) { var len; var v; var i; if ( node.kind === 'var' || state.ancestors[ state.ancestors.length-2 ] === state.body ) { len = node.declarations.length; if ( len === 1 ) { state.replace( node.start, node.start+node.kind.length, 'void' ); } else { state.replace( node.start, node.start+node.kind.length, 'void (' ); } for ( i = 0; i < len; i++ ) { v = node.declarations[ i ]; state.prepend( v, '(global.' ); state.append( v, ( v.init ) ? ')' : '=void 0)' ); } if ( len !== 1 ) { state.append( node.declarations[ len-1 ], ')' ); } } walk.base.VariableDeclaration( node, state, clbk ); // eslint-disable-line new-cap } /** * Wraps an `async`/`await` command string in order to handle the result of the returned promise. * * @private * @param {string} cmd - command to wrap * @returns {string} wrapped command */ function wrapCommand( cmd ) { return [ '"async";', '(function __iife__() {', 'function __onResult__(result) {__done__(null, result);};', 'function __onError__(error) {__done__(error);};', 'var __p__ = '+cmd+';', '__p__.then(__onResult__, __onError__);', '})()' ].join( '' ); } // MAIN // /** * Transforms a command containing a top-level `await` statement. * * @private * @param {string} cmd - command string * @returns {(string|Error)} transformed command or an error */ function processAwait( cmd ) { var state; var body; var node; var tmp; var ast; // Wrap the command string in an async IIFE: tmp = '(async function __wrapper__() {' + cmd + '})()'; // Attempt to parse the wrapped command string into an abstract syntax tree (AST): try { ast = parse( tmp, { 'ecmaVersion': 'latest' // async/await is available starting in ECMAScript Version 10 and acorn supports through version 11 (2020) }); } catch ( err ) { return err; } body = ast.body[ 0 ].expression.callee.body; // Split the wrapped command string into individual characters to allow transformation: tmp = tmp.split( '' ); // Create a `state` object for tracking state as we walk the AST: state = {}; state.body = body; state.ancestors = []; state.replace = replace; state.prepend = prepend; state.append = append; state.hasAwait = false; state.hasReturn = false; // Perform a recursive walk of the AST: walk.recursive( body, state, VISITORS ); // Do not transform if either the command does not actually contain an `await` expression OR if there exists a top-level `return` which is not allowed: if ( state.hasAwait === false || state.hasReturn ) { return new Error( 'invalid argument. Provided command either does not contain an `await` expression or contains a top-level `return` which is not allowed.' ); } // For an expression statement of the form `( expr );`, we do not want the left parenthesis before the `return` keyword, and, thus, we need to prepend a `return (` to the last AST node. Furthermore, we do not want the right parenthesis after the semicolon; in which case, as there can only be more right parentheses between `node.expression.end` and the semicolon, we can append a right parenthesis to `node.expression`. node = body.body[ body.body.length-1 ]; if ( node.type === 'ExpressionStatement' ) { state.prepend( node, 'return (' ); state.append( node.expression, ')' ); } else if ( node.type === 'VariableDeclaration' ) { node = node.declarations[ node.declarations.length-1 ]; state.append( node, '; return '+node.id.name+';' ); // return the assigned value of last declared variable } else if ( node.type === 'FunctionDeclaration' ) { state.append( node, '; return '+node.id.name+';' ); } // Introduce logic for handling the result of the returned promise: tmp = wrapCommand( tmp.join( '' ) ); if ( isSilentCommand( cmd ) ) { tmp += ';'; } // Return the transformed command: return tmp; /** * Replaces a substring within the wrapped command. * * @private * @param {NonNegativeInteger} start - start index * @param {NonNegativeInteger} stop - end index * @param {string} str - replacement string */ function replace( start, stop, str ) { var i; for ( i = start; i < stop; i++ ) { tmp[ i ] = ''; } if ( start === stop ) { str += tmp[ start ]; } tmp[ start ] = str; } /** * Prepends a string to the wrapped command. * * @private * @param {Node} node - AST node * @param {string} str - string to prepend */ function prepend( node, str ) { tmp[ node.start ] = str + tmp[ node.start ]; } /** * Appends a string to the wrapped command. * * @private * @param {Node} node - AST node * @param {string} str - string to append */ function append( node, str ) { tmp[ node.end-1] += str; } } // EXPORTS // module.exports = processAwait; |