diff --git a/src/scope.js b/src/scope.js index 1a6a796e1..a3a03fae7 100644 --- a/src/scope.js +++ b/src/scope.js @@ -30,7 +30,6 @@ var Scope = { }) }, push: function (ctx) { - assert(ctx, `trying to push ${ctx} into scopes`) this.scopes.push(ctx) }, pop: function (ctx) { diff --git a/tags/assign.js b/tags/assign.js index 4cd6c50cb..c25119207 100644 --- a/tags/assign.js +++ b/tags/assign.js @@ -1,3 +1,4 @@ +'use strict' const Liquid = require('..') const lexical = Liquid.lexical const re = new RegExp(`(${lexical.identifier.source})\\s*=(.*)`) @@ -6,13 +7,15 @@ const assert = require('../src/util/assert.js') module.exports = function (liquid) { liquid.registerTag('assign', { parse: function (token) { - var match = token.args.match(re) + let match = token.args.match(re) assert(match, `illegal token ${token.raw}`) this.key = match[1] this.value = match[2] }, render: function (scope) { - scope.set(this.key, liquid.evalValue(this.value, scope)) + let ctx = Object.create(null) + ctx[this.key] = liquid.evalValue(this.value, scope) + scope.push(ctx) return Promise.resolve('') } }) diff --git a/test/filters.js b/test/filters.js index 4a2fc1c38..631b196c5 100644 --- a/test/filters.js +++ b/test/filters.js @@ -54,8 +54,7 @@ describe('filters', function () { {%- for item in everything -%} - {{ item }} - {% endfor -%}`, - `- apples + {% endfor -%}`, `- apples - oranges - peaches - carrots @@ -70,8 +69,7 @@ describe('filters', function () { {%- for item in everything -%} - {{ item }} - {% endfor -%}`, - `- apples + {% endfor -%}`, `- apples - oranges - peaches - carrots @@ -156,7 +154,7 @@ describe('filters', function () { it('should support join', function () { var src = '{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}' + - '{{ beatles | join: " and " }}' + '{{ beatles | join: " and " }}' return test(src, 'John and Paul and George and Ringo') }) diff --git a/test/scope.js b/test/scope.js index 6ecce7de1..889d61d93 100644 --- a/test/scope.js +++ b/test/scope.js @@ -187,11 +187,6 @@ describe('scope', function () { }) describe('.push()', function () { - it('should throw when trying to push non-object', function () { - expect(function () { - scope.push(false) - }).to.throw() - }) it('should push scope', function () { scope.set('bar', 'bar') scope.push({ diff --git a/test/tags/assign.js b/test/tags/assign.js index d93f2831f..6a8e243cc 100644 --- a/test/tags/assign.js +++ b/test/tags/assign.js @@ -1,53 +1,67 @@ +'use strict' const Liquid = require('../..') const chai = require('chai') const expect = chai.expect chai.use(require('chai-as-promised')) describe('tags/assign', function () { - var liquid = Liquid() + let liquid = Liquid() it('should throw when variable expression illegal', function () { - var src = '{% assign / %}' - var ctx = {} + let src = '{% assign / %}' + let ctx = {} return expect(liquid.parseAndRender(src, ctx)).to.be.rejectedWith(/illegal/) }) - - it('should assign as string', function () { - var src = '{% assign foo="bar" %}{{foo}}' + it('should support assign to a string', function () { + let src = '{% assign foo="bar" %}{{foo}}' return expect(liquid.parseAndRender(src)) .to.eventually.equal('bar') }) + it('should support assign to a number', function () { + let src = '{% assign foo=10086 %}{{foo}}' + return expect(liquid.parseAndRender(src)) + .to.eventually.equal('10086') + }) + it('should shading rather than overwriting', function () { + let ctx = {foo: 'foo'} + let src = '{% assign foo="FOO" %}{{foo}}' + return liquid.parseAndRender(src, ctx) + .then(x => { + expect(x).to.equal('FOO') + expect(ctx.foo).to.equal('foo') + }) + }) it('should assign as array', function () { - var src = '{% assign foo=(1..3) %}{{foo}}' + let src = '{% assign foo=(1..3) %}{{foo}}' return expect(liquid.parseAndRender(src)) .to.eventually.equal('[1,2,3]') }) it('should assign as filter result', function () { - var src = '{% assign foo="a b" | capitalize | split: " " | first %}{{foo}}' + let src = '{% assign foo="a b" | capitalize | split: " " | first %}{{foo}}' return expect(liquid.parseAndRender(src)) .to.eventually.equal('A') }) it('should assign var-1', function () { - var src = '{% assign var-1 = 5 %}{{ var-1 }}' + let src = '{% assign var-1 = 5 %}{{ var-1 }}' return expect(liquid.parseAndRender(src)).to.eventually.equal('5') }) it('should assign var-', function () { - var src = '{% assign var- = 5 %}{{ var- }}' + let src = '{% assign var- = 5 %}{{ var- }}' return expect(liquid.parseAndRender(src)).to.eventually.equal('5') }) it('should assign -var', function () { - var src = '{% assign -var = 5 %}{{ -var }}' + let src = '{% assign -let = 5 %}{{ -let }}' return expect(liquid.parseAndRender(src)).to.eventually.equal('5') }) it('should assign -5-5', function () { - var src = '{% assign -5-5 = 5 %}{{ -5-5 }}' + let src = '{% assign -5-5 = 5 %}{{ -5-5 }}' return expect(liquid.parseAndRender(src)).to.eventually.equal('5') }) it('should assign 4-3', function () { - var src = '{% assign 4-3 = 5 %}{{ 4-3 }}' + let src = '{% assign 4-3 = 5 %}{{ 4-3 }}' return expect(liquid.parseAndRender(src)).to.eventually.equal('5') }) it('should not assign -6', function () { - var src = '{% assign -6 = 5 %}{{ -6 }}' + let src = '{% assign -6 = 5 %}{{ -6 }}' return expect(liquid.parseAndRender(src)).to.eventually.equal('-6') }) })