Skip to content
This repository was archived by the owner on Feb 21, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,27 @@ module.exports = function(opts) {
});

function conditionalHandler(inst) {
// jshint ignore: start
var condition = new Function('var context = this; with (context) { return ' + inst.args + '; }').call(data);
// jshint ignore: end
try {
// jshint ignore: start
var condition = new Function('var context = this; with (context) { return ' + inst.args + '; }').call(data);
// jshint ignore: end
} catch (error) {
throw new Error(error.message + ': ' + inst.args);
}

return condition ? inst.body : '';
}

function forHandler(inst) {
var condition = 'var context = this; with (context) { var result=""; for' + inst.args + ' { result+=`' + inst.body + '`; } return result; }';
var forLoop = 'for' + inst.args + ' { result+=`' + inst.body + '`; }';
var condition = 'var context = this; with (context) { var result=""; ' + forLoop + ' return result; }';
try {
// jshint ignore: start
var result = new Function(condition).call(data);
// jshint ignore: end
} catch (error) {
throw new Error(error.message + ': ' + forLoop);
}

return result;
}
Expand Down
46 changes: 46 additions & 0 deletions test/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const fileIncludePlugin = require('..');
const gutil = require('gulp-util');
const should = require('should');
const fs = require('fs');

describe('## error', () => {

it('# if statement', done => {
var file = new gutil.File({
path: 'test/fixtures-error/if.html',
contents: fs.readFileSync('test/fixtures-error/if.html')
});

var stream = fileIncludePlugin({
prefix: '@@',
basepath: '@root'
});
stream.on('error', error => {
error.message.should.equal('invalid is not defined: (invalid === true) ');
done();
});
stream.write(file);
stream.end();
});

it('# for statement', done => {
var file = new gutil.File({
path: 'test/fixtures-error/if.html',
contents: fs.readFileSync('test/fixtures-error/for.html')
});

var stream = fileIncludePlugin({
prefix: '@@',
basepath: '@root'
});
stream.on('error', error => {
error.message.should.equal('invalid is not defined: for (var i = 0; i < invalid.length; i++) { result+=`\n <label>`+invalid[i]+`</label>\n `; }');
done();
});
stream.write(file);
stream.end();
});

});
3 changes: 3 additions & 0 deletions test/fixtures-error/for.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@@for (var i = 0; i < invalid.length; i++) {
<label>`+invalid[i]+`</label>
}
3 changes: 3 additions & 0 deletions test/fixtures-error/if.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@@if (invalid === true) {
<h1>a</h1>
}