diff --git a/src/scope.js b/src/scope.js index 9c46863f4..07e4b833a 100644 --- a/src/scope.js +++ b/src/scope.js @@ -56,15 +56,17 @@ var Scope = { } var key = paths.shift() var value = getValueFromScopes(key, scopes) - return paths.reduce( - (value, key) => { - if (_.isNil(value)) { - throw new TypeError('undefined variable: ' + key) - } - return getValueFromParent(key, value) - }, - value - ) + if (_.isNil(value)) { + throw new TypeError('undefined variable: ' + key) + } + while (paths.length) { + key = paths.shift() + value = getValueFromParent(key, value) + if (_.isNil(value)) { + throw new TypeError('undefined variable: ' + key) + } + } + return value }, /* diff --git a/test/scope.js b/test/scope.js index 17ed436ab..fb3433267 100644 --- a/test/scope.js +++ b/test/scope.js @@ -151,12 +151,18 @@ describe('scope', function () { } expect(fn).to.throw(/undefined variable: notdefined/) }) - it('should throw when deep variable not exist', function () { - scope.set('foo', 'bar') + it('should throw when parent not defined', function () { function fn () { scope.get('foo.bar.not.defined') } - expect(fn).to.throw(/undefined variable: not/) + expect(fn).to.throw(/undefined variable: bar/) + }) + it('should throw when itself not defined', function () { + scope.set('foo', 'bar') + function fn () { + scope.get('foo.BAR') + } + expect(fn).to.throw(/undefined variable: BAR/) }) it('should find variable in parent scope', function () { scope.set('foo', 'foo')