Add validation warning: a condition which conflicts with a mapping will now emit a warning.

This commit is contained in:
Pieter Vander Vennet 2024-05-07 17:24:16 +02:00
parent a1de3b81ea
commit 600727e820

View file

@ -561,6 +561,57 @@ export class DetectNonErasedKeysInMappings extends DesugaringStep<QuestionableTa
}
}
export class DetectMappingsShadowedByCondition extends DesugaringStep<TagRenderingConfigJson> {
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<TagRenderingConfigJson> {
private readonly _calculatedTagNames: string[]
@ -1079,6 +1130,8 @@ export class ValidateTagRenderings extends Fuse<TagRenderingConfigJson> {
"Various validation on tagRenderingConfigs",
new MiscTagRenderingChecks(),
new DetectShadowedMappings(layerConfig),
new DetectMappingsShadowedByCondition(),
new DetectConflictingAddExtraTags(),
// TODO enable new DetectNonErasedKeysInMappings(),
new DetectMappingsWithImages(doesImageExist),