Skip to content

Commit

Permalink
fix: for throws undefined var with a null value with strictVariables
Browse files Browse the repository at this point in the history
  • Loading branch information
ebobby committed Aug 16, 2023
1 parent 3bd8d2e commit f360d18
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ export class Context {
public * _getFromScope (scope: unknown, paths: PropertyKey[] | string): IterableIterator<unknown> {
if (isString(paths)) paths = paths.split('.')
for (let i = 0; i < paths.length; i++) {
const previousScope = scope
scope = yield readProperty(scope as object, paths[i], this.ownPropertyOnly)
if (isNil(scope) && this.strictVariables) {
if (this.strictVariables && isNil(scope) && !definedProperty(previousScope as object, paths[i], this.ownPropertyOnly)) {
throw new InternalUndefinedVariableError((paths as string[]).slice(0, i + 1).join!('.'))
}
}
Expand All @@ -105,6 +106,13 @@ export class Context {
}
}

export function definedProperty (obj: Scope, key: PropertyKey, ownPropertyOnly: boolean) {
obj = toLiquid(obj)
if (isNil(obj)) return false
if (isArray(obj)) return false
return ownPropertyOnly && Object.hasOwnProperty.call(obj, key)
}

export function readProperty (obj: Scope, key: PropertyKey, ownPropertyOnly: boolean) {
obj = toLiquid(obj)
if (isNil(obj)) return obj
Expand Down
6 changes: 6 additions & 0 deletions test/integration/tags/for.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ describe('tags/for', function () {
const html = await liquid.parseAndRender(src, scope)
return expect(html).toBe('str-"string"-string')
})
it('should not report undefined variable on null value', async function () {
const engine = new Liquid({ strictVariables: true })
const src = '{% assign hello = "hello,world" | split: "," | concat: null %}{% for i in hello %}{{ i }},{% endfor %}'
const html = await engine.parseAndRender(src, scope)
return expect(html).toBe('hello,world,,')
})
describe('illegal', function () {
it('should reject when for not closed', function () {
const src = '{%for c in alpha%}{{c}}'
Expand Down

0 comments on commit f360d18

Please sign in to comment.