forked from MapComplete/MapComplete
		
	Formatting
This commit is contained in:
		
							parent
							
								
									39deec76d2
								
							
						
					
					
						commit
						d2f4df3b60
					
				
					 5 changed files with 82 additions and 72 deletions
				
			
		|  | @ -244,8 +244,8 @@ export class And extends TagsFilter { | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             for (let i = 0; i < optimized.length; i++){ |             for (let i = 0; i < optimized.length; i++) { | ||||||
|                 const opt = optimized[i]; |                 const opt = optimized[i] | ||||||
|                 if (opt instanceof Tag) { |                 if (opt instanceof Tag) { | ||||||
|                     const k = opt.key |                     const k = opt.key | ||||||
|                     const v = properties[k] |                     const v = properties[k] | ||||||
|  | @ -266,23 +266,24 @@ export class And extends TagsFilter { | ||||||
|                     if (v === undefined) { |                     if (v === undefined) { | ||||||
|                         continue |                         continue | ||||||
|                     } |                     } | ||||||
|                     if(opt.invert){ |                     if (opt.invert) { | ||||||
|                         // We should _not_ match this value
 |                         // We should _not_ match this value
 | ||||||
|                         // If 'v' is given, we already know what value it should be
 |                         // If 'v' is given, we already know what value it should be
 | ||||||
|                         // If 'v' is the not-expected value, we have a conflict and return false
 |                         // If 'v' is the not-expected value, we have a conflict and return false
 | ||||||
|                         // Otherwise, we can safely drop this value
 |                         // Otherwise, we can safely drop this value
 | ||||||
| 
 | 
 | ||||||
|                         const doesMatch = (typeof opt.value === "string" && v === opt.value) || |                         const doesMatch = | ||||||
|                             (v.match(<RegExp> opt.value) !== null) |                             (typeof opt.value === "string" && v === opt.value) || | ||||||
|  |                             v.match(<RegExp>opt.value) !== null | ||||||
| 
 | 
 | ||||||
|                         if(doesMatch){ |                         if (doesMatch) { | ||||||
|                             // We have a conflict as 'opt' is inverted
 |                             // We have a conflict as 'opt' is inverted
 | ||||||
|                             return false |                             return false | ||||||
|                         }else{ |                         } else { | ||||||
|                             optimized.splice(i, 1) |                             optimized.splice(i, 1) | ||||||
|                             i-- |                             i-- | ||||||
|                         } |                         } | ||||||
|                     }else if (v !== opt.value) { |                     } else if (v !== opt.value) { | ||||||
|                         // detected an internal conflict
 |                         // detected an internal conflict
 | ||||||
|                         return false |                         return false | ||||||
|                     } |                     } | ||||||
|  |  | ||||||
|  | @ -1,16 +1,26 @@ | ||||||
| import {Concat, Conversion, DesugaringContext, DesugaringStep, Each, FirstOf, Fuse, On, SetDefault,} from "./Conversion" | import { | ||||||
| import {LayerConfigJson} from "../Json/LayerConfigJson" |     Concat, | ||||||
| import {TagRenderingConfigJson} from "../Json/TagRenderingConfigJson" |     Conversion, | ||||||
| import {Utils} from "../../../Utils" |     DesugaringContext, | ||||||
|  |     DesugaringStep, | ||||||
|  |     Each, | ||||||
|  |     FirstOf, | ||||||
|  |     Fuse, | ||||||
|  |     On, | ||||||
|  |     SetDefault, | ||||||
|  | } from "./Conversion" | ||||||
|  | import { LayerConfigJson } from "../Json/LayerConfigJson" | ||||||
|  | import { TagRenderingConfigJson } from "../Json/TagRenderingConfigJson" | ||||||
|  | import { Utils } from "../../../Utils" | ||||||
| import RewritableConfigJson from "../Json/RewritableConfigJson" | import RewritableConfigJson from "../Json/RewritableConfigJson" | ||||||
| import SpecialVisualizations from "../../../UI/SpecialVisualizations" | import SpecialVisualizations from "../../../UI/SpecialVisualizations" | ||||||
| import Translations from "../../../UI/i18n/Translations" | import Translations from "../../../UI/i18n/Translations" | ||||||
| import {Translation} from "../../../UI/i18n/Translation" | import { Translation } from "../../../UI/i18n/Translation" | ||||||
| import tagrenderingconfigmeta from "../../../assets/tagrenderingconfigmeta.json" | import tagrenderingconfigmeta from "../../../assets/tagrenderingconfigmeta.json" | ||||||
| import {AddContextToTranslations} from "./AddContextToTranslations" | import { AddContextToTranslations } from "./AddContextToTranslations" | ||||||
| import FilterConfigJson from "../Json/FilterConfigJson" | import FilterConfigJson from "../Json/FilterConfigJson" | ||||||
| import predifined_filters from "../../../assets/layers/filters/filters.json" | import predifined_filters from "../../../assets/layers/filters/filters.json" | ||||||
| import {TagConfigJson} from "../Json/TagConfigJson" | import { TagConfigJson } from "../Json/TagConfigJson" | ||||||
| import PointRenderingConfigJson from "../Json/PointRenderingConfigJson" | import PointRenderingConfigJson from "../Json/PointRenderingConfigJson" | ||||||
| import LineRenderingConfigJson from "../Json/LineRenderingConfigJson" | import LineRenderingConfigJson from "../Json/LineRenderingConfigJson" | ||||||
| 
 | 
 | ||||||
|  | @ -40,11 +50,11 @@ class ExpandFilter extends DesugaringStep<LayerConfigJson> { | ||||||
|         context: string |         context: string | ||||||
|     ): { result: LayerConfigJson; errors?: string[]; warnings?: string[]; information?: string[] } { |     ): { result: LayerConfigJson; errors?: string[]; warnings?: string[]; information?: string[] } { | ||||||
|         if (json.filter === undefined || json.filter === null) { |         if (json.filter === undefined || json.filter === null) { | ||||||
|             return {result: json} // Nothing to change here
 |             return { result: json } // Nothing to change here
 | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         if (json.filter["sameAs"] !== undefined) { |         if (json.filter["sameAs"] !== undefined) { | ||||||
|             return {result: json} // Nothing to change here
 |             return { result: json } // Nothing to change here
 | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         const newFilters: FilterConfigJson[] = [] |         const newFilters: FilterConfigJson[] = [] | ||||||
|  | @ -60,9 +70,9 @@ class ExpandFilter extends DesugaringStep<LayerConfigJson> { | ||||||
|                     if (split.length > 2) { |                     if (split.length > 2) { | ||||||
|                         errors.push( |                         errors.push( | ||||||
|                             context + |                             context + | ||||||
|                             ": invalid filter name: " + |                                 ": invalid filter name: " + | ||||||
|                             filter + |                                 filter + | ||||||
|                             ", expected `layername.filterid`" |                                 ", expected `layername.filterid`" | ||||||
|                         ) |                         ) | ||||||
|                     } |                     } | ||||||
|                     const layer = this._state.sharedLayers.get(split[0]) |                     const layer = this._state.sharedLayers.get(split[0]) | ||||||
|  | @ -161,7 +171,6 @@ class ExpandTagRendering extends Conversion< | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private lookup(name: string): TagRenderingConfigJson[] | undefined { |     private lookup(name: string): TagRenderingConfigJson[] | undefined { | ||||||
| 
 |  | ||||||
|         const direct = this.directLookup(name) |         const direct = this.directLookup(name) | ||||||
| 
 | 
 | ||||||
|         if (direct === undefined) { |         if (direct === undefined) { | ||||||
|  | @ -198,7 +207,7 @@ class ExpandTagRendering extends Conversion< | ||||||
|         if (state.tagRenderings.has(name)) { |         if (state.tagRenderings.has(name)) { | ||||||
|             return [state.tagRenderings.get(name)] |             return [state.tagRenderings.get(name)] | ||||||
|         } |         } | ||||||
|         if(this._tagRenderingsByLabel.has(name)){ |         if (this._tagRenderingsByLabel.has(name)) { | ||||||
|             return this._tagRenderingsByLabel.get(name) |             return this._tagRenderingsByLabel.get(name) | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  | @ -239,7 +248,7 @@ class ExpandTagRendering extends Conversion< | ||||||
|                 if (found.condition === undefined) { |                 if (found.condition === undefined) { | ||||||
|                     found.condition = layer.source.osmTags |                     found.condition = layer.source.osmTags | ||||||
|                 } else { |                 } else { | ||||||
|                     found.condition = {and: [found.condition, layer.source.osmTags]} |                     found.condition = { and: [found.condition, layer.source.osmTags] } | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|  | @ -276,18 +285,18 @@ class ExpandTagRendering extends Conversion< | ||||||
|                     warnings.push( |                     warnings.push( | ||||||
|                         `${ctx}: A literal rendering was detected: ${tr} |                         `${ctx}: A literal rendering was detected: ${tr} | ||||||
|     Did you perhaps forgot to add a layer name as 'layername.${tr}'? ` +
 |     Did you perhaps forgot to add a layer name as 'layername.${tr}'? ` +
 | ||||||
|                         Array.from(state.sharedLayers.keys()).join(", ") |                             Array.from(state.sharedLayers.keys()).join(", ") | ||||||
|                     ) |                     ) | ||||||
|                 } |                 } | ||||||
| 
 | 
 | ||||||
|                 if (this._options?.noHardcodedStrings && this._state.sharedLayers.size > 0) { |                 if (this._options?.noHardcodedStrings && this._state.sharedLayers.size > 0) { | ||||||
|                     errors.push( |                     errors.push( | ||||||
|                         ctx + |                         ctx + | ||||||
|                         "Detected an invocation to a builtin tagRendering, but this tagrendering was not found: " + |                             "Detected an invocation to a builtin tagRendering, but this tagrendering was not found: " + | ||||||
|                         tr + |                             tr + | ||||||
|                         " \n    Did you perhaps forget to add the layer as prefix, such as `icons." + |                             " \n    Did you perhaps forget to add the layer as prefix, such as `icons." + | ||||||
|                         tr + |                             tr + | ||||||
|                         "`? " |                             "`? " | ||||||
|                     ) |                     ) | ||||||
|                 } |                 } | ||||||
| 
 | 
 | ||||||
|  | @ -318,11 +327,11 @@ class ExpandTagRendering extends Conversion< | ||||||
|                 } |                 } | ||||||
|                 errors.push( |                 errors.push( | ||||||
|                     "At " + |                     "At " + | ||||||
|                     ctx + |                         ctx + | ||||||
|                     ": an object calling a builtin can only have keys `builtin` or `override`, but a key with name `" + |                         ": an object calling a builtin can only have keys `builtin` or `override`, but a key with name `" + | ||||||
|                     key + |                         key + | ||||||
|                     "` was found. This won't be picked up! The full object is: " + |                         "` was found. This won't be picked up! The full object is: " + | ||||||
|                     JSON.stringify(tr) |                         JSON.stringify(tr) | ||||||
|                 ) |                 ) | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|  | @ -346,22 +355,22 @@ class ExpandTagRendering extends Conversion< | ||||||
|                             if (state.sharedLayers.size === 0) { |                             if (state.sharedLayers.size === 0) { | ||||||
|                                 warnings.push( |                                 warnings.push( | ||||||
|                                     ctx + |                                     ctx + | ||||||
|                                     ": BOOTSTRAPPING. Rerun generate layeroverview. While reusing tagrendering: " + |                                         ": BOOTSTRAPPING. Rerun generate layeroverview. While reusing tagrendering: " + | ||||||
|                                     name + |                                         name + | ||||||
|                                     ": layer " + |                                         ": layer " + | ||||||
|                                     layerName + |                                         layerName + | ||||||
|                                     " not found. Maybe you meant on of " + |                                         " not found. Maybe you meant on of " + | ||||||
|                                     candidates.slice(0, 3).join(", ") |                                         candidates.slice(0, 3).join(", ") | ||||||
|                                 ) |                                 ) | ||||||
|                             } else { |                             } else { | ||||||
|                                 errors.push( |                                 errors.push( | ||||||
|                                     ctx + |                                     ctx + | ||||||
|                                     ": While reusing tagrendering: " + |                                         ": While reusing tagrendering: " + | ||||||
|                                     name + |                                         name + | ||||||
|                                     ": layer " + |                                         ": layer " + | ||||||
|                                     layerName + |                                         layerName + | ||||||
|                                     " not found. Maybe you meant on of " + |                                         " not found. Maybe you meant on of " + | ||||||
|                                     candidates.slice(0, 3).join(", ") |                                         candidates.slice(0, 3).join(", ") | ||||||
|                                 ) |                                 ) | ||||||
|                             } |                             } | ||||||
|                             continue |                             continue | ||||||
|  | @ -373,11 +382,11 @@ class ExpandTagRendering extends Conversion< | ||||||
|                     candidates = Utils.sortedByLevenshteinDistance(name, candidates, (i) => i) |                     candidates = Utils.sortedByLevenshteinDistance(name, candidates, (i) => i) | ||||||
|                     errors.push( |                     errors.push( | ||||||
|                         ctx + |                         ctx + | ||||||
|                         ": The tagRendering with identifier " + |                             ": The tagRendering with identifier " + | ||||||
|                         name + |                             name + | ||||||
|                         " was not found.\n\tDid you mean one of " + |                             " was not found.\n\tDid you mean one of " + | ||||||
|                         candidates.join(", ") + |                             candidates.join(", ") + | ||||||
|                         "?\n(Hint: did you add a new label and are you trying to use this label at the same time? Run 'reset:layeroverview' first" |                             "?\n(Hint: did you add a new label and are you trying to use this label at the same time? Run 'reset:layeroverview' first" | ||||||
|                     ) |                     ) | ||||||
|                     continue |                     continue | ||||||
|                 } |                 } | ||||||
|  | @ -463,7 +472,7 @@ export class ExpandRewrite<T> extends Conversion<T | RewritableConfigJson<T>, T[ | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             if (typeof obj === "object") { |             if (typeof obj === "object") { | ||||||
|                 obj = {...obj} |                 obj = { ...obj } | ||||||
| 
 | 
 | ||||||
|                 const isTr = targetIsTranslation && Translations.isProbablyATranslation(obj) |                 const isTr = targetIsTranslation && Translations.isProbablyATranslation(obj) | ||||||
| 
 | 
 | ||||||
|  | @ -526,12 +535,12 @@ export class ExpandRewrite<T> extends Conversion<T | RewritableConfigJson<T>, T[ | ||||||
|         context: string |         context: string | ||||||
|     ): { result: T[]; errors?: string[]; warnings?: string[]; information?: string[] } { |     ): { result: T[]; errors?: string[]; warnings?: string[]; information?: string[] } { | ||||||
|         if (json === null || json === undefined) { |         if (json === null || json === undefined) { | ||||||
|             return {result: []} |             return { result: [] } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         if (json["rewrite"] === undefined) { |         if (json["rewrite"] === undefined) { | ||||||
|             // not a rewrite
 |             // not a rewrite
 | ||||||
|             return {result: [<T>json]} |             return { result: [<T>json] } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         const rewrite = <RewritableConfigJson<T>>json |         const rewrite = <RewritableConfigJson<T>>json | ||||||
|  | @ -571,7 +580,7 @@ export class ExpandRewrite<T> extends Conversion<T | RewritableConfigJson<T>, T[ | ||||||
|             ts.push(t) |             ts.push(t) | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return {result: ts} |         return { result: ts } | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -855,7 +864,7 @@ class ExpandIconBadges extends DesugaringStep<PointRenderingConfigJson | LineRen | ||||||
|         information?: string[] |         information?: string[] | ||||||
|     } { |     } { | ||||||
|         if (!json["iconBadges"]) { |         if (!json["iconBadges"]) { | ||||||
|             return {result: json} |             return { result: json } | ||||||
|         } |         } | ||||||
|         const badgesJson = (<PointRenderingConfigJson>json).iconBadges |         const badgesJson = (<PointRenderingConfigJson>json).iconBadges | ||||||
| 
 | 
 | ||||||
|  | @ -866,7 +875,7 @@ class ExpandIconBadges extends DesugaringStep<PointRenderingConfigJson | LineRen | ||||||
|         for (let i = 0; i < badgesJson.length; i++) { |         for (let i = 0; i < badgesJson.length; i++) { | ||||||
|             const iconBadge: { if: TagConfigJson; then: string | TagRenderingConfigJson } = |             const iconBadge: { if: TagConfigJson; then: string | TagRenderingConfigJson } = | ||||||
|                 badgesJson[i] |                 badgesJson[i] | ||||||
|             const {errors, result, warnings} = this._expand.convert( |             const { errors, result, warnings } = this._expand.convert( | ||||||
|                 iconBadge.then, |                 iconBadge.then, | ||||||
|                 context + ".iconBadges[" + i + "]" |                 context + ".iconBadges[" + i + "]" | ||||||
|             ) |             ) | ||||||
|  | @ -886,7 +895,7 @@ class ExpandIconBadges extends DesugaringStep<PointRenderingConfigJson | LineRen | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return { |         return { | ||||||
|             result: {...json, iconBadges}, |             result: { ...json, iconBadges }, | ||||||
|             errors: errs, |             errors: errs, | ||||||
|             warnings: warns, |             warnings: warns, | ||||||
|         } |         } | ||||||
|  | @ -899,7 +908,7 @@ class PreparePointRendering extends Fuse<PointRenderingConfigJson | LineRenderin | ||||||
|             "Prepares point renderings by expanding 'icon' and 'iconBadges'", |             "Prepares point renderings by expanding 'icon' and 'iconBadges'", | ||||||
|             new On( |             new On( | ||||||
|                 "icon", |                 "icon", | ||||||
|                 new FirstOf(new ExpandTagRendering(state, layer, {applyCondition: false})) |                 new FirstOf(new ExpandTagRendering(state, layer, { applyCondition: false })) | ||||||
|             ), |             ), | ||||||
|             new ExpandIconBadges(state, layer) |             new ExpandIconBadges(state, layer) | ||||||
|         ) |         ) | ||||||
|  | @ -922,7 +931,7 @@ export class PrepareLayer extends Fuse<LayerConfigJson> { | ||||||
|             new On( |             new On( | ||||||
|                 "titleIcons", |                 "titleIcons", | ||||||
|                 (layer) => |                 (layer) => | ||||||
|                     new Concat(new ExpandTagRendering(state, layer, {noHardcodedStrings: true})) |                     new Concat(new ExpandTagRendering(state, layer, { noHardcodedStrings: true })) | ||||||
|             ), |             ), | ||||||
|             new ExpandFilter(state) |             new ExpandFilter(state) | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|  | @ -58,8 +58,9 @@ export default class SourceConfig { | ||||||
|                     "Error at " + |                     "Error at " + | ||||||
|                     context + |                     context + | ||||||
|                     ": the specified tags are conflicting with each other: they will never match anything at all.\n" + |                     ": the specified tags are conflicting with each other: they will never match anything at all.\n" + | ||||||
|                     "\tThe offending tags are: "+params.osmTags.asHumanString(false, false, {})+ |                     "\tThe offending tags are: " + | ||||||
|                         "\tThey optmize into 'false' " |                     params.osmTags.asHumanString(false, false, {}) + | ||||||
|  |                     "\tThey optmize into 'false' " | ||||||
|                 ) |                 ) | ||||||
|             } |             } | ||||||
|             if (optimized === true) { |             if (optimized === true) { | ||||||
|  |  | ||||||
|  | @ -1,10 +1,10 @@ | ||||||
| import {TagsFilter} from "../../../Logic/Tags/TagsFilter" | import { TagsFilter } from "../../../Logic/Tags/TagsFilter" | ||||||
| import {And} from "../../../Logic/Tags/And" | import { And } from "../../../Logic/Tags/And" | ||||||
| import {Tag} from "../../../Logic/Tags/Tag" | import { Tag } from "../../../Logic/Tags/Tag" | ||||||
| import {TagUtils} from "../../../Logic/Tags/TagUtils" | import { TagUtils } from "../../../Logic/Tags/TagUtils" | ||||||
| import {Or} from "../../../Logic/Tags/Or" | import { Or } from "../../../Logic/Tags/Or" | ||||||
| import {RegexTag} from "../../../Logic/Tags/RegexTag" | import { RegexTag } from "../../../Logic/Tags/RegexTag" | ||||||
| import {describe, expect, it} from "vitest" | import { describe, expect, it } from "vitest" | ||||||
| 
 | 
 | ||||||
| describe("Tag optimalization", () => { | describe("Tag optimalization", () => { | ||||||
|     describe("And", () => { |     describe("And", () => { | ||||||
|  | @ -76,7 +76,6 @@ describe("Tag optimalization", () => { | ||||||
|             const opt = t.optimize() |             const opt = t.optimize() | ||||||
|             expect(typeof opt !== "boolean").true |             expect(typeof opt !== "boolean").true | ||||||
|             expect(TagUtils.toString(<TagsFilter>opt)).toBe("shop=sports") |             expect(TagUtils.toString(<TagsFilter>opt)).toBe("shop=sports") | ||||||
| 
 |  | ||||||
|         }) |         }) | ||||||
| 
 | 
 | ||||||
|         it("should optimize nested ORs", () => { |         it("should optimize nested ORs", () => { | ||||||
|  | @ -271,7 +270,7 @@ describe("Tag optimalization", () => { | ||||||
|                 or: [ |                 or: [ | ||||||
|                     "club=climbing", |                     "club=climbing", | ||||||
|                     { |                     { | ||||||
|                         and: ["sport=climbing", {or: ["club~*", "office~*"]}], |                         and: ["sport=climbing", { or: ["club~*", "office~*"] }], | ||||||
|                     }, |                     }, | ||||||
|                     { |                     { | ||||||
|                         and: [ |                         and: [ | ||||||
|  |  | ||||||
|  | @ -6,6 +6,6 @@ export default defineConfig({ | ||||||
|     test: { |     test: { | ||||||
|         globals: true, |         globals: true, | ||||||
|         setupFiles: ["./test/testhooks.ts"], |         setupFiles: ["./test/testhooks.ts"], | ||||||
|         include: ["./test/*.spec.ts","./test/**/*.spec.ts", "./*.doctest.ts", "./**/*.doctest.ts"], |         include: ["./test/*.spec.ts", "./test/**/*.spec.ts", "./*.doctest.ts", "./**/*.doctest.ts"], | ||||||
|     }, |     }, | ||||||
| }) | }) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue