From 0f9adfb067490383b1e9d76a2c0c8d92566005c3 Mon Sep 17 00:00:00 2001 From: harttle Date: Sat, 23 Dec 2017 21:31:55 +0800 Subject: [PATCH] change: remove deprecated features, coverall --- index.js | 4 ---- src/scope.js | 16 ++++++++++------ src/tokenizer.js | 2 +- test/scope.js | 10 ++++++++++ 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 0d9b50ff1..56f8bcf64 100644 --- a/index.js +++ b/index.js @@ -52,10 +52,6 @@ var _engine = { return this.getTemplate(filepath, opts.root) .then(templates => this.render(templates, ctx, opts)) }, - evalOutput: function (str, scope) { - console.warn('[liquidjs:deprecated] use .evalValue() instead of .evalOutput') - return this.evalValue(str, scope) - }, evalValue: function (str, scope) { var tpl = this.parser.parseValue(str.trim()) return this.renderer.evalValue(tpl, scope) diff --git a/src/scope.js b/src/scope.js index 6a88279f8..9c46863f4 100644 --- a/src/scope.js +++ b/src/scope.js @@ -11,9 +11,6 @@ var Scope = { return ctx }, get: function (str) { - if (str === 'liquid') { - throw new Error('NO LONGER SUPPORTED: use scope.opts instead of scope.get("liquid")') - } try { return this.getPropertyByPath(this.scopes, str) } catch (e) { @@ -129,12 +126,19 @@ function setPropertyByPath (obj, path, val) { var paths = (path + '').replace(/\[/g, '.').replace(/\]/g, '').split('.') for (var i = 0; i < paths.length; i++) { var key = paths[i] + if (!_.isObject(obj)) { + // cannot set property of non-object + return + } + // for end point if (i === paths.length - 1) { return (obj[key] = val) } - if (undefined === obj[key]) obj[key] = {} - // case for readonly objects - obj = obj[key] || {} + // if path not exist + if (undefined === obj[key]) { + obj[key] = {} + } + obj = obj[key] } } diff --git a/src/tokenizer.js b/src/tokenizer.js index 811db8274..38a3d65c2 100644 --- a/src/tokenizer.js +++ b/src/tokenizer.js @@ -63,7 +63,7 @@ function parse (input, file, options) { function parseHTMLToken (begin, end) { var htmlFragment = input.slice(begin, end) - currIndent = _.last((htmlFragment || '').split('\n')).length + currIndent = _.last((htmlFragment).split('\n')).length return { type: 'html', diff --git a/test/scope.js b/test/scope.js index b0067348a..17ed436ab 100644 --- a/test/scope.js +++ b/test/scope.js @@ -121,6 +121,16 @@ describe('scope', function () { scope.set('bar.coo', 'COO') expect(scope.get('bar.coo')).to.equal('COO') }) + it('should keep other properties of parent', function () { + scope.push({obj: {foo: 'FOO'}}) + scope.set('obj.bar', 'BAR') + expect(scope.get('obj.foo')).to.equal('FOO') + }) + it('should abort if property cannot be set', function () { + scope.push({obj: {foo: 'FOO'}}) + scope.set('obj.foo.bar', 'BAR') + expect(scope.get('obj.foo')).to.equal('FOO') + }) it("should set parents' corresponding value", function () { scope.push({}) scope.set('foo', 'bar')