From dc6a3013874872ac85f1fbe5184c74631122d851 Mon Sep 17 00:00:00 2001 From: Francisco Soto Date: Fri, 18 Aug 2023 15:08:18 -0700 Subject: [PATCH] fix: for throws undefined var with a null value with strictVariables --- src/context/context.ts | 4 ++-- src/util/underscore.ts | 4 ++++ test/integration/tags/for.spec.ts | 6 ++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/context/context.ts b/src/context/context.ts index a5dbe200fc..64ab56f440 100644 --- a/src/context/context.ts +++ b/src/context/context.ts @@ -2,7 +2,7 @@ import { Drop } from '../drop/drop' import { __assign } from 'tslib' import { NormalizedFullOptions, defaultOptions, RenderOptions } from '../liquid-options' import { Scope } from './scope' -import { isArray, isNil, isString, isFunction, toLiquid, InternalUndefinedVariableError, toValueSync } from '../util' +import { isArray, isNil, isUndefined, isString, isFunction, toLiquid, InternalUndefinedVariableError, toValueSync } from '../util' type PropertyKey = string | number; @@ -80,7 +80,7 @@ export class Context { if (isString(paths)) paths = paths.split('.') for (let i = 0; i < paths.length; i++) { scope = yield readProperty(scope as object, paths[i], this.ownPropertyOnly) - if (isNil(scope) && this.strictVariables) { + if (this.strictVariables && isUndefined(scope)) { throw new InternalUndefinedVariableError((paths as string[]).slice(0, i + 1).join!('.')) } } diff --git a/src/util/underscore.ts b/src/util/underscore.ts index fbe4e49833..08168df7bf 100644 --- a/src/util/underscore.ts +++ b/src/util/underscore.ts @@ -63,6 +63,10 @@ export function isNil (value: any): boolean { return value == null } +export function isUndefined (value: any): boolean { + return value === undefined +} + export function isArray (value: any): value is any[] { // be compatible with IE 8 return toString.call(value) === '[object Array]' diff --git a/test/integration/tags/for.spec.ts b/test/integration/tags/for.spec.ts index 35c8e52d06..0d09b8c1a7 100644 --- a/test/integration/tags/for.spec.ts +++ b/test/integration/tags/for.spec.ts @@ -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}}'