mirror of
https://github.com/harttle/liquidjs.git
synced 2026-10-03 09:05:14 -07:00
refactor: move file to token property
This commit is contained in:
@@ -33,8 +33,8 @@ var _engine = {
|
|||||||
|
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
parse: function(html) {
|
parse: function(html, filepath) {
|
||||||
var tokens = tokenizer.parse(html);
|
var tokens = tokenizer.parse(html, filepath);
|
||||||
return this.parser.parse(tokens);
|
return this.parser.parse(tokens);
|
||||||
},
|
},
|
||||||
render: function(tpl, ctx, opts) {
|
render: function(tpl, ctx, opts) {
|
||||||
@@ -56,11 +56,7 @@ var _engine = {
|
|||||||
renderFile: function(filepath, ctx, opts) {
|
renderFile: function(filepath, ctx, opts) {
|
||||||
opts = _.assign({}, opts);
|
opts = _.assign({}, opts);
|
||||||
return this.getTemplate(filepath, opts.root)
|
return this.getTemplate(filepath, opts.root)
|
||||||
.then(templates => this.render(templates, ctx, opts))
|
.then(templates => this.render(templates, ctx, opts));
|
||||||
.catch(e => {
|
|
||||||
e.file = filepath;
|
|
||||||
throw e;
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
evalOutput: function(str, scope) {
|
evalOutput: function(str, scope) {
|
||||||
var tpl = this.parser.parseOutput(str.trim());
|
var tpl = this.parser.parseOutput(str.trim());
|
||||||
@@ -100,7 +96,7 @@ var _engine = {
|
|||||||
.then(str => this.parse(str))
|
.then(str => this.parse(str))
|
||||||
.then(tpl => this.cache[filepath] = tpl);
|
.then(tpl => this.cache[filepath] = tpl);
|
||||||
} else {
|
} else {
|
||||||
return readFileAsync(filepath).then(str => this.parse(str));
|
return readFileAsync(filepath).then(str => this.parse(str, filepath));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
+3
-2
@@ -3,7 +3,7 @@ const TokenizationError = require('./util/error.js').TokenizationError;
|
|||||||
const _ = require('./util/underscore.js');
|
const _ = require('./util/underscore.js');
|
||||||
const assert = require('../src/util/assert.js');
|
const assert = require('../src/util/assert.js');
|
||||||
|
|
||||||
function parse(html) {
|
function parse(html, filepath) {
|
||||||
assert(_.isString(html), 'illegal input type');
|
assert(_.isString(html), 'illegal input type');
|
||||||
|
|
||||||
var tokens = [];
|
var tokens = [];
|
||||||
@@ -58,7 +58,8 @@ function parse(html) {
|
|||||||
raw: match[offset],
|
raw: match[offset],
|
||||||
value: match[offset + 1].trim(),
|
value: match[offset + 1].trim(),
|
||||||
line: getLineNum(match),
|
line: getLineNum(match),
|
||||||
input: html
|
input: html,
|
||||||
|
file: filepath
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+17
-3
@@ -8,9 +8,10 @@ function TokenizationError(message, token) {
|
|||||||
|
|
||||||
this.input = token.input;
|
this.input = token.input;
|
||||||
this.line = token.line;
|
this.line = token.line;
|
||||||
|
this.file = token.file;
|
||||||
|
|
||||||
var context = mkContext(token.input, token.line);
|
var context = mkContext(token.input, token.line);
|
||||||
this.message = message + ', line:' + token.line;
|
this.message = mkMessage(message, token);
|
||||||
this.stack = context + '\n' + (this.stack || '');
|
this.stack = context + '\n' + (this.stack || '');
|
||||||
}
|
}
|
||||||
TokenizationError.prototype = Object.create(Error.prototype);
|
TokenizationError.prototype = Object.create(Error.prototype);
|
||||||
@@ -22,9 +23,10 @@ function ParseError(e, token) {
|
|||||||
|
|
||||||
this.input = token.input;
|
this.input = token.input;
|
||||||
this.line = token.line;
|
this.line = token.line;
|
||||||
|
this.file = token.file;
|
||||||
|
|
||||||
var context = mkContext(token.input, token.line);
|
var context = mkContext(token.input, token.line);
|
||||||
this.message = e.message + ', line:' + token.line;
|
this.message = mkMessage(e.message, token);
|
||||||
this.stack = context + '\n' + (this.stack || '');
|
this.stack = context + '\n' + (this.stack || '');
|
||||||
}
|
}
|
||||||
ParseError.prototype = Object.create(Error.prototype);
|
ParseError.prototype = Object.create(Error.prototype);
|
||||||
@@ -40,9 +42,10 @@ function RenderError(e, tpl) {
|
|||||||
|
|
||||||
this.input = tpl.token.input;
|
this.input = tpl.token.input;
|
||||||
this.line = tpl.token.line;
|
this.line = tpl.token.line;
|
||||||
|
this.file = tpl.token.file;
|
||||||
|
|
||||||
var context = mkContext(tpl.token.input, tpl.token.line);
|
var context = mkContext(tpl.token.input, tpl.token.line);
|
||||||
this.message = e.message + ', line:' + tpl.token.line;
|
this.message = mkMessage(e.message, tpl.token);
|
||||||
this.stack = context + '\n' + (e.stack || '');
|
this.stack = context + '\n' + (e.stack || '');
|
||||||
}
|
}
|
||||||
RenderError.prototype = Object.create(Error.prototype);
|
RenderError.prototype = Object.create(Error.prototype);
|
||||||
@@ -93,6 +96,17 @@ function align(n, max) {
|
|||||||
return blank + str;
|
return blank + str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function mkMessage(msg, token){
|
||||||
|
msg = msg || '';
|
||||||
|
if(token.file){
|
||||||
|
msg += ', file:' + token.file;
|
||||||
|
}
|
||||||
|
if(token.line){
|
||||||
|
msg += ', line:' + token.line;
|
||||||
|
}
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
TokenizationError,
|
TokenizationError,
|
||||||
ParseError,
|
ParseError,
|
||||||
|
|||||||
+2
-4
@@ -155,7 +155,7 @@ describe('error', function() {
|
|||||||
expect(err.name).to.equal('RenderError');
|
expect(err.name).to.equal('RenderError');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it.only('should contain original template context in err.stack', function() {
|
it('should contain original template context in err.stack', function() {
|
||||||
var origin = ['1st', '2nd', '3rd', 'X{%throwingTag%} Y', '5th', '6th', '7th'];
|
var origin = ['1st', '2nd', '3rd', 'X{%throwingTag%} Y', '5th', '6th', '7th'];
|
||||||
mock({
|
mock({
|
||||||
'/throwing-tag.html': origin.join('\n')
|
'/throwing-tag.html': origin.join('\n')
|
||||||
@@ -173,9 +173,7 @@ describe('error', function() {
|
|||||||
return expect(engine.parseAndRender(html)).to.eventually
|
return expect(engine.parseAndRender(html)).to.eventually
|
||||||
.be.rejected
|
.be.rejected
|
||||||
.then(function(err) {
|
.then(function(err) {
|
||||||
console.log(err.message);
|
expect(err.message).to.equal('intended render error, file:/throwing-tag.html, line:4');
|
||||||
console.log(err.stack);
|
|
||||||
expect(err.message).to.equal('intended render error, line:4');
|
|
||||||
expect(err.stack).to.contain(message.join('\n'));
|
expect(err.stack).to.contain(message.join('\n'));
|
||||||
expect(err.name).to.equal('RenderError');
|
expect(err.name).to.equal('RenderError');
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user