Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2581 from realm/mf-nslocalizedstring_key
Add `nslocalizedstring_key` opt-in rule
- Loading branch information
Showing
7 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
Source/SwiftLintFramework/Rules/Lint/NSLocalizedStringKeyRule.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public struct NSLocalizedStringKeyRule: ASTRule, OptInRule, ConfigurationProviderRule, AutomaticTestableRule { | ||
public var configuration = SeverityConfiguration(.warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "nslocalizedstring_key", | ||
name: "NSLocalizedString Key", | ||
description: "Static strings should be used as key in NSLocalizedString in order to genstrings work.", | ||
kind: .lint, | ||
nonTriggeringExamples: [ | ||
"NSLocalizedString(\"key\", comment: nil)", | ||
"NSLocalizedString(\"key\" + \"2\", comment: nil)" | ||
], | ||
triggeringExamples: [ | ||
"NSLocalizedString(↓method(), comment: nil)", | ||
"NSLocalizedString(↓\"key_\\(param)\", comment: nil)" | ||
] | ||
) | ||
|
||
public func validate(file: File, | ||
kind: SwiftExpressionKind, | ||
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { | ||
guard kind == .call, | ||
dictionary.name == "NSLocalizedString", | ||
let firstArgument = dictionary.enclosedArguments.first, | ||
firstArgument.name == nil, | ||
let offset = firstArgument.offset, | ||
let length = firstArgument.length, | ||
case let kinds = file.syntaxMap.kinds(inByteRange: NSRange(location: offset, length: length)), | ||
!kinds.allSatisfy({ $0 == .string }) else { | ||
return [] | ||
} | ||
|
||
return [ | ||
StyleViolation(ruleDescription: type(of: self).description, | ||
severity: configuration.severity, | ||
location: Location(file: file, byteOffset: offset)) | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters