Skip to content

Commit

Permalink
realm#2422 - Specify which compiler protocol initialization rule was …
Browse files Browse the repository at this point in the history
…violated
  • Loading branch information
Timofey Solonin committed Oct 1, 2018
1 parent bf85625 commit 5bf50c8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
51 changes: 29 additions & 22 deletions Source/SwiftLintFramework/Rules/Lint/CompilerProtocolInitRule.swift
Original file line number Diff line number Diff line change
@@ -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<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 = 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 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 []
}
Expand All @@ -47,15 +54,15 @@ public struct CompilerProtocolInitRule: ASTRule, ConfigurationProviderRule, Auto
continue
}

return [range]
return [(compilerProtocol, range)]
}

return []
}
}

private struct ExpressibleByCompiler {
private let protocolName: String
let protocolName: String
let initCallNames: Set<String>
private let arguments: [[String]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ class CommaRuleTests: XCTestCase {
}
}

class CompilerProtocolInitRuleTests: XCTestCase {
func testWithDefaultConfiguration() {
verifyRule(CompilerProtocolInitRule.description)
}
}

class ContainsOverFirstNotNilRuleTests: XCTestCase {
func testWithDefaultConfiguration() {
verifyRule(ContainsOverFirstNotNilRule.description)
Expand Down

0 comments on commit 5bf50c8

Please sign in to comment.