Files
SwiftLint/Source/SwiftLintFramework/Rules/Lint/NSLocalizedStringRequireBundleRule.swift
T
Zev Eisenberg fcf848608e Add Inline test failure messages (#3040)
* Add Example wrapper in order to display test failures inline when running in Xcode.
* Stop using Swift 5.1-only features so we can compile on Xcode 10.2.
* Wrap strings in Example.
* Add Changelog entry.
* Wrap all examples in Example struct.
* Better and more complete capturing of line numbers.
* Fix broken test.
* Better test traceability.
* Address or disable linting warnings.
* Add documentation comments.
* Disable linter for a few cases.
* Limit mutability and add copy-and-mutate utility functions.
* Limit scope of mutability.
2020-02-02 10:35:37 +02:00

61 lines
2.3 KiB
Swift

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 NSLocalizedString should specify the bundle which contains the strings file.",
kind: .lint,
nonTriggeringExamples: [
Example("""
NSLocalizedString("someKey", bundle: .main, comment: "test")
"""),
Example("""
NSLocalizedString("someKey", tableName: "a",
bundle: Bundle(for: A.self),
comment: "test")
"""),
Example("""
NSLocalizedString("someKey", tableName: "xyz",
bundle: someBundle, value: "test"
comment: "test")
"""),
Example("""
arbitraryFunctionCall("something")
""")
],
triggeringExamples: [
Example("""
↓NSLocalizedString("someKey", comment: "test")
"""),
Example("""
↓NSLocalizedString("someKey", tableName: "a", comment: "test")
"""),
Example("""
↓NSLocalizedString("someKey", tableName: "xyz",
value: "test", comment: "test")
""")
]
)
public func validate(file: SwiftLintFile,
kind: SwiftExpressionKind,
dictionary: SourceKittenDictionary) -> [StyleViolation] {
let isBundleArgument: (SourceKittenDictionary) -> 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))
]
}
}