From bf56d444eb4085d1e4bbf4e05ede89f776952792 Mon Sep 17 00:00:00 2001 From: Alessandro Calzavara Date: Thu, 19 Sep 2024 18:21:50 +0200 Subject: [PATCH] add match for String(localized:) --- .../Parsers/SwiftParser.swift | 34 ++++++++++++++- .../xcschemes/stringslint.xcscheme | 4 -- .../Parsers/SwiftParserTests.swift | 42 +++++++++++++++++++ 3 files changed, 75 insertions(+), 5 deletions(-) diff --git a/Sources/StringsLintFramework/Parsers/SwiftParser.swift b/Sources/StringsLintFramework/Parsers/SwiftParser.swift index 0280bb8..ea1827b 100644 --- a/Sources/StringsLintFramework/Parsers/SwiftParser.swift +++ b/Sources/StringsLintFramework/Parsers/SwiftParser.swift @@ -14,6 +14,7 @@ public struct SwiftParser: LocalizableParser { } let foundationPattern: String + let stringPattern: String let swiftUIImplicitPattern: String let swiftUIImplicitEnabled: Bool let swiftUIExplicitPattern: String @@ -40,6 +41,7 @@ public struct SwiftParser: LocalizableParser { public init(macros: [String], customRegex: CustomRegex?, swiftUIImplicitEnabled: Bool) { self.foundationPattern = "(\(macros.joined(separator: "|")))\\(\"(?[^\"]+)\", (tableName: \"(?[^\"]+)\", )?(value: \"(?[^\"]+)\", )?(comment: \"([^\"]*)\")\\)" + self.stringPattern = "String\\(localized: \"(?[^\"]+)\"(, defaultValue: \"(?[^\"]+)\")?(, table: \"(?
[^\"]+)\")?(, comment: \"([^\"]*)\")?\\)" self.swiftUIExplicitPattern = "Text\\((LocalizedStringKey\\()?\"([^\"]+)\"\\)?, tableName: \"([^\"]+)\"(, comment: \"([^\"]*)\")?\\)" self.swiftUIImplicitPattern = "(Text|Button|LocalizedStringKey)\\(\"([^\"]+)\"\\)" self.ignoreThisPattern = "//stringslint:ignore" @@ -79,7 +81,7 @@ public struct SwiftParser: LocalizableParser { var strings = [LocalizedString]() // Foundation - + do { let regex = try NSRegularExpression(pattern: self.foundationPattern) let results = regex.matches(in: text, options: [ .reportCompletion ], range: NSRange(text.startIndex..., in: text)) @@ -111,6 +113,36 @@ public struct SwiftParser: LocalizableParser { print("invalid regex: \(error.localizedDescription)") } + do { + let regex = try NSRegularExpression(pattern: self.stringPattern) + let results = regex.matches(in: text, options: [ .reportCompletion ], range: NSRange(text.startIndex..., in: text)) + for result in results { + + let keyRange = result.range(withName: "key") + let tableRange = result.range(withName: "table") + let valueRange = result.range(withName: "value") + + var key = "" + if keyRange.location != NSNotFound { + key = String(text[Range(keyRange, in: text)!]) + } + + var table = "Localizable" + if tableRange.location != NSNotFound { + table = String(text[Range(tableRange, in: text)!]) + } + + var value: String? = nil + if valueRange.location != NSNotFound { + value = String(text[Range(valueRange, in: text)!]) + } + + strings.append(LocalizedString(key: key, table: table, value: value, locale: .none, location: location)) + } + } catch let error { + print("invalid regex: \(error.localizedDescription)") + } + // Swift UI - explicit do { diff --git a/StringsLint.xcodeproj/xcshareddata/xcschemes/stringslint.xcscheme b/StringsLint.xcodeproj/xcshareddata/xcschemes/stringslint.xcscheme index f33605d..182c644 100644 --- a/StringsLint.xcodeproj/xcshareddata/xcschemes/stringslint.xcscheme +++ b/StringsLint.xcodeproj/xcshareddata/xcschemes/stringslint.xcscheme @@ -39,8 +39,6 @@ - - - -