chore: automated housekeeping...

This commit is contained in:
Pieter Vander Vennet 2025-01-18 00:30:06 +01:00
parent d7509c8d6f
commit 66c27cbad4
127 changed files with 6557 additions and 2698 deletions

View file

@ -15,47 +15,73 @@ import { FlatTag, OptimizedTag, TagsFilterClosed } from "../../../Logic/Tags/Tag
import { TagsFilter } from "../../../Logic/Tags/TagsFilter"
import { And } from "../../../Logic/Tags/And"
export class PruneFilters extends DesugaringStep<LayerConfigJson>{
export class PruneFilters extends DesugaringStep<LayerConfigJson> {
constructor() {
super("Removes all filters which are impossible, e.g. because they conflict with the base tags", ["filter"],"PruneFilters")
super(
"Removes all filters which are impossible, e.g. because they conflict with the base tags",
["filter"],
"PruneFilters"
)
}
private prune(sourceTags:FlatTag, filter: FilterConfigJson, context: ConversionContext): FilterConfigJson{
if(!filter.strict){
private prune(
sourceTags: FlatTag,
filter: FilterConfigJson,
context: ConversionContext
): FilterConfigJson {
if (!filter.strict) {
return filter
}
const countBefore = filter.options.length
const newOptions: FilterConfigOptionJson[] = filter.options.filter(option => {
if(!option.osmTags){
return true
}
const condition = <OptimizedTag & TagsFilterClosed> TagUtils.Tag(option.osmTags).optimize()
return condition.shadows(sourceTags);
}).map(option => {
if(!option.osmTags){
return option
}
let basetags = TagUtils.Tag(option.osmTags)
return {...option, osmTags: (<TagsFilter>TagUtils.removeKnownParts(basetags ,sourceTags)).asJson()}
})
const newOptions: FilterConfigOptionJson[] = filter.options
.filter((option) => {
if (!option.osmTags) {
return true
}
const condition = <OptimizedTag & TagsFilterClosed>(
TagUtils.Tag(option.osmTags).optimize()
)
return condition.shadows(sourceTags)
})
.map((option) => {
if (!option.osmTags) {
return option
}
let basetags = TagUtils.Tag(option.osmTags)
return {
...option,
osmTags: (<TagsFilter>TagUtils.removeKnownParts(basetags, sourceTags)).asJson(),
}
})
const countAfter = newOptions.length
if(countAfter !== countBefore){
context.enters("filter", filter.id ).info("Pruned "+(countBefore-countAfter)+" options away from filter (out of "+countBefore+")")
if (countAfter !== countBefore) {
context
.enters("filter", filter.id)
.info(
"Pruned " +
(countBefore - countAfter) +
" options away from filter (out of " +
countBefore +
")"
)
}
return {...filter, options: newOptions, strict: undefined}
return { ...filter, options: newOptions, strict: undefined }
}
public convert(json: LayerConfigJson, context: ConversionContext): LayerConfigJson {
if(!Array.isArray(json.filter) || typeof json.source === "string"){
if (!Array.isArray(json.filter) || typeof json.source === "string") {
return json
}
if(!json.source["osmTags"]){
if (!json.source["osmTags"]) {
return json
}
const sourceTags = TagUtils.Tag(json.source["osmTags"])
return {...json, filter: json.filter?.map(obj => this.prune(sourceTags, <FilterConfigJson> obj, context))}
return {
...json,
filter: json.filter?.map((obj) =>
this.prune(sourceTags, <FilterConfigJson>obj, context)
),
}
}
}
export class ExpandFilter extends DesugaringStep<LayerConfigJson> {
@ -69,7 +95,7 @@ export class ExpandFilter extends DesugaringStep<LayerConfigJson> {
"If the string is formatted 'layername.filtername, it will be looked up into that layer instead. Note that pruning should still be done",
].join(" "),
["filter"],
"ExpandFilter",
"ExpandFilter"
)
this._state = state
}
@ -84,11 +110,11 @@ export class ExpandFilter extends DesugaringStep<LayerConfigJson> {
public static buildFilterFromTagRendering(
tr: TagRenderingConfigJson,
context: ConversionContext,
context: ConversionContext
): FilterConfigJson {
if (!(tr.mappings?.length >= 1)) {
context.err(
"Found a matching tagRendering to base a filter on, but this tagRendering does not contain any mappings",
"Found a matching tagRendering to base a filter on, but this tagRendering does not contain any mappings"
)
}
const qtr = <QuestionableTagRenderingConfigJson>tr
@ -103,7 +129,7 @@ export class ExpandFilter extends DesugaringStep<LayerConfigJson> {
if (qtr.multiAnswer && osmTags instanceof Tag) {
osmTags = new RegexTag(
osmTags.key,
new RegExp("^(.+;)?" + osmTags.value + "(;.+)$", "is"),
new RegExp("^(.+;)?" + osmTags.value + "(;.+)$", "is")
)
}
if (mapping.alsoShowIf) {
@ -161,7 +187,7 @@ export class ExpandFilter extends DesugaringStep<LayerConfigJson> {
if (matchingTr) {
const filter = ExpandFilter.buildFilterFromTagRendering(
matchingTr,
context.enters("filter", i),
context.enters("filter", i)
)
newFilters.push(filter)
continue
@ -175,7 +201,7 @@ export class ExpandFilter extends DesugaringStep<LayerConfigJson> {
const split = filter.split(".")
if (split.length > 2) {
context.err(
"invalid filter name: " + filter + ", expected `layername.filterid`",
"invalid filter name: " + filter + ", expected `layername.filterid`"
)
}
const layer = this._state.sharedLayers.get(split[0])
@ -184,7 +210,7 @@ export class ExpandFilter extends DesugaringStep<LayerConfigJson> {
}
const expectedId = split[1]
const expandedFilter = (<(FilterConfigJson | string)[]>layer.filter).find(
(f) => typeof f !== "string" && f.id === expectedId,
(f) => typeof f !== "string" && f.id === expectedId
)
if (expandedFilter === undefined) {
context.err("Did not find filter with name " + filter)
@ -199,15 +225,15 @@ export class ExpandFilter extends DesugaringStep<LayerConfigJson> {
const suggestions = Utils.sortedByLevenshteinDistance(
filter,
Array.from(ExpandFilter.predefinedFilters.keys()),
(t) => t,
(t) => t
)
context
.enter(filter)
.err(
"While searching for predefined filter " +
filter +
": this filter is not found. Perhaps you meant one of: " +
suggestions,
filter +
": this filter is not found. Perhaps you meant one of: " +
suggestions
)
}
newFilters.push(found)

View file

@ -11,9 +11,9 @@ export class ExpandTagRendering extends Conversion<
| string
| TagRenderingConfigJson
| {
builtin: string | string[]
override: any
},
builtin: string | string[]
override: any
},
TagRenderingConfigJson[]
> {
private readonly _state: DesugaringContext
@ -35,12 +35,12 @@ export class ExpandTagRendering extends Conversion<
noHardcodedStrings?: false | boolean
// If set, a question will be added to the 'sharedTagRenderings'. Should only be used for 'questions.json'
addToContext?: false | boolean
},
}
) {
super(
"Converts a tagRenderingSpec into the full tagRendering, e.g. by substituting the tagRendering by the shared-question and reusing the builtins",
[],
"ExpandTagRendering",
"ExpandTagRendering"
)
this._state = state
this._self = self
@ -60,10 +60,11 @@ export class ExpandTagRendering extends Conversion<
public convert(
spec: string | any,
ctx: ConversionContext,
ctx: ConversionContext
): QuestionableTagRenderingConfigJson[] {
const trs = this.convertOnce(spec, ctx)
?.map(tr => this.pruneMappings<TagRenderingConfigJson & { id: string }>(tr, ctx))
const trs = this.convertOnce(spec, ctx)?.map((tr) =>
this.pruneMappings<TagRenderingConfigJson & { id: string }>(tr, ctx)
)
if (!Array.isArray(trs)) {
ctx.err("Result of lookup for " + spec + " is not iterable; got " + trs)
return undefined
@ -71,8 +72,9 @@ export class ExpandTagRendering extends Conversion<
const result = []
for (const tr of trs) {
if (typeof tr === "string" || tr["builtin"] !== undefined) {
const stable = this.convert(tr, ctx.inOperation("recursive_resolve"))
.map(tr => this.pruneMappings(tr, ctx))
const stable = this.convert(tr, ctx.inOperation("recursive_resolve")).map((tr) =>
this.pruneMappings(tr, ctx)
)
result.push(...stable)
if (this._options?.addToContext) {
for (const tr of stable) {
@ -90,39 +92,47 @@ export class ExpandTagRendering extends Conversion<
return result
}
private pruneMappings<T extends (TagRenderingConfigJson & {
id: string
})>(tagRendering: T, ctx: ConversionContext): T {
private pruneMappings<
T extends TagRenderingConfigJson & {
id: string
}
>(tagRendering: T, ctx: ConversionContext): T {
if (!tagRendering["strict"]) {
return tagRendering
}
if(!this._self.source["osmTags"]){
if (!this._self.source["osmTags"]) {
return tagRendering
}
ctx.inOperation("expandTagRendering:pruning").enters(tagRendering.id)
.info(`PRUNING! Tagrendering to prune: ${tagRendering.id} in the context of layer ${this._self.id} Sourcetags: ${this._self.source["osmTags"]}`)
ctx.inOperation("expandTagRendering:pruning")
.enters(tagRendering.id)
.info(
`PRUNING! Tagrendering to prune: ${tagRendering.id} in the context of layer ${this._self.id} Sourcetags: ${this._self.source["osmTags"]}`
)
const before = tagRendering.mappings?.length ?? 0
const alwaysTags = TagUtils.Tag(this._self.source["osmTags"])
const newMappings = tagRendering.mappings?.filter(mapping => {
const condition = TagUtils.Tag(mapping.if)
return condition.shadows(alwaysTags);
}).map(mapping => {
const newIf = TagUtils.removeKnownParts(
TagUtils.Tag(mapping.if), alwaysTags)
if (typeof newIf === "boolean") {
throw "Invalid removeKnownParts"
}
return {
...mapping,
if: newIf.asJson(),
}
})
const newMappings = tagRendering.mappings
?.filter((mapping) => {
const condition = TagUtils.Tag(mapping.if)
return condition.shadows(alwaysTags)
})
.map((mapping) => {
const newIf = TagUtils.removeKnownParts(TagUtils.Tag(mapping.if), alwaysTags)
if (typeof newIf === "boolean") {
throw "Invalid removeKnownParts"
}
return {
...mapping,
if: newIf.asJson(),
}
})
const after = newMappings?.length ?? 0
if (before - after > 0) {
ctx.info(`Pruned mappings for ${tagRendering.id}, from ${before} to ${after} (removed ${before - after})`)
ctx.info(
`Pruned mappings for ${tagRendering.id}, from ${before} to ${after} (removed ${
before - after
})`
)
}
const tr = {
...tagRendering,
@ -132,7 +142,10 @@ export class ExpandTagRendering extends Conversion<
return tr
}
private lookup(name: string, ctx: ConversionContext): (TagRenderingConfigJson & { id: string })[] | undefined {
private lookup(
name: string,
ctx: ConversionContext
): (TagRenderingConfigJson & { id: string })[] | undefined {
const direct = this.directLookup(name)
if (direct === undefined) {
@ -202,9 +215,11 @@ export class ExpandTagRendering extends Conversion<
matchingTrs = layerTrs.filter((tr) => tr["id"] === id || tr["labels"]?.indexOf(id) >= 0)
}
const contextWriter = new AddContextToTranslations<TagRenderingConfigJson & { id: string }>("layers:")
const contextWriter = new AddContextToTranslations<TagRenderingConfigJson & { id: string }>(
"layers:"
)
for (let i = 0; i < matchingTrs.length; i++) {
let found: (TagRenderingConfigJson & { id: string }) = Utils.Clone(matchingTrs[i])
let found: TagRenderingConfigJson & { id: string } = Utils.Clone(matchingTrs[i])
if (this._options?.applyCondition) {
// The matched tagRenderings are 'stolen' from another layer. This means that they must match the layer condition before being shown
if (typeof layer.source !== "string") {
@ -220,8 +235,8 @@ export class ExpandTagRendering extends Conversion<
found,
ConversionContext.construct(
[layer.id, "tagRenderings", found["id"]],
["AddContextToTranslations"],
),
["AddContextToTranslations"]
)
)
matchingTrs[i] = found
}
@ -232,7 +247,10 @@ export class ExpandTagRendering extends Conversion<
return undefined
}
private convertOnce(tr: string | any, ctx: ConversionContext): (TagRenderingConfigJson & { id: string })[] {
private convertOnce(
tr: string | any,
ctx: ConversionContext
): (TagRenderingConfigJson & { id: string })[] {
const state = this._state
if (typeof tr === "string") {
@ -250,17 +268,17 @@ export class ExpandTagRendering extends Conversion<
ctx.warn(
`A literal rendering was detected: ${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) {
ctx.err(
"Detected an invocation to a builtin tagRendering, but this tagrendering was not found: " +
tr +
" \n Did you perhaps forget to add the layer as prefix, such as `icons." +
tr +
"`? ",
tr +
" \n Did you perhaps forget to add the layer as prefix, such as `icons." +
tr +
"`? "
)
}
@ -293,9 +311,9 @@ export class ExpandTagRendering extends Conversion<
}
ctx.err(
"An object calling a builtin can only have keys `builtin` or `override`, but a key with name `" +
key +
"` was found. This won't be picked up! The full object is: " +
JSON.stringify(tr),
key +
"` was found. This won't be picked up! The full object is: " +
JSON.stringify(tr)
)
}
@ -317,39 +335,39 @@ export class ExpandTagRendering extends Conversion<
const candidates = Utils.sortedByLevenshteinDistance(
layerName,
Utils.NoNull(Array.from(state.sharedLayers.keys())),
(s) => s,
(s) => s
)
if (state.sharedLayers.size === 0) {
ctx.warn(
"BOOTSTRAPPING. Rerun generate layeroverview. While reusing tagrendering: " +
name +
": layer " +
layerName +
" not found for now, but ignoring as this is a bootstrapping run. ",
name +
": layer " +
layerName +
" not found for now, but ignoring as this is a bootstrapping run. "
)
} else {
ctx.err(
": While reusing tagrendering: " +
name +
": layer " +
layerName +
" not found. Maybe you meant one of " +
candidates.slice(0, 3).join(", "),
name +
": layer " +
layerName +
" not found. Maybe you meant one of " +
candidates.slice(0, 3).join(", ")
)
}
continue
}
candidates = Utils.NoNull(layer.tagRenderings.map((tr) => tr["id"])).map(
(id) => layerName + "." + id,
(id) => layerName + "." + id
)
}
candidates = Utils.sortedByLevenshteinDistance(name, candidates, (i) => i)
ctx.err(
"The tagRendering with identifier " +
name +
" was not found.\n\tDid you mean one of " +
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",
name +
" was not found.\n\tDid you mean one of " +
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"
)
continue
}

View file

@ -1,6 +1,18 @@
import { Concat, DesugaringContext, DesugaringStep, Each, FirstOf, Fuse, On, SetDefault } from "./Conversion"
import {
Concat,
DesugaringContext,
DesugaringStep,
Each,
FirstOf,
Fuse,
On,
SetDefault,
} from "./Conversion"
import { LayerConfigJson } from "../Json/LayerConfigJson"
import { MinimalTagRenderingConfigJson, TagRenderingConfigJson } from "../Json/TagRenderingConfigJson"
import {
MinimalTagRenderingConfigJson,
TagRenderingConfigJson,
} from "../Json/TagRenderingConfigJson"
import { Utils } from "../../../Utils"
import RewritableConfigJson from "../Json/RewritableConfigJson"
import SpecialVisualizations from "../../../UI/SpecialVisualizations"
@ -695,7 +707,7 @@ class PreparePointRendering extends Fuse<PointRenderingConfigJson> {
constructor(state: DesugaringContext, layer: LayerConfigJson) {
super(
"Prepares point renderings by expanding 'icon' and 'iconBadges'." +
" A tagRendering from the host tagRenderings will be substituted in",
" A tagRendering from the host tagRenderings will be substituted in",
new On(
"marker",
new Each(
@ -751,19 +763,21 @@ class ExpandMarkerRenderings extends DesugaringStep<IconConfigJson> {
}
convert(json: IconConfigJson, context: ConversionContext): IconConfigJson {
const expander = new ExpandTagRendering(this._state, this._layer, {applyCondition: false})
const expander = new ExpandTagRendering(this._state, this._layer, { applyCondition: false })
const result: IconConfigJson = { icon: undefined, color: undefined }
if (json.icon && json.icon["builtin"]) {
result.icon = <MinimalTagRenderingConfigJson>(
expander.convert(<any>json.icon, context.enter("icon"))[0]
) ?? json.icon
result.icon =
<MinimalTagRenderingConfigJson>(
expander.convert(<any>json.icon, context.enter("icon"))[0]
) ?? json.icon
} else {
result.icon = json.icon
}
if (json.color && json.color["builtin"]) {
result.color = <MinimalTagRenderingConfigJson>(
expander.convert(<any>json.color, context.enter("color"))[0]
) ?? json.color
result.color =
<MinimalTagRenderingConfigJson>(
expander.convert(<any>json.color, context.enter("color"))[0]
) ?? json.color
} else {
result.color = json.color
}
@ -1002,7 +1016,7 @@ export class PrepareLayer extends Fuse<LayerConfigJson> {
}
convert(json: LayerConfigJson, context: ConversionContext): LayerConfigJson {
if(json === undefined || json === null){
if (json === undefined || json === null) {
throw "Error: prepareLayer got null"
}
return super.convert(json, context)

View file

@ -112,7 +112,7 @@ export class DoesImageExist extends DesugaringStep<string> {
if (!this._knownImagePaths.has(image)) {
if (this.doesPathExist === undefined || image.indexOf("nsi/logos/") >= 0) {
// pass
} else if (!this.doesPathExist(image) ) {
} else if (!this.doesPathExist(image)) {
context.err(
`Image with path ${image} does not exist.\n Check for typo's and missing directories in the path. `
)