Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support specifying a custom regex #33

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Sources/StringsLintFramework/Models/RegexConfiguration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// CustomRegex.swift
//
//
// Created by Jalal Awqati on 18/02/2024.
//

import Foundation

public struct CustomRegex {

private enum Key: String {
case pattern
case matchIndex = "match_index"
}

let pattern: String
let matchIndex: Int

init(pattern: String, matchIndex: Int) {
self.pattern = pattern
self.matchIndex = matchIndex
}

init?(config: Any?) {
if let config = config,
let pattern = defaultDictionaryValue(config, for: Key.pattern.rawValue) as? String,
let matchIndex = defaultDictionaryValue(config, for: Key.matchIndex.rawValue) as? Int {
self.pattern = pattern
self.matchIndex = matchIndex
} else {
return nil
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@ public struct SwiftParserConfiguration {

private enum Key: String {
case macros = "macros"
case customRegex = "regex"
}

public var macros: [String] = [
"NSLocalizedString",
"CFLocalizedString"
]


public var customRegex: CustomRegex?

public mutating func apply(_ configuration: Any) throws {

guard let configuration = configuration as? [String: Any] else {
throw ConfigurationError.unknownConfiguration
}

self.macros += defaultStringArray(configuration[Key.macros.rawValue])
self.customRegex = CustomRegex(config: configuration[Key.customRegex.rawValue])
}

}
30 changes: 26 additions & 4 deletions Sources/StringsLintFramework/Parsers/SwiftParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public struct SwiftParser: LocalizableParser {
let uiKitPattern: String
let swiftUIImplicitPattern: String
let swiftUIExplicitPattern: String
let customRegex: CustomRegex?
let ignoreThisPattern: String

public var supportedFileExtentions: [String] {
Expand All @@ -24,7 +25,7 @@ public struct SwiftParser: LocalizableParser {

public init() {
let config = SwiftParserConfiguration()
self.init(macros: config.macros)
self.init(macros: config.macros, customRegex: config.customRegex)
}

public init(configuration: Any) throws {
Expand All @@ -33,16 +34,17 @@ public struct SwiftParser: LocalizableParser {
try config.apply(defaultDictionaryValue(configuration, for: SwiftParser.self.identifier))
} catch {}

self.init(macros: config.macros)
self.init(macros: config.macros, customRegex: config.customRegex)
}

public init(macros: [String]) {
public init(macros: [String], customRegex: CustomRegex? = nil) {
self.uiKitPattern = "(\(macros.joined(separator: "|")))\\(\"([^\"]+)\", (tableName: \"([^\"]+)\", )?(comment: \"([^\"]*)\")\\)"
self.swiftUIExplicitPattern = "Text\\((LocalizedStringKey\\()?\"([^\"]+)\"\\)?, tableName: \"([^\"]+)\"(, comment: \"([^\"]*)\")?\\)"
self.swiftUIImplicitPattern = "(Text|Button|LocalizedStringKey)\\(\"([^\"]+)\"\\)"
self.ignoreThisPattern = "//stringslint:ignore"
self.customRegex = customRegex
}

public func support(file: File) -> Bool {
return file.name.hasSuffix(".swift")
}
Expand Down Expand Up @@ -141,6 +143,26 @@ public struct SwiftParser: LocalizableParser {
}
}

if let customRegex = customRegex {
do {
let regex = try NSRegularExpression(pattern: customRegex.pattern)
let results = regex.matches(in: text, options: [ .reportCompletion ], range: NSRange(text.startIndex..., in: text))

for result in results {

var key = ""
if result.numberOfRanges > customRegex.matchIndex {
key = String(text[Range(result.range(at: customRegex.matchIndex), in: text)!])
}

strings.append(LocalizedString(key: key, table: "Localizable", locale: .none, location: location))

}
} catch let error {
print("invalid regex: \(error.localizedDescription)")
}
}

return strings
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class SwiftParserConfigurationTests: ConfigurationTestCase {
let content = """
macros:
- ABCString
regex:
pattern: ^\"([^\"]+)\".localized$
match_index: 2
"""

let data = try self.creareConfigFileAsDictionary(with: content)
Expand All @@ -24,6 +27,8 @@ macros:

XCTAssertEqual(configuration.macros.count, 3)
XCTAssertEqual(configuration.macros, [ "NSLocalizedString", "CFLocalizedString", "ABCString" ])
XCTAssertEqual(configuration.customRegex?.pattern, "^\"([^\"]+)\".localized$")
XCTAssertEqual(configuration.customRegex?.matchIndex, 2)
}

}
22 changes: 21 additions & 1 deletion Tests/StringsLintFrameworkTests/Parsers/SwiftParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,27 @@ ABCLocalizedString(\"abc\", tableName: \"Extras\", comment: \"blabla\")
XCTAssertEqual(results[1].locale, .none)
XCTAssertEqual(results[1].location, Location(file: file, line: 2))
}


func testParseCustomRegex() throws {

let content = """
\"blabla\".localized
"""

let file = try self.createTempFile("test1.swift", with: content)

let customRegex = CustomRegex(pattern: "^\"([^\"]+)\".localized$", matchIndex: 1)
let parser = SwiftParser(macros: [], customRegex: customRegex)
let results = try parser.parse(file: file)

XCTAssertEqual(results.count, 1)

XCTAssertEqual(results[0].key, "blabla")
XCTAssertEqual(results[0].table, "Localizable")
XCTAssertEqual(results[0].locale, .none)
XCTAssertEqual(results[0].location, Location(file: file, line: 1))
}

func testSwiftUITextWithImplicitString() throws {

let content = """
Expand Down