Skip to content

Commit

Permalink
Merge pull request #1144 from realm/jp-api-guidelines-cli-commands
Browse files Browse the repository at this point in the history
update swiftlint/Commands to follow Swift 3 API Design Guidelines
  • Loading branch information
jpsim authored Jan 10, 2017
2 parents cbbcf27 + c2c79a8 commit 706bf72
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
8 changes: 4 additions & 4 deletions Source/swiftlint/Commands/LintCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ struct LintCommand: CommandProtocol {
linter.file.invalidateCache()
reporter.reportViolations(currentViolations, realtimeCondition: true)
}.flatMap { files in
if LintCommand.isWarningThresholdBroken(configuration, violations: violations) {
violations.append(LintCommand.createThresholdViolation(configuration.warningThreshold!))
if LintCommand.isWarningThresholdBroken(configuration: configuration, violations: violations) {
violations.append(LintCommand.createThresholdViolation(threshold: configuration.warningThreshold!))
reporter.reportViolations([violations.last!], realtimeCondition: true)
}
reporter.reportViolations(violations, realtimeCondition: false)
Expand Down Expand Up @@ -83,14 +83,14 @@ struct LintCommand: CommandProtocol {
)
}

private static func isWarningThresholdBroken(_ configuration: Configuration,
private static func isWarningThresholdBroken(configuration: Configuration,
violations: [StyleViolation]) -> Bool {
guard let warningThreshold = configuration.warningThreshold else { return false }
let numberOfWarningViolations = violations.filter({ $0.severity == .warning }).count
return numberOfWarningViolations >= warningThreshold
}

private static func createThresholdViolation(_ threshold: Int) -> StyleViolation {
private static func createThresholdViolation(threshold: Int) -> StyleViolation {
let description = RuleDescription(
identifier: "warning_threshold",
name: "Warning Threshold",
Expand Down
34 changes: 16 additions & 18 deletions Source/swiftlint/Commands/RulesCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,21 @@ import Result
import SwiftLintFramework
import SwiftyTextTable

private let violationMarker = ""
private func print(ruleDescription desc: RuleDescription) {
print("\(desc.consoleDescription)")

if !desc.triggeringExamples.isEmpty {
func indent(_ string: String) -> String {
return string.components(separatedBy: "\n")
.map { " \($0)" }
.joined(separator: "\n")
}
print("\nTriggering Examples (violation is marked with '↓'):")
for (index, example) in desc.triggeringExamples.enumerated() {
print("\nExample #\(index + 1)\n\n\(indent(example))")
}
}
}

struct RulesCommand: CommandProtocol {
let verb = "rules"
Expand All @@ -23,30 +37,14 @@ struct RulesCommand: CommandProtocol {
return .failure(.usageError(description: "No rule with identifier: \(ruleID)"))
}

printRuleDescript(rule.description)
print(ruleDescription: rule.description)
return .success()
}

let configuration = Configuration(commandLinePath: options.configurationFile)
print(TextTable(ruleList: masterRuleList, configuration: configuration).render())
return .success()
}

fileprivate func printRuleDescript(_ desc: RuleDescription) {
print("\(desc.consoleDescription)")

if !desc.triggeringExamples.isEmpty {
func indent(_ string: String) -> String {
return string.components(separatedBy: "\n")
.map { " \($0)" }
.joined(separator: "\n")
}
print("\nTriggering Examples (violation is marked with '\(violationMarker)'):")
for (index, example) in desc.triggeringExamples.enumerated() {
print("\nExample #\(index + 1)\n\n\(indent(example))")
}
}
}
}

struct RulesOptions: OptionsProtocol {
Expand Down

0 comments on commit 706bf72

Please sign in to comment.