Code coverage report for src/compile/set.js

Statements: 100% (32 / 32)      Branches: 90% (18 / 20)      Functions: 100% (4 / 4)      Lines: 100% (31 / 31)      Ignored: none     

All files » src/compile/ » set.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 701       1         65 65 65 4   61             65 65     65 1 64 9     65 65   65 28   37     65   54       11 11 5     11 11     11   11 11 11 11 11                
module.exports = function(Velocity, utils){
  /**
   * 变量设置
   */
  utils.mixin(Velocity.prototype, {
    /**
     * 获取执行环境,对于macro中定义的变量,为局部变量,不贮存在全局中,执行后销毁
     */
    getContext: function(){
      var condition = this.condition;
      var local = this.local;
      if (condition) {
        return local[condition];
      } else {
        return this.context;
      }
    },
    /**
     * parse #set
     */
    setValue: function(ast){
      var ref = ast.equal[0];
      var context  = this.getContext();
 
      //see https://github.com/shepherdwind/velocity.js/issues/25
      if (this.condition && this.condition.indexOf('macro:') === 0) {
        context = this.context;
      } else if (this.context[ref.id] != null) {
        context = this.context;
      }
 
      var valAst = ast.equal[1];
      var val;
 
      if (valAst.type === 'math') {
        val = this.getExpression(valAst);
      } else {
        val = this.getLiteral(ast.equal[1]);
      }
 
      if (!ref.path) {
 
        context[ref.id] = val;
 
      } else {
 
        var baseRef = context[ref.id];
        if (typeof baseRef != 'object') {
          baseRef = {};
        }
 
        context[ref.id] = baseRef;
        var len = ref.path ? ref.path.length: 0;
 
        //console.log(val);
        utils.forEach(ref.path, function(exp, i){
 
          var isEnd = len === i + 1;
          var key = exp.id;
          if (exp.type === 'index')  key = key.value;
          baseRef[key] = isEnd? val: {};
          baseRef = baseRef[key];
 
        });
 
      }
    }
  });
};