forked from MapComplete/MapComplete
		
	Fixed all shadowed mappings, make those an error
This commit is contained in:
		
							parent
							
								
									aa5e944447
								
							
						
					
					
						commit
						fe8c63d762
					
				
					 4 changed files with 75 additions and 66 deletions
				
			
		|  | @ -238,8 +238,25 @@ export class PrevalidateTheme extends Fuse<LayoutConfigJson> { | |||
| } | ||||
| 
 | ||||
| export class DetectShadowedMappings extends DesugaringStep<TagRenderingConfigJson> { | ||||
|     constructor() { | ||||
|     private readonly _calculatedTagNames: string[]; | ||||
|     constructor(layerConfig?: LayerConfigJson) { | ||||
|         super("Checks that the mappings don't shadow each other", [], "DetectShadowedMappings"); | ||||
|         this._calculatedTagNames = DetectShadowedMappings.extractCalculatedTagNames(layerConfig); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      *  | ||||
|      * DetectShadowedMappings.extractCalculatedTagNames({calculatedTags: ["_abc:=js()"]}) // => ["_abc"]
 | ||||
|      * DetectShadowedMappings.extractCalculatedTagNames({calculatedTags: ["_abc=js()"]}) // => ["_abc"]
 | ||||
|      */ | ||||
|     private static extractCalculatedTagNames(layerConfig?: LayerConfigJson){ | ||||
|         return layerConfig?.calculatedTags?.map(ct => { | ||||
|             if(ct.indexOf(':=') >= 0){ | ||||
|                 return ct.split(':=')[0] | ||||
|             } | ||||
|             return ct.split("=")[0] | ||||
|         }) ?? [] | ||||
|          | ||||
|     } | ||||
| 
 | ||||
|     convert(json: TagRenderingConfigJson, context: string): { result: TagRenderingConfigJson; errors?: string[]; warnings?: string[] } { | ||||
|  | @ -248,6 +265,10 @@ export class DetectShadowedMappings extends DesugaringStep<TagRenderingConfigJso | |||
|         if (json.mappings === undefined || json.mappings.length === 0) { | ||||
|             return {result: json} | ||||
|         } | ||||
|         const defaultProperties = {} | ||||
|         for (const calculatedTagName of this._calculatedTagNames) { | ||||
|             defaultProperties[calculatedTagName] = "some_calculated_tag_value_for_"+calculatedTagName | ||||
|         } | ||||
|         const parsedConditions = json.mappings.map(m => { | ||||
|             const ifTags = TagUtils.Tag(m.if); | ||||
|             if(m.hideInAnswer !== undefined && m.hideInAnswer !== false && m.hideInAnswer !== true){ | ||||
|  | @ -263,7 +284,7 @@ export class DetectShadowedMappings extends DesugaringStep<TagRenderingConfigJso | |||
|                 // Yes, it might be shadowed, but running this check is to difficult right now
 | ||||
|                 continue | ||||
|             } | ||||
|             const keyValues = parsedConditions[i].asChange({}); | ||||
|             const keyValues = parsedConditions[i].asChange(defaultProperties); | ||||
|             const properties = {} | ||||
|             keyValues.forEach(({k, v}) => { | ||||
|                 properties[k] = v | ||||
|  | @ -289,10 +310,6 @@ export class DetectShadowedMappings extends DesugaringStep<TagRenderingConfigJso | |||
| 
 | ||||
|         } | ||||
| 
 | ||||
|         // TODO make this errors again
 | ||||
|         warnings.push(...errors) | ||||
|         errors.splice(0, errors.length) | ||||
| 
 | ||||
|         return { | ||||
|             errors, | ||||
|             warnings, | ||||
|  | @ -341,9 +358,9 @@ export class DetectMappingsWithImages extends DesugaringStep<TagRenderingConfigJ | |||
| } | ||||
| 
 | ||||
| export class ValidateTagRenderings extends Fuse<TagRenderingConfigJson> { | ||||
|     constructor() { | ||||
|     constructor(layerConfig: LayerConfigJson) { | ||||
|         super("Various validation on tagRenderingConfigs", | ||||
|             new DetectShadowedMappings(), | ||||
|             new DetectShadowedMappings( layerConfig), | ||||
|             new DetectMappingsWithImages()     | ||||
|         ); | ||||
|     } | ||||
|  | @ -439,7 +456,7 @@ export class ValidateLayer extends DesugaringStep<LayerConfigJson> { | |||
|                 } | ||||
|             } | ||||
|             if (json.tagRenderings !== undefined) { | ||||
|                const r = new OnEvery("tagRenderings", new ValidateTagRenderings()).convert(json, context) | ||||
|                const r = new OnEvery("tagRenderings", new ValidateTagRenderings(json)).convert(json, context) | ||||
|                 warnings.push(...(r.warnings??[])) | ||||
|                 errors.push(...(r.errors??[])) | ||||
|                 information.push(...(r.information??[])) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue