Skip to content

Commit

Permalink
Merge branch 'v4.9.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
aui committed May 10, 2017
2 parents 8200461 + 7db9222 commit 10d78de
Show file tree
Hide file tree
Showing 18 changed files with 150 additions and 241 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## v4.9.0

1. 增强调试功能:日志输出错误行号以及上下文

## v4.8.2

1. 修复子模板没有继承父模板编译 options 问题
Expand Down
10 changes: 4 additions & 6 deletions example/web-test-speed/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,11 @@
type: 'bar'
},

title: {
text: 'Template Engine Rendering Speed Test'
},
title: false,

subtitle: {
text: config.length + ' list × ' + config.calls + ' calls'
},
// subtitle: {
// text: config.length + ' list × ' + config.calls + ' calls'
// },

xAxis: {
categories: categories,
Expand Down
20 changes: 0 additions & 20 deletions lib/compile/adapter/each.js

This file was deleted.

77 changes: 0 additions & 77 deletions lib/compile/adapter/escape.js

This file was deleted.

1 change: 1 addition & 0 deletions lib/compile/adapter/include.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var include = function include(filename, data, blocks, options) {
var compile = require('../index');
options = options.$extend({
filename: options.resolveFilename(filename, options),
bail: true,
source: null
});
return compile(options)(data, blocks);
Expand Down
12 changes: 5 additions & 7 deletions lib/compile/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ var Compiler = function () {
this.external = (_external = {}, _external[DATA] = true, _external[IMPORTS] = true, _external[OPTIONS] = true, _external);

// 按需编译到模板渲染函数的内置变量
this.internal = (_internal = {}, _internal[OUT] = '\'\'', _internal[LINE] = '[0,0,\'\']', _internal[BLOCKS] = 'arguments[1]||{}', _internal[FROM] = 'null', _internal[PRINT] = 'function(){' + OUT + '+=\'\'.concat.apply(\'\',arguments)}', _internal[INCLUDE] = 'function(src,data){' + OUT + '+=' + OPTIONS + '.include(src,data||' + DATA + ',arguments[2]||' + BLOCKS + ',' + OPTIONS + ')}', _internal[EXTEND] = 'function(from){' + FROM + '=from}', _internal[BLOCK] = 'function(name,callback){if(' + FROM + '){' + OUT + '=\'\';callback();' + BLOCKS + '[name]=' + OUT + '}else{if(typeof ' + BLOCKS + '[name]===\'string\'){' + OUT + '+=' + BLOCKS + '[name]}else{callback()}}}', _internal);
this.internal = (_internal = {}, _internal[OUT] = '\'\'', _internal[LINE] = '[0,0]', _internal[BLOCKS] = 'arguments[1]||{}', _internal[FROM] = 'null', _internal[PRINT] = 'function(){' + OUT + '+=\'\'.concat.apply(\'\',arguments)}', _internal[INCLUDE] = 'function(src,data){' + OUT + '+=' + OPTIONS + '.include(src,data||' + DATA + ',arguments[2]||' + BLOCKS + ',' + OPTIONS + ')}', _internal[EXTEND] = 'function(from){' + FROM + '=from}', _internal[BLOCK] = 'function(name,callback){if(' + FROM + '){' + OUT + '=\'\';callback();' + BLOCKS + '[name]=' + OUT + '}else{if(typeof ' + BLOCKS + '[name]===\'string\'){' + OUT + '+=' + BLOCKS + '[name]}else{callback()}}}', _internal);

// 内置函数依赖关系声明
this.dependencies = (_dependencies = {}, _dependencies[PRINT] = [OUT], _dependencies[INCLUDE] = [OUT, OPTIONS, DATA, BLOCKS], _dependencies[EXTEND] = [FROM, /*[*/INCLUDE /*]*/], _dependencies[BLOCK] = [FROM, OUT, BLOCKS], _dependencies);
Expand Down Expand Up @@ -365,7 +365,7 @@ var Compiler = function () {
scripts.forEach(function (script) {

if (script.tplToken.type === tplTokenizer.TYPE_EXPRESSION) {
stacks.push(LINE + '=[' + [script.tplToken.line, script.tplToken.start, stringify(script.source)].join(',') + ']');
stacks.push(LINE + '=[' + [script.tplToken.line, script.tplToken.start].join(',') + ']');
}

mappings.push(mapping(script.code, script.tplToken));
Expand All @@ -374,7 +374,7 @@ var Compiler = function () {

stacks.push('}catch(error){');

stacks.push('throw {' + ['name:\'RuntimeError\'', 'path:' + stringify(filename), 'message:error.message', 'line:' + LINE + '[0]+1', 'column:' + LINE + '[1]+1', 'source:' + LINE + '[2]', 'stack:error.stack'].join(',') + '}');
stacks.push('throw {' + ['name:\'RuntimeError\'', 'path:' + stringify(filename), 'message:error.message', 'line:' + LINE + '[0]+1', 'column:' + LINE + '[1]+1', 'source:' + stringify(source), 'stack:error.stack'].join(',') + '}');

stacks.push('}');
} else {
Expand Down Expand Up @@ -404,12 +404,10 @@ var Compiler = function () {
var index = 0;
var line = 0;
var start = 0;
var source2 = source;

while (index < scripts.length) {
var current = scripts[index];
if (!this.checkExpression(current.code)) {
source2 = current.source;
line = current.tplToken.line;
start = current.tplToken.start;
break;
Expand All @@ -423,8 +421,8 @@ var Compiler = function () {
message: error.message,
line: line + 1,
column: start + 1,
source: source2,
script: renderCode,
source: source,
generated: renderCode,
stack: error.stack
};
}
Expand Down
71 changes: 61 additions & 10 deletions lib/compile/error.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,68 @@
'use strict';

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

/**
* 模板错误处理类
*/
function TemplateError(error) {
var stack = error.stack;
delete error.stack;
this.name = 'TemplateError';
this.message = JSON.stringify(error, null, 4);
this.stack = stack;
}

TemplateError.prototype = Object.create(Error.prototype);
TemplateError.prototype.constructor = TemplateError;
var TemplateError = function (_Error) {
_inherits(TemplateError, _Error);

function TemplateError(error) {
_classCallCheck(this, TemplateError);

var _this = _possibleConstructorReturn(this, _Error.call(this, error));

var message = error.message;

if (TemplateError.debugTypes[error.name]) {

if (error.source) {
message = TemplateError.debug(error);
}

_this.path = error.path;
}

_this.name = 'TemplateError';
_this.message = message;
return _this;
}

TemplateError.debug = function debug(error) {
var source = error.source,
path = error.path,
line = error.line,
column = error.column;


var lines = source.split(/\n/);
var start = Math.max(line - 3, 0);
var end = Math.min(lines.length, line + 3);

// Error context
var context = lines.slice(start, end).map(function (code, index) {
var number = index + start + 1;
var left = number === line ? ' >> ' : ' ';
return '' + left + number + '| ' + code;
}).join('\n');

// Alter exception message
return (path || 'anonymous') + ':' + line + ':' + column + '\n' + (context + '\n\n') + ('' + error.message);
};

return TemplateError;
}(Error);

;

TemplateError.debugTypes = {
'RuntimeError': true,
'CompileError': true
};

module.exports = TemplateError;
2 changes: 2 additions & 0 deletions lib/compile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ var compile = function compile(source) {

var error = new TemplateError({
name: 'CompileError',
path: filename,
message: 'template not found: ' + e.message,
stack: e.stack
});
Expand Down Expand Up @@ -124,6 +125,7 @@ var compile = function compile(source) {
};

render.mappings = fn.mappings;
render.sourcesContent = fn.sourcesContent;
render.toString = function () {
return fn.toString();
};
Expand Down
Loading

0 comments on commit 10d78de

Please sign in to comment.