From 600727e820b6b66b49a02cfe58dedf1ba410da30 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Tue, 7 May 2024 17:24:16 +0200 Subject: [PATCH] Add validation warning: a condition which conflicts with a mapping will now emit a warning. --- .../ThemeConfig/Conversion/Validation.ts | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/Models/ThemeConfig/Conversion/Validation.ts b/src/Models/ThemeConfig/Conversion/Validation.ts index 2e798962e..9d0f7ffa8 100644 --- a/src/Models/ThemeConfig/Conversion/Validation.ts +++ b/src/Models/ThemeConfig/Conversion/Validation.ts @@ -561,6 +561,57 @@ export class DetectNonErasedKeysInMappings extends DesugaringStep { + + constructor() { + super("Checks that, if the tagrendering has a condition, that a mapping is not contradictory to it, i.e. that there are no dead mappings", [], "DetectMappingsShadowedByCondition") + } + + /** + * + * const validator = new DetectMappingsShadowedByCondition() + * const ctx = ConversionContext.construct([],["test"]) + * validator.convert({ + * condition: "count>0", + * mappings:[ + * { + * if: "count=0", + * then:{ + * en: "No count" + * } + * } + * ] + * }, ctx) + * ctx.hasErrors() // => true + */ + convert(json: TagRenderingConfigJson, context: ConversionContext): TagRenderingConfigJson { + if(!json.condition && !json.metacondition){ + return json + } + if(!json.mappings || json.mappings?.length ==0){ + return json + } + let conditionJson = json.condition ?? json.metacondition + if(json.condition !== undefined && json.metacondition !== undefined){ + conditionJson = {and: [json.condition, json.metacondition]} + } + const condition = TagUtils.Tag(conditionJson, context.path.join(".")) + + for (let i = 0; i < json.mappings.length; i++){ + const mapping = json.mappings[i] + const tagIf = TagUtils.Tag(mapping.if, context.path.join(".")) + const optimized = new And([tagIf, condition]).optimize() + if(optimized === false){ + context.enters("mappings",i).warn("Detected a conflicting mapping and condition. The mapping requires tags " + tagIf.asHumanString()+", yet this can never happen because the set condition requires "+condition.asHumanString()) + } + } + + + return undefined + } + +} + export class DetectShadowedMappings extends DesugaringStep { private readonly _calculatedTagNames: string[] @@ -1079,6 +1130,8 @@ export class ValidateTagRenderings extends Fuse { "Various validation on tagRenderingConfigs", new MiscTagRenderingChecks(), new DetectShadowedMappings(layerConfig), + + new DetectMappingsShadowedByCondition(), new DetectConflictingAddExtraTags(), // TODO enable new DetectNonErasedKeysInMappings(), new DetectMappingsWithImages(doesImageExist),