From 09f67f26334ead1502976e5462300e2a06874fbd Mon Sep 17 00:00:00 2001 From: const_var <79074469+bhvngt@users.noreply.github.com> Date: Wed, 23 Mar 2022 02:46:02 +0530 Subject: [PATCH] fix: emit equal when needed - alternative to #1853 (#1922) * fix: emit equal when needed - alternative to #1853 * fix: prettier errors * fix: tslint errors * Update lib/vocabularies/validation/enum.ts * Update lib/vocabularies/validation/enum.ts Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> --- lib/vocabularies/validation/enum.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/vocabularies/validation/enum.ts b/lib/vocabularies/validation/enum.ts index fa85373c4..76377fb02 100644 --- a/lib/vocabularies/validation/enum.ts +++ b/lib/vocabularies/validation/enum.ts @@ -20,7 +20,9 @@ const def: CodeKeywordDefinition = { const {gen, data, $data, schema, schemaCode, it} = cxt if (!$data && schema.length === 0) throw new Error("enum must have non-empty array") const useLoop = schema.length >= it.opts.loopEnum - const eql = useFunc(gen, equal) + let eql: Name | undefined + const getEql = (): Name => (eql ??= useFunc(gen, equal)) + let valid: Code if (useLoop || $data) { valid = gen.let("valid") @@ -36,14 +38,14 @@ const def: CodeKeywordDefinition = { function loopEnum(): void { gen.assign(valid, false) gen.forOf("v", schemaCode as Code, (v) => - gen.if(_`${eql}(${data}, ${v})`, () => gen.assign(valid, true).break()) + gen.if(_`${getEql()}(${data}, ${v})`, () => gen.assign(valid, true).break()) ) } function equalCode(vSchema: Name, i: number): Code { const sch = schema[i] return typeof sch === "object" && sch !== null - ? _`${eql}(${data}, ${vSchema}[${i}])` + ? _`${getEql()}(${data}, ${vSchema}[${i}])` : _`${data} === ${sch}` } },