fix: harden model catalog parsing

main
Peter Steinberger 2025-12-06 05:21:07 +01:00
parent 5e6af3d732
commit 46d55a8ada
1 changed files with 12 additions and 8 deletions

View File

@ -1096,15 +1096,19 @@ enum ModelCatalogLoader {
} }
private static func sanitize(source: String) -> String { private static func sanitize(source: String) -> String {
var text = source guard let exportRange = source.range(of: "export const MODELS"),
text = text.replacingOccurrences(of: #"(?m)^import[^\n]*\n"#, with: "", options: .regularExpression) let firstBrace = source[exportRange.upperBound...].firstIndex(of: "{"),
text = text.replacingOccurrences( let lastBrace = source.lastIndex(of: "}")
of: #"export\s+const\s+MODELS"#, else {
with: "var MODELS", return "var MODELS = {}"
}
var body = String(source[firstBrace...lastBrace])
body = body.replacingOccurrences(
of: #"satisfies\s+[A-Za-z0-9_<>.,\-\s]+"#,
with: "",
options: .regularExpression) options: .regularExpression)
text = text.replacingOccurrences(of: #"satisfies\s+Model<[^>]+>"#, with: "", options: .regularExpression) body = body.replacingOccurrences(of: #"as\s+[A-Za-z0-9_<>.,\-\s]+"#, with: "", options: .regularExpression)
text = text.replacingOccurrences(of: #"as\s+Model<[^>]+>"#, with: "", options: .regularExpression) return "var MODELS = \(body);"
return text
} }
} }