From fbc77f29b502c25519902e9eacf896824d0938d9 Mon Sep 17 00:00:00 2001 From: "marcelo.fabri" Date: Wed, 21 Aug 2024 01:25:15 -0700 Subject: [PATCH] Trigger key_missing_from_base for iOS Fixes #57 --- .../parseAndValidateXCStrings.swift | 14 +++++++- .../ValidateStringsTests.swift | 35 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/Sources/LocheckLogic/Validators/parseAndValidateXCStrings.swift b/Sources/LocheckLogic/Validators/parseAndValidateXCStrings.swift index 212eb8d..8a21066 100644 --- a/Sources/LocheckLogic/Validators/parseAndValidateXCStrings.swift +++ b/Sources/LocheckLogic/Validators/parseAndValidateXCStrings.swift @@ -83,10 +83,22 @@ func validateStrings( path: baseString.path, lineNumber: baseString.line) } + + let baseStringsKeys = Set(baseStrings.map(\.key)) + + var missingInBaseKeys: Set = [] + for translationString in translationStrings where !baseStringsKeys.contains(translationString.key) { + problemReporter.report( + KeyMissingFromBase( + key: translationString.key), + path: translationString.path, + lineNumber: translationString.line) + missingInBaseKeys.insert(translationString.key) + } // MARK: Validate arguments - for translationString in translationStrings { + for translationString in translationStrings where !missingInBaseKeys.contains(translationString.key) { let baseArgumentPositions = Set(translationString.base.arguments.map(\.position)) let translationArgumentPositions = Set(translationString.translation.arguments.map(\.position)) diff --git a/Tests/LocheckLogicTests/ValidateStringsTests.swift b/Tests/LocheckLogicTests/ValidateStringsTests.swift index df4f9c7..2409226 100644 --- a/Tests/LocheckLogicTests/ValidateStringsTests.swift +++ b/Tests/LocheckLogicTests/ValidateStringsTests.swift @@ -181,4 +181,39 @@ class ValidateStringsTests: XCTestCase { "abc:1: warning: 'missing' is missing from trnsltn (key_missing_from_translation)", ]) } + + func testExtraKeyInTranslation() { + let problemReporter = ProblemReporter(log: false) + validateStrings( + baseStrings: [ + LocalizedStringPair( + string: "\"present\" = \"present\";", + path: "abc", + line: 0, + basePath: "", + baseLineFallback: 0)!, + ], + translationStrings: [ + LocalizedStringPair( + string: "\"present\" = \"tneserp\";", + path: "def", + line: 0, + basePath: "", + baseLineFallback: 0)!, + LocalizedStringPair( + string: "\"extra\" = \"%s\";", + path: "abc", + line: 1, + basePath: "", + baseLineFallback: 0)!, + ], + baseLanguageName: "en", + translationLanguageName: "trnsltn", + problemReporter: problemReporter) + + XCTAssertEqual( + problemReporter.problems.map(\.messageForXcode), [ + "abc:1: warning: 'extra' is missing from the base translation (key_missing_from_base)", + ]) + } }