From 5bf50c8920c0eb0e44b8954f396f4486d1fc69f0 Mon Sep 17 00:00:00 2001 From: Timofey Solonin Date: Mon, 1 Oct 2018 12:32:43 +0300 Subject: [PATCH] #2422 - Specify which compiler protocol initialization rule was violated --- .../Rules/Lint/CompilerProtocolInitRule.swift | 51 +++++++++++-------- .../AutomaticRuleTests.generated.swift | 6 --- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/Source/SwiftLintFramework/Rules/Lint/CompilerProtocolInitRule.swift b/Source/SwiftLintFramework/Rules/Lint/CompilerProtocolInitRule.swift index 4e93b3d6c4..d4e5a06fa6 100644 --- a/Source/SwiftLintFramework/Rules/Lint/CompilerProtocolInitRule.swift +++ b/Source/SwiftLintFramework/Rules/Lint/CompilerProtocolInitRule.swift @@ -1,38 +1,45 @@ import Foundation import SourceKittenFramework -public struct CompilerProtocolInitRule: ASTRule, ConfigurationProviderRule, AutomaticTestableRule { +public struct CompilerProtocolInitRule: ASTRule, ConfigurationProviderRule { public var configuration = SeverityConfiguration(.warning) public init() {} - public static let description = RuleDescription( - identifier: "compiler_protocol_init", - name: "Compiler Protocol Init", - description: "The initializers declared in compiler protocols such as `ExpressibleByArrayLiteral` " + - "shouldn't be called directly.", - kind: .lint, - nonTriggeringExamples: [ - "let set: Set = [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 = 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 = [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 func validate(file: File, kind: SwiftExpressionKind, dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { return violationRanges(in: file, kind: kind, dictionary: dictionary).map { - StyleViolation(ruleDescription: type(of: self).description, - severity: configuration.severity, - location: Location(file: file, characterOffset: $0.location)) + let (violation, range) = $0 + return StyleViolation( + ruleDescription: type(of: self).specificDescription(for: violation.protocolName, isPlural: false), + severity: configuration.severity, + location: Location(file: file, characterOffset: range.location) + ) } } private func violationRanges(in file: File, kind: SwiftExpressionKind, - dictionary: [String: SourceKitRepresentable]) -> [NSRange] { + dictionary: [String: SourceKitRepresentable]) -> [(ExpressibleByCompiler, NSRange)] { guard kind == .call, let name = dictionary.name else { return [] } @@ -47,7 +54,7 @@ public struct CompilerProtocolInitRule: ASTRule, ConfigurationProviderRule, Auto continue } - return [range] + return [(compilerProtocol, range)] } return [] @@ -55,7 +62,7 @@ public struct CompilerProtocolInitRule: ASTRule, ConfigurationProviderRule, Auto } private struct ExpressibleByCompiler { - private let protocolName: String + let protocolName: String let initCallNames: Set private let arguments: [[String]] diff --git a/Tests/SwiftLintFrameworkTests/AutomaticRuleTests.generated.swift b/Tests/SwiftLintFrameworkTests/AutomaticRuleTests.generated.swift index 6e9b91a395..8916c82b64 100644 --- a/Tests/SwiftLintFrameworkTests/AutomaticRuleTests.generated.swift +++ b/Tests/SwiftLintFrameworkTests/AutomaticRuleTests.generated.swift @@ -66,12 +66,6 @@ class CommaRuleTests: XCTestCase { } } -class CompilerProtocolInitRuleTests: XCTestCase { - func testWithDefaultConfiguration() { - verifyRule(CompilerProtocolInitRule.description) - } -} - class ContainsOverFirstNotNilRuleTests: XCTestCase { func testWithDefaultConfiguration() { verifyRule(ContainsOverFirstNotNilRule.description)