Skip to content

Commit

Permalink
Merge pull request #37 from dral3x/feature/match-string-localized
Browse files Browse the repository at this point in the history
add match for String(localized:)
  • Loading branch information
dral3x authored Sep 19, 2024
2 parents 0723a67 + bf56d44 commit 8600b81
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 5 deletions.
34 changes: 33 additions & 1 deletion Sources/StringsLintFramework/Parsers/SwiftParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public struct SwiftParser: LocalizableParser {
}

let foundationPattern: String
let stringPattern: String
let swiftUIImplicitPattern: String
let swiftUIImplicitEnabled: Bool
let swiftUIExplicitPattern: String
Expand All @@ -40,6 +41,7 @@ public struct SwiftParser: LocalizableParser {

public init(macros: [String], customRegex: CustomRegex?, swiftUIImplicitEnabled: Bool) {
self.foundationPattern = "(\(macros.joined(separator: "|")))\\(\"(?<key>[^\"]+)\", (tableName: \"(?<table>[^\"]+)\", )?(value: \"(?<value>[^\"]+)\", )?(comment: \"([^\"]*)\")\\)"
self.stringPattern = "String\\(localized: \"(?<key>[^\"]+)\"(, defaultValue: \"(?<value>[^\"]+)\")?(, table: \"(?<table>[^\"]+)\")?(, comment: \"([^\"]*)\")?\\)"
self.swiftUIExplicitPattern = "Text\\((LocalizedStringKey\\()?\"([^\"]+)\"\\)?, tableName: \"([^\"]+)\"(, comment: \"([^\"]*)\")?\\)"
self.swiftUIImplicitPattern = "(Text|Button|LocalizedStringKey)\\(\"([^\"]+)\"\\)"
self.ignoreThisPattern = "//stringslint:ignore"
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
</BuildableReference>
</TestableReference>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
Expand All @@ -62,8 +60,6 @@
ReferencedContainer = "container:StringsLint.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
42 changes: 42 additions & 0 deletions Tests/StringsLintFrameworkTests/Parsers/SwiftParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,46 @@ Text("abc")

XCTAssertEqual(results.count, 0)
}

func testStringLocalized() throws {

let content = """
String(localized: "abc")
String(localized: "def", defaultValue: "DEF")
String(localized: "ghi", table: "AnotherTable")
String(localized: "jkl", comment: "mmm")
"""

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

let parser = SwiftParser(macros: [], customRegex: nil, swiftUIImplicitEnabled: false)

let results = try parser.parse(file: file)

XCTAssertEqual(results.count, 4)

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

XCTAssertEqual(results[1].key, "def")
XCTAssertEqual(results[1].table, "Localizable")
XCTAssertEqual(results[1].value, "DEF")
XCTAssertEqual(results[1].locale, .none)
XCTAssertEqual(results[1].location, Location(file: file, line: 2))

XCTAssertEqual(results[2].key, "ghi")
XCTAssertEqual(results[2].value, nil)
XCTAssertEqual(results[2].table, "AnotherTable")
XCTAssertEqual(results[2].locale, .none)
XCTAssertEqual(results[2].location, Location(file: file, line: 3))

XCTAssertEqual(results[3].key, "jkl")
XCTAssertEqual(results[3].value, nil)
XCTAssertEqual(results[3].table, "Localizable")
XCTAssertEqual(results[3].locale, .none)
XCTAssertEqual(results[3].location, Location(file: file, line: 4))
}
}

0 comments on commit 8600b81

Please sign in to comment.