strict_filters/strict_variables when creating engine

This commit is contained in:
harttle
2016-11-07 00:01:02 +08:00
parent 1e7873afc0
commit ddd06e12b5
8 changed files with 203 additions and 90 deletions
+1 -1
View File
@@ -21,7 +21,7 @@ module.exports = function() {
if (typeof filter !== 'function'){
return {
name: name,
error: new Error(`undefined filter: ${name}`)
error: new TypeError(`undefined filter: ${name}`)
};
}
+3 -10
View File
@@ -4,9 +4,9 @@ const assert = require('./util/assert.js');
const referenceError = /undefined variable|Cannot read property .* of undefined/;
var Scope = {
getAll: function(str) {
getAll: function() {
var ctx = {};
for (i = this.scopes.length - 1; i >= 0; i--) {
for (var i = this.scopes.length - 1; i >= 0; i--) {
_.assign(ctx, this.scopes[i]);
}
return ctx;
@@ -26,13 +26,6 @@ var Scope = {
throw new TypeError('undefined variable: ' + str);
}
},
safeGet: function(str) {
try {
var val = this.safeGet(str);
} catch (e) {;
}
return val;
},
set: function(k, v) {
this.setPropertyByPath(this.scopes[this.scopes.length - 1], k, v);
return this;
@@ -112,7 +105,7 @@ var Scope = {
}
// foo["bar"]
else {
var j = str.indexOf(delemiter, i + 2);
j = str.indexOf(delemiter, i + 2);
assert(j !== -1, `unbalanced ${delemiter}: ${str}`);
name = str.slice(i + 2, j);
seq.push(name);
+51 -3
View File
@@ -25,27 +25,75 @@ function forOwn(object, iteratee) {
return object;
}
function assign(dst, src) {
dst = dst || {};
/*
* Assigns own enumerable string keyed properties of source objects to the destination object.
* Source objects are applied from left to right.
* Subsequent sources overwrite property assignments of previous sources.
*
* Note: This method mutates object and is loosely based on Object.assign.
*
* @param {Object} object The destination object.
* @param {...Object} sources The source objects.
* @return {Object} Returns object.
*/
function assign(object) {
object = isObject(object) ? object : {};
var srcs = Array.prototype.slice.call(arguments, 1);
srcs.forEach(function(src) {
_assignBinary(object, src);
});
return object;
}
function _assignBinary(dst, src) {
if (!dst) return dst;
forOwn(src, function(v, k) {
dst[k] = v;
});
return dst;
}
function isArray(value) {
return value instanceof Array;
}
function echo(prefix){
function echo(prefix) {
return v => {
console.log('[' + prefix + ']', v);
return v;
};
}
function uniq(arr) {
var u = {},
a = [];
for (var i = 0, l = arr.length; i < l; ++i) {
if (u.hasOwnProperty(arr[i])) {
continue;
}
a.push(arr[i]);
u[arr[i]] = 1;
}
return a;
}
/*
* Checks if value is the language type of Object.
* (e.g. arrays, functions, objects, regexes, new Number(0), and new String(''))
* @param {any} value The value to check.
* @return {Boolean} Returns true if value is an object, else false.
*/
function isObject(value) {
return value !== null && typeof value === 'object';
}
exports.isString = isString;
exports.isArray = isArray;
exports.isObject = isObject;
exports.forOwn = forOwn;
exports.assign = assign;
exports.uniq = uniq;
exports.echo = echo;