All files / src/walk fclauseWalker.ts

93.51% Statements 72/77
83.33% Branches 30/36
100% Functions 12/12
93.15% Lines 68/73
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  15x 15x 15x 15x 15x 15x   275x 275x         272x     272x 271x 271x 213x   58x 58x       1x       58x 58x 58x 58x     213x 213x 213x 213x     58x 43x 43x 36x 36x   34x       34x 32x     2x   34x 32x       34x 6x 6x 2x 2x         43x 43x 43x 43x 7x 7x     36x               967x 967x 965x 965x 2x 2x     963x 963x       2x       213x 213x 933x 933x 932x 1x 1x   931x 931x 931x                   931x       15x                            
import isProblem from "../utils/isProblem";
import Problem from "../models/Problem";
import functionName from "../utils/fnName";
import namedFn from "../utils/namedFn";
import betterThrow from "../utils/betterThrow";
 
export default function fclauseWalker( clause, walkFn ) {
  var { args: argsClause, ret: retClause, fn: validateFn } = clause.opts;
 
  return {
    trailblaze: fclauseTrailblaze,
    reconstruct: fclauseReconstruct,
  };
 
  function fclauseTrailblaze( fn ) {
    return fn;
  }
 
  function fclauseReconstruct( fn, walkOpts ) {
    if ( fn ) {
      var { conform, instrument } = walkOpts;
 
      if ( conforEm && instrument ) {
        return instrumentConformed( fn, walkOpts );
      } else if ( instrument ) {
        return _instrument( fn, walkOpts );
      }
    } else {
      throw new Error( 'A function must be clauseified for instrumentation.' );
    }
  }
 
  function _instrument( fn, walkOpts ) {
    var fnName = functionName( fn );
    var instrumentedFn = getInstrumentedFn( fnName, fn );
    var namedClauseedFn = namedFn( fnName, instrumentedFn, '__instrumented' );
    return namedClauseedFn;
  }
 
  function instrumentConformed( fn, walkOpts ) {
    var fnName = functionName( fn );
    var argConformedFn = getArgConformedInstrumentedFn( fnName, fn );
    var namedArgConformedFn = namedFn( fnName, argConformedFn, '__conformed' );
 
    return namedArgConformedFn;
  }
 
  function getInstrumentedFn( fnName, fn ) {
    return function __instrumented() {
      var args = Array.prototype.slice.call( arguments );
      var instrumentedArgs = checkArgs( fn, fnName, args );
      var retVal = fn.apply( this, instrumentedArgs );
      var instrumentedRetVal = checkRet( fn, fnName, retVal );
 
      // TODO optimize
      var conformedArgs =
      argsClause ?
        walkFn( argsClause, args, { conform: true, instrument: true } ) :
        args;
      let conformedRetVal;
      if ( retClause ) {
        conformedRetVal = walkFn( retClause, retVal, { conform: true, instrument: true } );
      } else {
        conformedRetVal = retVal;
      }
 
      checkFnRelation( fnName, fn, validateFn, conformedArgs, conformedRetVal );
      return instrumentedRetVal;
    };
  }
 
  function checkFnRelation( fnName, fn, validateFn, conformedArgs, retVal ) {
    if ( validateFn ) {
      var r = validateFn.call( null, conformedArgs, retVal );
      ifE ( !r ) {
        var p = new Problem( { args: conformedArgs, ret: retVal }, clause, [],
          `Function ${fnName} failed valiation on argument-return value relation` );
        betterThrow( p );
      }
    }
  }
 
  function checkArgs( fn, fnName, args ) {
    const displayFnName = fnName || '<anonymous>';
    if ( argsClause ) {
      var instrumentedArgs = walkFn( argsClause, args, { phase: 'trailblaze' } );
      if ( isProblem( instrumentedArgs ) ) {
        var p = new Problem( args, clause, [ instrumentedArgs ], `Arguments for function ${displayFnName}() is not valid` );
        betterThrow( p );
      } else {
        return walkFn( argsClause, instrumentedArgs, { phase: 'reconstruct', conform: false, instrument: true } );
      }
    } else {
      return args;
    }
  }
 
  function checkRet( fn, fnName, retVal ) {
    const displayFnName = fnName || '<anonymous>';
 
    if ( retClause ) {
      var instrumentedRetVal = walkFn( retClause, retVal, { phase: 'trailblaze' } );
      if ( isProblem( instrumentedRetVal ) ) {
        var p = new Problem( retVal, clause, [ instrumentedRetVal ], 'Return value for function ' + displayFnName + '() is not valid' );
        betterThrow( p );
      } else {
        var r = walkFn( retClause, instrumentedRetVal, { phase: 'reconstruct', instrument: true, conform: false } );
        return r;
      }
    } else {
      return retVal;
    }
  }
 
  function getArgConformedInstrumentedFn( fnName, fn ) {
    const displayFnName = fnName || '<anonymous>';
I
    return function __instrumentConformed() {
      var args = Array.prototype.slice.call( arguments );

      var conformedArgs = walkFn( argsClause, args, { conform: true, instrument: true } );
      if ( isProblem( conformedArgs ) ) {
        var p = new Problem( args, argsClause, [ conformedArgs ], `Arguments for function ${displayFnName} is not valid` );
        betterThrow( p );
      }
 
      var retVal = fn.call( this, conformedArgs );
 
      checkRet( fn, fnName, retVal );
 
      if ( validateFn ) {
        var conformedRetVal;
        if ( retClause ) {
          conformedRetVal = walkFn( retClause, retVal, { conform: true, instrument: true } );
        } else {
          conformedRetVal = retVal;
        }
        checkFnRelation( fnName, fn, validateFn, conformedArgs, conformedRetVal );
      }
 
      return retVal;
    };
  }
 
}