Skip to content

Commit

Permalink
code style
Browse files Browse the repository at this point in the history
  • Loading branch information
errorrik committed Jan 23, 2019
1 parent 56c83a5 commit 9aec050
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 86 deletions.
2 changes: 2 additions & 0 deletions src/runtime/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ Data.prototype.get = function (expr, callee) {
* @inner
* @param {Object|Array} source 要变更的源数据
* @param {Array} exprPaths 属性路径
* @param {number} pathsStart 当前处理的属性路径指针位置
* @param {number} pathsLen 属性路径长度
* @param {*} value 变更属性值
* @param {Data} data 对应的Data对象
* @return {*} 变更后的新数据
Expand Down
175 changes: 89 additions & 86 deletions src/view/compile-js-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,86 @@ function genSSRId() {
return '_id' + (ssrIndex++);
}

var stringifier = {
obj: function (source) {
var prefixComma;
var result = '{';

for (var key in source) {
if (!source.hasOwnProperty(key) || typeof source[key] === 'undefined') {
continue;
}

if (prefixComma) {
result += ',';
}
prefixComma = 1;

result += compileExprSource.stringLiteralize(key) + ':' + stringifier.any(source[key]);
}

return result + '}';
},

arr: function (source) {
var prefixComma;
var result = '[';

each(source, function (value) {
if (prefixComma) {
result += ',';
}
prefixComma = 1;

result += stringifier.any(value);
});

return result + ']';
},

str: function (source) {
return compileExprSource.stringLiteralize(source);
},

date: function (source) {
return 'new Date(' + source.getTime() + ')';
},

any: function (source) {
switch (typeof source) {
case 'string':
return stringifier.str(source);

case 'number':
return '' + source;

case 'boolean':
return source ? 'true' : 'false';

case 'object':
if (!source) {
return null;
}

if (source instanceof Array) {
return stringifier.arr(source);
}

if (source instanceof Date) {
return stringifier.date(source);
}

return stringifier.obj(source);
}

throw new Error('Cannot Stringify:' + source);
}
};

var COMPONENT_RESERVED_MEMBERS = splitStr2Obj('aNode,computed,filters,components,'
+ 'initData,template,attached,created,detached,disposed,compiled'
);

/**
* 生成序列化时起始桩的html
*
Expand Down Expand Up @@ -59,13 +139,13 @@ function serializeStumpEnd(type) {
var elementSourceCompiler = {

/* eslint-disable max-params */

/**
* 编译元素标签头
*
* @param {CompileSourceBuffer} sourceBuffer 编译源码的中间buffer
* @param {string} tagName 标签名
* @param {Array} props 属性列表
* @param {Object=} bindDirective bind指令对象
* @param {ANode} aNode 抽象节点
* @param {string=} tagNameVariable 组件标签为外部动态传入时的标签变量名
*/
tagStart: function (sourceBuffer, aNode, tagNameVariable) {
var props = aNode.props;
Expand Down Expand Up @@ -256,7 +336,8 @@ var elementSourceCompiler = {
* 编译元素闭合
*
* @param {CompileSourceBuffer} sourceBuffer 编译源码的中间buffer
* @param {string} tagName 标签名
* @param {ANode} aNode 抽象节点
* @param {string=} tagNameVariable 组件标签为外部动态传入时的标签变量名
*/
tagEnd: function (sourceBuffer, aNode, tagNameVariable) {
var tagName = aNode.tagName;
Expand Down Expand Up @@ -668,11 +749,13 @@ var aNodeCompiler = {
};

/**
* 生成组件 renderer 时 ctx 对象构建的代码
* 生成组件构建的代码
*
* @inner
* @param {CompileSourceBuffer} sourceBuffer 编译源码的中间buffer
* @param {Object} component 组件实例
* @param {Function} ComponentClass 组件类
* @param {string} contextId 构建render环境的id
* @return {string} 组件在当前环境下的方法标识
*/
function compileComponentSource(sourceBuffer, ComponentClass, contextId) {
ComponentClass.ssrContext = ComponentClass.ssrContext || {};
Expand Down Expand Up @@ -744,86 +827,6 @@ function compileComponentSource(sourceBuffer, ComponentClass, contextId) {
return componentIdInContext;
}

var stringifier = {
obj: function (source) {
var prefixComma;
var result = '{';

for (var key in source) {
if (!source.hasOwnProperty(key) || typeof source[key] === 'undefined') {
continue;
}

if (prefixComma) {
result += ',';
}
prefixComma = 1;

result += compileExprSource.stringLiteralize(key) + ':' + stringifier.any(source[key]);
}

return result + '}';
},

arr: function (source) {
var prefixComma;
var result = '[';

each(source, function (value) {
if (prefixComma) {
result += ',';
}
prefixComma = 1;

result += stringifier.any(value);
});

return result + ']';
},

str: function (source) {
return compileExprSource.stringLiteralize(source);
},

date: function (source) {
return 'new Date(' + source.getTime() + ')';
},

any: function (source) {
switch (typeof source) {
case 'string':
return stringifier.str(source);

case 'number':
return '' + source;

case 'boolean':
return source ? 'true' : 'false';

case 'object':
if (!source) {
return null;
}

if (source instanceof Array) {
return stringifier.arr(source);
}

if (source instanceof Date) {
return stringifier.date(source);
}

return stringifier.obj(source);
}

throw new Error('Cannot Stringify:' + source);
}
};

var COMPONENT_RESERVED_MEMBERS = splitStr2Obj('aNode,computed,filters,components,'
+ 'initData,template,attached,created,detached,disposed,compiled'
);

/**
* 生成组件 renderer 时 ctx 对象构建的代码
*
Expand Down

0 comments on commit 9aec050

Please sign in to comment.