fix: {%assign%} now adds a shading scope

This commit is contained in:
harttle
2018-07-21 21:30:40 +08:00
parent 36f84e4b3c
commit b5cfd2d2c5
5 changed files with 36 additions and 27 deletions
-1
View File
@@ -30,7 +30,6 @@ var Scope = {
})
},
push: function (ctx) {
assert(ctx, `trying to push ${ctx} into scopes`)
this.scopes.push(ctx)
},
pop: function (ctx) {
+5 -2
View File
@@ -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('')
}
})
+3 -5
View File
@@ -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')
})
-5
View File
@@ -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({
+28 -14
View File
@@ -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')
})
})