Skip to content

Commit

Permalink
realm#2422 - Replace ruleDescription change with a StyleViolation rea…
Browse files Browse the repository at this point in the history
…son field in CompilerProtocolInitRule
  • Loading branch information
Timofey Solonin authored and sjavora committed Mar 9, 2019
1 parent f6ba18a commit 042bfba
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions Source/SwiftLintFramework/Rules/Lint/CompilerProtocolInitRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,38 @@ public struct CompilerProtocolInitRule: ASTRule, ConfigurationProviderRule {

public init() {}

public static let description = specificDescription(for: "such as `ExpressibleByArrayLiteral`", isPlural: true)

private static func specificDescription(for violation: String, isPlural: Bool) -> RuleDescription {
return RuleDescription(
identifier: "compiler_protocol_init",
name: "Compiler Protocol Init",
description: "The initializers declared in compiler protocol\(isPlural ? "s" : "") \(violation) " +
"shouldn't be called directly.",
kind: .lint,
nonTriggeringExamples: [
"let set: Set<Int> = [1, 2]\n",
"let set = Set(array)\n"
],
triggeringExamples: [
"let set = ↓Set(arrayLiteral: 1, 2)\n",
"let set = ↓Set.init(arrayLiteral: 1, 2)\n"
]
)
public static let description = RuleDescription(
identifier: "compiler_protocol_init",
name: "Compiler Protocol Init",
description: CompilerProtocolInitRule.violationReason(
protocolName: "such as `ExpressibleByArrayLiteral`",
isPlural: true
),
kind: .lint,
nonTriggeringExamples: [
"let set: Set<Int> = [1, 2]\n",
"let set = Set(array)\n"
],
triggeringExamples: [
"let set = ↓Set(arrayLiteral: 1, 2)\n",
"let set = ↓Set.init(arrayLiteral: 1, 2)\n"
]
)

private static func violationReason(protocolName: String, isPlural: Bool) -> String {
return "The initializers declared in compiler protocol\(isPlural ? "s" : "") \(protocolName) " +
"shouldn't be called directly."
}

public func validate(file: File, kind: SwiftExpressionKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
return violationRanges(in: file, kind: kind, dictionary: dictionary).map {
let (violation, range) = $0
return StyleViolation(
ruleDescription: type(of: self).specificDescription(for: violation.protocolName, isPlural: false),
ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, characterOffset: range.location)
location: Location(file: file, characterOffset: range.location),
reason: type(of: self).violationReason(protocolName: violation.protocolName, isPlural: false)
)
}
}
Expand Down

0 comments on commit 042bfba

Please sign in to comment.