Skip to content

Commit 10d78de

Browse files
committed
Merge branch 'v4.9.0'
2 parents 8200461 + 7db9222 commit 10d78de

18 files changed

Lines changed: 150 additions & 241 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## v4.9.0
4+
5+
1. 增强调试功能:日志输出错误行号以及上下文
6+
37
## v4.8.2
48

59
1. 修复子模板没有继承父模板编译 options 问题

example/web-test-speed/index.html

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,11 @@
207207
type: 'bar'
208208
},
209209

210-
title: {
211-
text: 'Template Engine Rendering Speed Test'
212-
},
210+
title: false,
213211

214-
subtitle: {
215-
text: config.length + ' list × ' + config.calls + ' calls'
216-
},
212+
// subtitle: {
213+
// text: config.length + ' list × ' + config.calls + ' calls'
214+
// },
217215

218216
xAxis: {
219217
categories: categories,

lib/compile/adapter/each.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

lib/compile/adapter/escape.js

Lines changed: 0 additions & 77 deletions
This file was deleted.

lib/compile/adapter/include.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var include = function include(filename, data, blocks, options) {
1212
var compile = require('../index');
1313
options = options.$extend({
1414
filename: options.resolveFilename(filename, options),
15+
bail: true,
1516
source: null
1617
});
1718
return compile(options)(data, blocks);

lib/compile/compiler.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ var Compiler = function () {
8383
this.external = (_external = {}, _external[DATA] = true, _external[IMPORTS] = true, _external[OPTIONS] = true, _external);
8484

8585
// 按需编译到模板渲染函数的内置变量
86-
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);
86+
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);
8787

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

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

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

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

377-
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(',') + '}');
377+
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(',') + '}');
378378

379379
stacks.push('}');
380380
} else {
@@ -404,12 +404,10 @@ var Compiler = function () {
404404
var index = 0;
405405
var line = 0;
406406
var start = 0;
407-
var source2 = source;
408407

409408
while (index < scripts.length) {
410409
var current = scripts[index];
411410
if (!this.checkExpression(current.code)) {
412-
source2 = current.source;
413411
line = current.tplToken.line;
414412
start = current.tplToken.start;
415413
break;
@@ -423,8 +421,8 @@ var Compiler = function () {
423421
message: error.message,
424422
line: line + 1,
425423
column: start + 1,
426-
source: source2,
427-
script: renderCode,
424+
source: source,
425+
generated: renderCode,
428426
stack: error.stack
429427
};
430428
}

lib/compile/error.js

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,68 @@
11
'use strict';
22

3+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
4+
5+
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; }
6+
7+
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; }
8+
39
/**
410
* 模板错误处理类
511
*/
6-
function TemplateError(error) {
7-
var stack = error.stack;
8-
delete error.stack;
9-
this.name = 'TemplateError';
10-
this.message = JSON.stringify(error, null, 4);
11-
this.stack = stack;
12-
}
13-
14-
TemplateError.prototype = Object.create(Error.prototype);
15-
TemplateError.prototype.constructor = TemplateError;
12+
var TemplateError = function (_Error) {
13+
_inherits(TemplateError, _Error);
14+
15+
function TemplateError(error) {
16+
_classCallCheck(this, TemplateError);
17+
18+
var _this = _possibleConstructorReturn(this, _Error.call(this, error));
19+
20+
var message = error.message;
21+
22+
if (TemplateError.debugTypes[error.name]) {
23+
24+
if (error.source) {
25+
message = TemplateError.debug(error);
26+
}
27+
28+
_this.path = error.path;
29+
}
30+
31+
_this.name = 'TemplateError';
32+
_this.message = message;
33+
return _this;
34+
}
35+
36+
TemplateError.debug = function debug(error) {
37+
var source = error.source,
38+
path = error.path,
39+
line = error.line,
40+
column = error.column;
41+
42+
43+
var lines = source.split(/\n/);
44+
var start = Math.max(line - 3, 0);
45+
var end = Math.min(lines.length, line + 3);
46+
47+
// Error context
48+
var context = lines.slice(start, end).map(function (code, index) {
49+
var number = index + start + 1;
50+
var left = number === line ? ' >> ' : ' ';
51+
return '' + left + number + '| ' + code;
52+
}).join('\n');
53+
54+
// Alter exception message
55+
return (path || 'anonymous') + ':' + line + ':' + column + '\n' + (context + '\n\n') + ('' + error.message);
56+
};
57+
58+
return TemplateError;
59+
}(Error);
60+
61+
;
62+
63+
TemplateError.debugTypes = {
64+
'RuntimeError': true,
65+
'CompileError': true
66+
};
1667

1768
module.exports = TemplateError;

lib/compile/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ var compile = function compile(source) {
7474

7575
var error = new TemplateError({
7676
name: 'CompileError',
77+
path: filename,
7778
message: 'template not found: ' + e.message,
7879
stack: e.stack
7980
});
@@ -124,6 +125,7 @@ var compile = function compile(source) {
124125
};
125126

126127
render.mappings = fn.mappings;
128+
render.sourcesContent = fn.sourcesContent;
127129
render.toString = function () {
128130
return fn.toString();
129131
};

0 commit comments

Comments
 (0)