Skip to content

Commit

Permalink
Merge pull request #2672 from matthew-healy/matthew-healy/nslocalized…
Browse files Browse the repository at this point in the history
…string-require-bundle

Add NSLocalizedStringRequireBundleRule
  • Loading branch information
marcelofabri authored Apr 1, 2019
2 parents 9d0ac2d + 2894da1 commit 442a59d
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
[Cihat Gündüz](https://github.com/Dschee)
[#2294](https://github.com/realm/SwiftLint/issues/2294)

* Add `nslocalizedstring_require_bundle` rule to ensure calls to
`NSLocalizedString` specify the bundle where the strings file is located.
[Matthew Healy](https://github.com/matthew-healy)
[#2595](https://github.com/realm/SwiftLint/2595)

#### Bug Fixes

* `colon` rule now catches violations when declaring generic types with
Expand Down
55 changes: 55 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
* [No Grouping Extension](#no-grouping-extension)
* [Notification Center Detachment](#notification-center-detachment)
* [NSLocalizedString Key](#nslocalizedstring-key)
* [NSLocalizedString Require Bundle](#nslocalizedstring-require-bundle)
* [NSObject Prefer isEqual](#nsobject-prefer-isequal)
* [Number Separator](#number-separator)
* [Object Literal](#object-literal)
Expand Down Expand Up @@ -13549,6 +13550,60 @@ NSLocalizedString(↓"key_\(param)", comment: nil)



## NSLocalizedString Require Bundle

Identifier | Enabled by default | Supports autocorrection | Kind | Analyzer | Minimum Swift Compiler Version
--- | --- | --- | --- | --- | ---
`nslocalizedstring_require_bundle` | Disabled | No | lint | No | 3.0.0

Calls to NSLocalisedString should specify the bundle which contains the strings file.

### Examples

<details>
<summary>Non Triggering Examples</summary>

```swift
NSLocalizedString("someKey", bundle: .main, comment: "test")
```

```swift
NSLocalizedString("someKey", tableName: "a",
bundle: Bundle(for: A.self),
comment: "test")
```

```swift
NSLocalizedString("someKey", tableName: "xyz",
bundle: someBundle, value: "test"
comment: "test")
```

```swift
arbitraryFunctionCall("something")
```

</details>
<details>
<summary>Triggering Examples</summary>

```swift
↓NSLocalizedString("someKey", comment: "test")
```

```swift
↓NSLocalizedString("someKey", tableName: "a", comment: "test")
```

```swift
↓NSLocalizedString("someKey", tableName: "xyz",
value: "test", comment: "test")
```

</details>



## NSObject Prefer isEqual

Identifier | Enabled by default | Supports autocorrection | Kind | Analyzer | Minimum Swift Compiler Version
Expand Down
1 change: 1 addition & 0 deletions Source/SwiftLintFramework/Models/MasterRuleList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public let masterRuleList = RuleList(rules: [
MultilineParametersRule.self,
MultipleClosuresWithTrailingClosureRule.self,
NSLocalizedStringKeyRule.self,
NSLocalizedStringRequireBundleRule.self,
NSObjectPreferIsEqualRule.self,
NestingRule.self,
NimbleOperatorRule.self,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import SourceKittenFramework

public struct NSLocalizedStringRequireBundleRule: ASTRule, OptInRule, ConfigurationProviderRule, AutomaticTestableRule {
public var configuration = SeverityConfiguration(.warning)

public init() {}

public static let description = RuleDescription(
identifier: "nslocalizedstring_require_bundle",
name: "NSLocalizedString Require Bundle",
description: "Calls to NSLocalisedString should specify the bundle which contains the strings file.",
kind: .lint,
nonTriggeringExamples: [
"""
NSLocalizedString("someKey", bundle: .main, comment: "test")
""",
"""
NSLocalizedString("someKey", tableName: "a",
bundle: Bundle(for: A.self),
comment: "test")
""",
"""
NSLocalizedString("someKey", tableName: "xyz",
bundle: someBundle, value: "test"
comment: "test")
""",
"""
arbitraryFunctionCall("something")
"""
],
triggeringExamples: [
"""
↓NSLocalizedString("someKey", comment: "test")
""",
"""
↓NSLocalizedString("someKey", tableName: "a", comment: "test")
""",
"""
↓NSLocalizedString("someKey", tableName: "xyz",
value: "test", comment: "test")
"""
]
)

public func validate(file: File,
kind: SwiftExpressionKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
let isBundleArgument: ([String: SourceKitRepresentable]) -> Bool = { $0.name == "bundle" }
guard kind == .call,
dictionary.name == "NSLocalizedString",
let offset = dictionary.offset,
!dictionary.enclosedArguments.contains(where: isBundleArgument) else {
return []
}

return [
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, byteOffset: offset))
]
}
}
4 changes: 4 additions & 0 deletions SwiftLint.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
1F11B3CF1C252F23002E8FA8 /* ClosingBraceRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F11B3CE1C252F23002E8FA8 /* ClosingBraceRule.swift */; };
24B4DF0D1D6DFDE90097803B /* RedundantNilCoalescingRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24B4DF0B1D6DFA370097803B /* RedundantNilCoalescingRule.swift */; };
24E17F721B14BB3F008195BE /* File+Cache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24E17F701B1481FF008195BE /* File+Cache.swift */; };
287F8B642230843000BDC504 /* NSLocalizedStringRequireBundleRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 287F8B62223083ED00BDC504 /* NSLocalizedStringRequireBundleRule.swift */; };
2882895F222975D00037CF5F /* NSObjectPreferIsEqualRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2882895B22287C9C0037CF5F /* NSObjectPreferIsEqualRule.swift */; };
288289602229776C0037CF5F /* NSObjectPreferIsEqualRuleExamples.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2882895D22287E2C0037CF5F /* NSObjectPreferIsEqualRuleExamples.swift */; };
29AD4C661F6EA1D5009B66E1 /* ContainsOverFirstNotNilRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29AD4C641F6EA16C009B66E1 /* ContainsOverFirstNotNilRule.swift */; };
Expand Down Expand Up @@ -500,6 +501,7 @@
1F11B3CE1C252F23002E8FA8 /* ClosingBraceRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ClosingBraceRule.swift; sourceTree = "<group>"; };
24B4DF0B1D6DFA370097803B /* RedundantNilCoalescingRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RedundantNilCoalescingRule.swift; sourceTree = "<group>"; };
24E17F701B1481FF008195BE /* File+Cache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "File+Cache.swift"; sourceTree = "<group>"; };
287F8B62223083ED00BDC504 /* NSLocalizedStringRequireBundleRule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSLocalizedStringRequireBundleRule.swift; sourceTree = "<group>"; };
2882895B22287C9C0037CF5F /* NSObjectPreferIsEqualRule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSObjectPreferIsEqualRule.swift; sourceTree = "<group>"; };
2882895D22287E2C0037CF5F /* NSObjectPreferIsEqualRuleExamples.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSObjectPreferIsEqualRuleExamples.swift; sourceTree = "<group>"; };
29AD4C641F6EA16C009B66E1 /* ContainsOverFirstNotNilRule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContainsOverFirstNotNilRule.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1083,6 +1085,7 @@
D4DABFD61E2C23B1009617B6 /* NotificationCenterDetachmentRule.swift */,
D4DABFD81E2C59BC009617B6 /* NotificationCenterDetachmentRuleExamples.swift */,
D41985E621F85014003BE2B7 /* NSLocalizedStringKeyRule.swift */,
287F8B62223083ED00BDC504 /* NSLocalizedStringRequireBundleRule.swift */,
2882895B22287C9C0037CF5F /* NSObjectPreferIsEqualRule.swift */,
2882895D22287E2C0037CF5F /* NSObjectPreferIsEqualRuleExamples.swift */,
78F032441D7C877800BE709A /* OverriddenSuperCallRule.swift */,
Expand Down Expand Up @@ -1939,6 +1942,7 @@
D4FBADD01E00DA0400669C73 /* OperatorUsageWhitespaceRule.swift in Sources */,
D4C4A3521DEFBBB700E0E04C /* FileHeaderConfiguration.swift in Sources */,
623675B01F960C5C009BE6F3 /* QuickDiscouragedPendingTestRule.swift in Sources */,
287F8B642230843000BDC504 /* NSLocalizedStringRequireBundleRule.swift in Sources */,
D47079AD1DFE2FA700027086 /* EmptyParametersRule.swift in Sources */,
E87E4A091BFB9CAE00FCFE46 /* SyntaxKind+SwiftLint.swift in Sources */,
3B0B14541C505D6300BE82F7 /* SeverityConfiguration.swift in Sources */,
Expand Down
7 changes: 7 additions & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,12 @@ extension NSLocalizedStringKeyRuleTests {
]
}

extension NSLocalizedStringRequireBundleRuleTests {
static var allTests: [(String, (NSLocalizedStringRequireBundleRuleTests) -> () throws -> Void)] = [
("testWithDefaultConfiguration", testWithDefaultConfiguration)
]
}

extension NSObjectPreferIsEqualRuleTests {
static var allTests: [(String, (NSObjectPreferIsEqualRuleTests) -> () throws -> Void)] = [
("testWithDefaultConfiguration", testWithDefaultConfiguration)
Expand Down Expand Up @@ -1590,6 +1596,7 @@ XCTMain([
testCase(MultilineParametersRuleTests.allTests),
testCase(MultipleClosuresWithTrailingClosureRuleTests.allTests),
testCase(NSLocalizedStringKeyRuleTests.allTests),
testCase(NSLocalizedStringRequireBundleRuleTests.allTests),
testCase(NSObjectPreferIsEqualRuleTests.allTests),
testCase(NestingRuleTests.allTests),
testCase(NimbleOperatorRuleTests.allTests),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,12 @@ class NSLocalizedStringKeyRuleTests: XCTestCase {
}
}

class NSLocalizedStringRequireBundleRuleTests: XCTestCase {
func testWithDefaultConfiguration() {
verifyRule(NSLocalizedStringRequireBundleRule.description)
}
}

class NSObjectPreferIsEqualRuleTests: XCTestCase {
func testWithDefaultConfiguration() {
verifyRule(NSObjectPreferIsEqualRule.description)
Expand Down

0 comments on commit 442a59d

Please sign in to comment.