Add wrong default import from json files to code quality checks, fix those imports

This commit is contained in:
Pieter Vander Vennet 2023-02-08 01:14:21 +01:00
parent 71c815d37d
commit ce44f34bf3
42 changed files with 167 additions and 148 deletions

View file

@ -1,21 +1,20 @@
import { Conversion, DesugaringStep } from "./Conversion"
import { LayoutConfigJson } from "../Json/LayoutConfigJson"
import { Utils } from "../../../Utils"
import * as metapaths from "../../../assets/layoutconfigmeta.json"
import * as tagrenderingmetapaths from "../../../assets/questionabletagrenderingconfigmeta.json"
import metapaths from "../../../assets/layoutconfigmeta.json"
import tagrenderingmetapaths from "../../../assets/questionabletagrenderingconfigmeta.json"
import Translations from "../../../UI/i18n/Translations"
export class ExtractImages extends Conversion<LayoutConfigJson, string[]> {
private _isOfficial: boolean
private _sharedTagRenderings: Map<string, any>
private static readonly layoutMetaPaths = (metapaths["default"] ?? metapaths).filter(
private static readonly layoutMetaPaths = metapaths.filter(
(mp) =>
ExtractImages.mightBeTagRendering(mp) ||
ExtractImages.mightBeTagRendering(<any>mp) ||
(mp.typeHint !== undefined && (mp.typeHint === "image" || mp.typeHint === "icon"))
)
private static readonly tagRenderingMetaPaths =
tagrenderingmetapaths["default"] ?? tagrenderingmetapaths
private static readonly tagRenderingMetaPaths = tagrenderingmetapaths
constructor(isOfficial: boolean, sharedTagRenderings: Map<string, any>) {
super("Extract all images from a layoutConfig using the meta paths.", [], "ExctractImages")
@ -23,14 +22,16 @@ export class ExtractImages extends Conversion<LayoutConfigJson, string[]> {
this._sharedTagRenderings = sharedTagRenderings
}
public static mightBeTagRendering(metapath: { type: string | string[] }): boolean {
public static mightBeTagRendering(metapath: { type?: string | string[] }): boolean {
if (!Array.isArray(metapath.type)) {
return false
}
return metapath.type.some(
(t) =>
t["$ref"] == "#/definitions/TagRenderingConfigJson" ||
t["$ref"] == "#/definitions/QuestionableTagRenderingConfigJson"
return (
metapath.type?.some(
(t) =>
t["$ref"] == "#/definitions/TagRenderingConfigJson" ||
t["$ref"] == "#/definitions/QuestionableTagRenderingConfigJson"
) ?? false
)
}
@ -83,7 +84,7 @@ export class ExtractImages extends Conversion<LayoutConfigJson, string[]> {
const errors = []
const warnings = []
for (const metapath of ExtractImages.layoutMetaPaths) {
const mightBeTr = ExtractImages.mightBeTagRendering(metapath)
const mightBeTr = ExtractImages.mightBeTagRendering(<any>metapath)
const allRenderedValuesAreImages =
metapath.typeHint === "icon" || metapath.typeHint === "image"
const found = Utils.CollectPath(metapath.path, json)
@ -271,14 +272,11 @@ export class FixImages extends DesugaringStep<LayoutConfigJson> {
json = Utils.Clone(json)
let paths = metapaths["default"] ?? metapaths
let trpaths = tagrenderingmetapaths["default"] ?? tagrenderingmetapaths
for (const metapath of paths) {
for (const metapath of metapaths) {
if (metapath.typeHint !== "image" && metapath.typeHint !== "icon") {
continue
}
const mightBeTr = ExtractImages.mightBeTagRendering(metapath)
const mightBeTr = ExtractImages.mightBeTagRendering(<any>metapath)
Utils.WalkPath(metapath.path, json, (leaf, path) => {
if (typeof leaf === "string") {
return replaceString(leaf)
@ -287,7 +285,7 @@ export class FixImages extends DesugaringStep<LayoutConfigJson> {
if (mightBeTr) {
// We might have reached a tagRenderingConfig containing icons
// lets walk every rendered value and fix the images in there
for (const trpath of trpaths) {
for (const trpath of tagrenderingmetapaths) {
if (trpath.typeHint !== "rendered") {
continue
}

View file

@ -16,10 +16,10 @@ import RewritableConfigJson from "../Json/RewritableConfigJson"
import SpecialVisualizations from "../../../UI/SpecialVisualizations"
import Translations from "../../../UI/i18n/Translations"
import { Translation } from "../../../UI/i18n/Translation"
import * as tagrenderingconfigmeta from "../../../assets/tagrenderingconfigmeta.json"
import tagrenderingconfigmeta from "../../../assets/tagrenderingconfigmeta.json"
import { AddContextToTranslations } from "./AddContextToTranslations"
import FilterConfigJson from "../Json/FilterConfigJson"
import * as predifined_filters from "../../../assets/layers/filters/filters.json"
import predifined_filters from "../../../assets/layers/filters/filters.json"
class ExpandFilter extends DesugaringStep<LayerConfigJson> {
private static load_filters(): Map<string, FilterConfigJson> {
@ -730,8 +730,7 @@ export class RewriteSpecial extends DesugaringStep<TagRenderingConfigJson> {
} {
const errors = []
json = Utils.Clone(json)
const paths: { path: string[]; type?: any; typeHint?: string }[] =
tagrenderingconfigmeta["default"] ?? tagrenderingconfigmeta
const paths: { path: string[]; type?: any; typeHint?: string }[] = tagrenderingconfigmeta
for (const path of paths) {
if (path.typeHint !== "rendered") {
continue

View file

@ -9,11 +9,9 @@ import LayoutConfig from "../LayoutConfig"
import { TagRenderingConfigJson } from "../Json/TagRenderingConfigJson"
import { TagUtils } from "../../../Logic/Tags/TagUtils"
import { ExtractImages } from "./FixImages"
import ScriptUtils from "../../../scripts/ScriptUtils"
import { And } from "../../../Logic/Tags/And"
import Translations from "../../../UI/i18n/Translations"
import Svg from "../../../Svg"
import { QuestionableTagRenderingConfigJson } from "../Json/QuestionableTagRenderingConfigJson"
import FilterConfigJson from "../Json/FilterConfigJson"
import DeleteConfig from "../DeleteConfig"
@ -365,7 +363,7 @@ export class PrevalidateTheme extends Fuse<LayoutConfigJson> {
}
}
export class DetectShadowedMappings extends DesugaringStep<QuestionableTagRenderingConfigJson> {
export class DetectShadowedMappings extends DesugaringStep<TagRenderingConfigJson> {
private readonly _calculatedTagNames: string[]
constructor(layerConfig?: LayerConfigJson) {
@ -425,9 +423,9 @@ export class DetectShadowedMappings extends DesugaringStep<QuestionableTagRender
* r.errors[0].indexOf("The mapping key=value&x=y is fully matched by a previous mapping (namely 0)") >= 0 // => true
*/
convert(
json: QuestionableTagRenderingConfigJson,
json: TagRenderingConfigJson,
context: string
): { result: QuestionableTagRenderingConfigJson; errors?: string[]; warnings?: string[] } {
): { result: TagRenderingConfigJson; errors?: string[]; warnings?: string[] } {
const errors = []
const warnings = []
if (json.mappings === undefined || json.mappings.length === 0) {
@ -441,12 +439,9 @@ export class DetectShadowedMappings extends DesugaringStep<QuestionableTagRender
const parsedConditions = json.mappings.map((m, i) => {
const ctx = `${context}.mappings[${i}]`
const ifTags = TagUtils.Tag(m.if, ctx)
if (
m.hideInAnswer !== undefined &&
m.hideInAnswer !== false &&
m.hideInAnswer !== true
) {
let conditionTags = TagUtils.Tag(m.hideInAnswer)
const hideInAnswer = m["hideInAnswer"]
if (hideInAnswer !== undefined && hideInAnswer !== false && hideInAnswer !== true) {
let conditionTags = TagUtils.Tag(hideInAnswer)
// Merge the condition too!
return new And([conditionTags, ifTags])
}
@ -467,8 +462,8 @@ export class DetectShadowedMappings extends DesugaringStep<QuestionableTagRender
const doesMatch = parsedConditions[j].matchesProperties(properties)
if (
doesMatch &&
json.mappings[j].hideInAnswer === true &&
json.mappings[i].hideInAnswer !== true
json.mappings[j]["hideInAnswer"] === true &&
json.mappings[i]["hideInAnswer"] !== true
) {
warnings.push(
`At ${context}: Mapping ${i} is shadowed by mapping ${j}. However, mapping ${j} has 'hideInAnswer' set, which will result in a different rendering in question-mode.`
@ -623,7 +618,8 @@ export class ValidateTagRenderings extends Fuse<TagRenderingConfigJson> {
super(
"Various validation on tagRenderingConfigs",
new DetectShadowedMappings(layerConfig),
new DetectMappingsWithImages(doesImageExist)
new DetectMappingsWithImages(doesImageExist),
new MiscTagRenderingChecks()
)
}
}

View file

@ -26,7 +26,7 @@ export interface MappingConfigJson {
/**
* Size of the image
*/
class: "small" | "medium" | "large" | string
class?: "small" | "medium" | "large" | string
}
/**

View file

@ -125,7 +125,7 @@ export interface TagRenderingConfigJson {
* A hint to mapcomplete on how to render this icon within the mapping.
* This is translated to 'mapping-icon-<classtype>', so defining your own in combination with a custom CSS is possible (but discouraged)
*/
class: "small" | "medium" | "large" | string
class?: "small" | "medium" | "large" | string
}
}[]
}

View file

@ -7,7 +7,7 @@ import TilesourceConfig from "./TilesourceConfig"
import { ExtractImages } from "./Conversion/FixImages"
import ExtraLinkConfig from "./ExtraLinkConfig"
import { Utils } from "../../Utils"
import * as used_languages from "../../assets/generated/used_languages.json"
import used_languages from "../../assets/generated/used_languages.json"
export default class LayoutConfig {
public static readonly defaultSocialImage = "assets/SocialImage.png"
public readonly id: string