forked from MapComplete/MapComplete
TYpes: better type 'On'
This commit is contained in:
parent
f5888dee56
commit
cb0cb710a9
3 changed files with 11 additions and 10 deletions
|
@ -166,11 +166,11 @@ export class Each<X, Y> extends Conversion<X[], Y[]> {
|
|||
}
|
||||
}
|
||||
|
||||
export class On<P, T> extends DesugaringStep<T> {
|
||||
private readonly key: string
|
||||
export class On<S extends string, P, T extends Record<S, P>> extends DesugaringStep<T> {
|
||||
private readonly key: S
|
||||
private readonly step: (t: T) => Conversion<P, P>
|
||||
|
||||
constructor(key: string, step: Conversion<P, P> | ((t: T) => Conversion<P, P>)) {
|
||||
constructor(key: S, step: Conversion<P, P> | ((t: T) => Conversion<P, P>)) {
|
||||
super(`On(${key}, ${step.name})`, "Applies " + step.name + " onto property `" + key + "`")
|
||||
if (typeof step === "function") {
|
||||
this.step = step
|
||||
|
@ -181,7 +181,7 @@ export class On<P, T> extends DesugaringStep<T> {
|
|||
}
|
||||
|
||||
convert(json: T, context: ConversionContext): T {
|
||||
const key = this.key
|
||||
const key: S = this.key
|
||||
const value: P = json?.[key]
|
||||
if (value === undefined || value === null) {
|
||||
return json
|
||||
|
@ -189,7 +189,8 @@ export class On<P, T> extends DesugaringStep<T> {
|
|||
|
||||
json = { ...json }
|
||||
const step = this.step(json)
|
||||
json[key] = step.convert(value, context.enter(key).inOperation("on[" + key + "]"))
|
||||
const converted = step.convert(value, context.enter(key).inOperation("on[" + key + "]"))
|
||||
json[key] = <T[S]> converted
|
||||
return json
|
||||
}
|
||||
}
|
||||
|
|
|
@ -329,15 +329,15 @@ export class PrevalidateLayer extends DesugaringStep<LayerConfigJson> {
|
|||
}
|
||||
}
|
||||
|
||||
if (json.filter) {
|
||||
new On("filter", new Each(new ValidateFilter())).convert(json, context)
|
||||
if (json.filter && Array.isArray(json.filter)) {
|
||||
new On("filter", new Each(new ValidateFilter())).convert(<any> json, context)
|
||||
}
|
||||
|
||||
if (json.tagRenderings !== undefined) {
|
||||
new On(
|
||||
"tagRenderings",
|
||||
new Each(new ValidateTagRenderings(json, this._doesImageExist))
|
||||
).convert(json, context)
|
||||
).convert(<any> json, context)
|
||||
}
|
||||
|
||||
if (json.pointRendering !== null && json.pointRendering !== undefined) {
|
||||
|
|
|
@ -901,12 +901,12 @@ export class ValidateLayer extends Conversion<
|
|||
}
|
||||
}
|
||||
|
||||
export class ValidateFilter extends DesugaringStep<FilterConfigJson> {
|
||||
export class ValidateFilter extends DesugaringStep<string | FilterConfigJson> {
|
||||
constructor() {
|
||||
super("ValidateFilter", "Detect common errors in the filters")
|
||||
}
|
||||
|
||||
convert(filter: FilterConfigJson, context: ConversionContext): FilterConfigJson {
|
||||
convert(filter: string | FilterConfigJson, context: ConversionContext): string | FilterConfigJson {
|
||||
if (typeof filter === "string") {
|
||||
// Calling another filter, we skip
|
||||
return filter
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue