Skip to content

Commit

Permalink
feat: config.env to throw errors when null values used
Browse files Browse the repository at this point in the history
  • Loading branch information
shepherdwind committed Sep 19, 2016
1 parent a050028 commit 6981dc4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -113,6 +113,7 @@ params:

- escape {boolean} default `true`, default escape variable to html encode, you can set false to close it.
- unescape {object} define the object, which key do not need escape. For example, set unescape equal `{control: true}`, so `$control.html` will not escape.
- env {string} when env equal `development` will throw error when null values are used

#### parse

Expand Down
28 changes: 26 additions & 2 deletions src/compile/references.js
Expand Up @@ -124,9 +124,13 @@ module.exports = function(Velocity, utils) {

if (local.isLocaled) ret = local['value'];

if (ast.path && ret !== undefined) {
if (ast.path) {

utils.some(ast.path, function(property) {
utils.some(ast.path, function(property, i, len) {

if (ret === undefined) {
this._throw(ast, property);
}

// 第三个参数,返回后面的参数ast
ret = this.getAttributes(property, ret, ast);
Expand Down Expand Up @@ -294,11 +298,31 @@ module.exports = function(Velocity, utils) {
}

} else {
this._throw(ast, property, 'TypeError');
ret = undefined;
}
}

return ret;
},

_throw: function(ast, property, errorName) {
if (this.config.env !== 'development') {
return;
}

var text = Velocity.Helper.getRefText(ast);
var pos = ast.pos;
var propertyName = property.type === 'index' ? property.id.value : property.id;
var errorMsg = 'get property ' + propertyName + ' of undefined';
if (errorName === 'TypeError') {
errorMsg = propertyName + ' is not method';
}

errorMsg += '\n at L/N ' + text + ' ' + pos.first_line + ':' + pos.first_column;
var e = new Error(errorMsg);
e.name = errorName || 'ReferenceError';
throw e;
}
})

Expand Down
47 changes: 47 additions & 0 deletions tests/compile.js
Expand Up @@ -186,6 +186,53 @@ describe('Compile', function() {
ret = compile.render(context)
assert.equal('<i>', ret)
})

describe('env', function() {
it('should throw on property when parent is null', function() {
var vm = '$foo.bar';
var compile = new Compile(parse(vm), { env: 'development' })
function foo() {
compile.render()
};
foo.should.throw(/get property bar of undefined/);
});

it('should throw on index when parent is null', function() {
var vm = '$foo[1]';
var compile = new Compile(parse(vm), { env: 'development' })
function foo() {
compile.render()
};
foo.should.throw(/get property 1 of undefined/);
});

it('should throw on function when parent is null', function() {
var vm = '$foo.xx()';
var compile = new Compile(parse(vm), { env: 'development' })
function foo() {
compile.render()
};
foo.should.throw(/get property xx of undefined/);
});

it('should throw when mult level', function() {
var vm = '$foo.bar.xx.bar1';
var compile = new Compile(parse(vm), { env: 'development' })
function foo() {
compile.render({ foo: { bar: {} }});
};
foo.should.throw(/get property bar1 of undefined/);
});

it('not function', function() {
var vm = '$foo.bar.xx()';
var compile = new Compile(parse(vm), { env: 'development' })
function foo() {
return compile.render({ foo: { bar: {} }});
};
foo.should.throw(/xx is not method/);
});
});
})


Expand Down

0 comments on commit 6981dc4

Please sign in to comment.