Code coverage report for src/helper/structure.js

Statements: 8.7% (8 / 92)      Branches: 0% (0 / 48)      Functions: 0% (0 / 12)      Lines: 8.7% (8 / 92)      Ignored: none     

All files » src/helper/ » structure.js
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        1 1 1       1                                                                                                                                                                                                                                                                                                         1 1 1 1  
"use strict";
/**
 * velocity数据结构构造,根据vm文件,构造数据结构
 */
var utils = require('../utils');
var Helper = require('./index');
function Structure(){
  this.init.apply(this, arguments);
}
 
Structure.prototype = {
 
  init: function(asts){
    this.context = {};
    this.conditions = [];
    this.local = {};
    this.leval = 0;
    this.ignores = {};
    utils.forEach(asts, this._render, this);
  },
 
  _render: function(ast){
    var type = ast.type;
    var leval = this.leval;
    if (type === 'references'){
      this._setRefs(ast);
    } else if (type === 'if' || type === 'elseif') {
      this._setIf(ast.condition);
    } else if (type === 'set') {
      this.setValue(ast);
      leval++;
    } else if (type === 'foreach') {
      this.leval = leval++;
      this._setForEach(ast);
      leval++;
    } else if (type === 'end') {
      this.leval = leval++;
    }
 
  },
 
  _setForEach: function(ast){
    if (ast.from.type == 'references') {
      this._setRefs(ast.from, ['string']);
    }
  },
 
  _setIf: function(condition){
    var type = condition.type;
    if (type === 'references') {
      this._setRefs(condition);
    } else if (type === 'math') {
      utils.forEach(condition.expression, function(ast){
        this._setIf(ast);
      }, this);
    }
  },
 
  getReferences: Helper.getRefText,
 
  _setRefs: function(ast, spyData){
    var context = this.context;
    spyData = spyData || 'string';
    debugger;
    if (ast.path) {
      context[ast.id] = context[ast.id] || {};
      var ret = context[ast.id];
      var len = ast.path.length;
      utils.forEach(ast.path, function(property, i){
        var isEnd = len === i + 1;
        var spy = isEnd? spyData: {};
        ret = this._setAttributes(property, ret, spy);
      }, this);
    } else {
      //强制设值
      if (spyData !== 'string'){
        context[ast.id] = spyData;
      } else {
        context[ast.id] = context[ast.id] || spyData;
      }
    }
  },
 
  _setAttributes: function(property, baseRef, spy){
    /**
     * type对应着velocity.yy中的attribute,三种类型: method, index, property
     */
    var type = property.type;
    var ret;
    var id = property.id;
    if (type === 'method'){
      ret = this._setPropMethod(property, baseRef, spy);
    } else if (type === 'property') {
      baseRef[id] = spy;
      ret = baseRef[id];
    } else {
      ret = this._setPropIndex(property, baseRef, spy);
    }
    return ret;
  },
 
  /**
   * $foo.bar[1] index求值
   */
  _setPropIndex: function(property, baseRef, spy){
    var ast = property.id;
    var key;
    if (ast.type === 'references'){
      key = this.getReferences(ast);
    } else {
      key = ast.value;
    }
 
    baseRef[key] = spy;
    return baseRef[key];
  },
 
  _setPropMethod: function(property, baseRef, spy){
 
    var id         = property.id;
    var ret        = '';
    var _id        = id.slice(3);
    //特殊方法
    var specialFns = ['keySet'];
 
    if (id.indexOf('get') === 0){
      if (!_id) {
        //map 对应的get方法
        _id = this.getLiteral(property.args[0]);
      }
      baseRef[_id] = spy;
      ret = baseRef[_id];
    } else if (id.indexOf('set') === 0) {
      baseRef[_id] = this.getLiteral(property.args[0]);
      ret = baseRef;
    } else if (id === 'keySet') {
      ret = Object.keys(baseRef);
    } else {
      ret = baseRef[id];
      var args = [];
 
      utils.forEach(property.args, function(exp){
        args.push(this.getLiteral(exp));
      }, this);
 
      if (ret && ret.call) {
        ret = ret.apply(baseRef, args); 
      } else {
        //spy fn
        baseRef[id] = utils.noop(spy);
        ret = baseRef[id];
      }
    }
 
    return ret;
  }
 
};
 
require('../compile/expression')(Structure, utils);
require('../compile/literal')(Structure, utils);
require('../compile/set')(Structure, utils);
module.exports = Structure;