mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
With the binding of configurations to their associated rule types "unknown configuration" errors can be made more specific mentioning also the rule's identifier in the printed message.
67 lines
2.4 KiB
Swift
67 lines
2.4 KiB
Swift
import SwiftSyntax
|
|
|
|
struct NSLocalizedStringRequireBundleRule: SwiftSyntaxRule, OptInRule, ConfigurationProviderRule {
|
|
var configuration = SeverityConfiguration<Self>(.warning)
|
|
|
|
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")
|
|
""")
|
|
]
|
|
)
|
|
|
|
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
|
|
Visitor(viewMode: .sourceAccurate)
|
|
}
|
|
}
|
|
|
|
private extension NSLocalizedStringRequireBundleRule {
|
|
final class Visitor: ViolationsSyntaxVisitor {
|
|
override func visitPost(_ node: FunctionCallExprSyntax) {
|
|
if let identifierExpr = node.calledExpression.as(IdentifierExprSyntax.self),
|
|
identifierExpr.identifier.tokenKind == .identifier("NSLocalizedString"),
|
|
!node.argumentList.containsArgument(named: "bundle") {
|
|
violations.append(node.positionAfterSkippingLeadingTrivia)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private extension TupleExprElementListSyntax {
|
|
func containsArgument(named name: String) -> Bool {
|
|
contains { arg in
|
|
arg.label?.tokenKind == .identifier(name)
|
|
}
|
|
}
|
|
}
|