Skip to content

Commit

Permalink
Use 3 spaces for indentation instead of 4
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed Apr 8, 2023
1 parent 6c2b940 commit 6ea226d
Show file tree
Hide file tree
Showing 49 changed files with 2,962 additions and 2,962 deletions.
152 changes: 76 additions & 76 deletions Sources/AnyLint/AutoCorrection.swift
Expand Up @@ -3,89 +3,89 @@ import Utility

/// Information about an autocorrection.
public struct AutoCorrection {
/// The matching text before applying the autocorrection.
public let before: String

/// The matching text after applying the autocorrection.
public let after: String

var appliedMessageLines: [String] {
if useDiffOutput, #available(OSX 10.15, *) {
var lines: [String] = ["Autocorrection applied, the diff is: (+ added, - removed)"]

let beforeLines = before.components(separatedBy: .newlines)
let afterLines = after.components(separatedBy: .newlines)

for difference in afterLines.difference(from: beforeLines).sorted() {
switch difference {
case let .insert(offset, element, _):
lines.append("+ [L\(offset + 1)] \(element)".green)

case let .remove(offset, element, _):
lines.append("- [L\(offset + 1)] \(element)".red)
}
/// The matching text before applying the autocorrection.
public let before: String

/// The matching text after applying the autocorrection.
public let after: String

var appliedMessageLines: [String] {
if useDiffOutput, #available(OSX 10.15, *) {
var lines: [String] = ["Autocorrection applied, the diff is: (+ added, - removed)"]

let beforeLines = before.components(separatedBy: .newlines)
let afterLines = after.components(separatedBy: .newlines)

for difference in afterLines.difference(from: beforeLines).sorted() {
switch difference {
case let .insert(offset, element, _):
lines.append("+ [L\(offset + 1)] \(element)".green)

case let .remove(offset, element, _):
lines.append("- [L\(offset + 1)] \(element)".red)
}

return lines
} else {
return [
"Autocorrection applied, the diff is: (+ added, - removed)",
"- \(before.showWhitespacesAndNewlines())".red,
"+ \(after.showWhitespacesAndNewlines())".green,
]
}
}

var useDiffOutput: Bool {
before.components(separatedBy: .newlines).count >= Constants.newlinesRequiredForDiffing ||
after.components(separatedBy: .newlines).count >= Constants.newlinesRequiredForDiffing
}

/// Initializes an autocorrection.
public init(before: String, after: String) {
self.before = before
self.after = after
}
}

return lines
} else {
return [
"Autocorrection applied, the diff is: (+ added, - removed)",
"- \(before.showWhitespacesAndNewlines())".red,
"+ \(after.showWhitespacesAndNewlines())".green,
]
}
}

var useDiffOutput: Bool {
before.components(separatedBy: .newlines).count >= Constants.newlinesRequiredForDiffing ||
after.components(separatedBy: .newlines).count >= Constants.newlinesRequiredForDiffing
}

/// Initializes an autocorrection.
public init(before: String, after: String) {
self.before = before
self.after = after
}
}

extension AutoCorrection: ExpressibleByDictionaryLiteral {
public init(dictionaryLiteral elements: (String, String)...) {
guard
let before = elements.first(where: { $0.0 == "before" })?.1,
let after = elements.first(where: { $0.0 == "after" })?.1
else {
log.message("Failed to convert Dictionary literal '\(elements)' to type AutoCorrection.", level: .error)
log.exit(status: .failure)
exit(EXIT_FAILURE) // only reachable in unit tests
}

self = AutoCorrection(before: before, after: after)
}
public init(dictionaryLiteral elements: (String, String)...) {
guard
let before = elements.first(where: { $0.0 == "before" })?.1,
let after = elements.first(where: { $0.0 == "after" })?.1
else {
log.message("Failed to convert Dictionary literal '\(elements)' to type AutoCorrection.", level: .error)
log.exit(status: .failure)
exit(EXIT_FAILURE) // only reachable in unit tests
}
self = AutoCorrection(before: before, after: after)
}
}

// TODO: make the autocorrection diff sorted by line number
@available(OSX 10.15, *)
extension CollectionDifference.Change: Comparable where ChangeElement == String {
public static func < (lhs: Self, rhs: Self) -> Bool {
switch (lhs, rhs) {
case let (.remove(leftOffset, _, _), .remove(rightOffset, _, _)), let (.insert(leftOffset, _, _), .insert(rightOffset, _, _)):
return leftOffset < rightOffset

case let (.remove(leftOffset, _, _), .insert(rightOffset, _, _)):
return leftOffset < rightOffset || true

case let (.insert(leftOffset, _, _), .remove(rightOffset, _, _)):
return leftOffset < rightOffset || false
}
}

public static func == (lhs: Self, rhs: Self) -> Bool {
switch (lhs, rhs) {
case let (.remove(leftOffset, _, _), .remove(rightOffset, _, _)), let (.insert(leftOffset, _, _), .insert(rightOffset, _, _)):
return leftOffset == rightOffset

case (.remove, .insert), (.insert, .remove):
return false
}
}
public static func < (lhs: Self, rhs: Self) -> Bool {
switch (lhs, rhs) {
case let (.remove(leftOffset, _, _), .remove(rightOffset, _, _)), let (.insert(leftOffset, _, _), .insert(rightOffset, _, _)):
return leftOffset < rightOffset
case let (.remove(leftOffset, _, _), .insert(rightOffset, _, _)):
return leftOffset < rightOffset || true
case let (.insert(leftOffset, _, _), .remove(rightOffset, _, _)):
return leftOffset < rightOffset || false
}
}
public static func == (lhs: Self, rhs: Self) -> Bool {
switch (lhs, rhs) {
case let (.remove(leftOffset, _, _), .remove(rightOffset, _, _)), let (.insert(leftOffset, _, _), .insert(rightOffset, _, _)):
return leftOffset == rightOffset
case (.remove, .insert), (.insert, .remove):
return false
}
}
}
110 changes: 55 additions & 55 deletions Sources/AnyLint/CheckInfo.swift
Expand Up @@ -3,77 +3,77 @@ import Utility

/// Provides some basic information needed in each lint check.
public struct CheckInfo {
/// The identifier of the check defined here. Can be used when defining exceptions within files for specific lint checks.
public let id: String
/// The identifier of the check defined here. Can be used when defining exceptions within files for specific lint checks.
public let id: String

/// The hint to be shown as guidance on what the issue is and how to fix it. Can reference any capture groups in the first regex parameter (e.g. `contentRegex`).
public let hint: String
/// The hint to be shown as guidance on what the issue is and how to fix it. Can reference any capture groups in the first regex parameter (e.g. `contentRegex`).
public let hint: String

/// The severity level for the report in case the check fails.
public let severity: Severity
/// The severity level for the report in case the check fails.
public let severity: Severity

/// Initializes a new info object for the lint check.
public init(id: String, hint: String, severity: Severity = .warning) {
self.id = id
self.hint = hint
self.severity = severity
}
/// Initializes a new info object for the lint check.
public init(id: String, hint: String, severity: Severity = .warning) {
self.id = id
self.hint = hint
self.severity = severity
}
}

extension CheckInfo: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
public func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}

extension CheckInfo: CustomStringConvertible {
public var description: String {
"check '\(id)'"
}
public var description: String {
"check '\(id)'"
}
}

extension CheckInfo: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
let customSeverityRegex: Regex = [
"id": #"^[^@:]+"#,
"severitySeparator": #"@"#,
"severity": #"[^:]+"#,
"hintSeparator": #": ?"#,
"hint": #".*$"#,
]
public init(stringLiteral value: String) {
let customSeverityRegex: Regex = [
"id": #"^[^@:]+"#,
"severitySeparator": #"@"#,
"severity": #"[^:]+"#,
"hintSeparator": #": ?"#,
"hint": #".*$"#,
]

if let customSeverityMatch = customSeverityRegex.firstMatch(in: value) {
let id = customSeverityMatch.captures[0]!
let severityString = customSeverityMatch.captures[2]!
let hint = customSeverityMatch.captures[4]!
if let customSeverityMatch = customSeverityRegex.firstMatch(in: value) {
let id = customSeverityMatch.captures[0]!
let severityString = customSeverityMatch.captures[2]!
let hint = customSeverityMatch.captures[4]!

guard let severity = Severity.from(string: severityString) else {
log.message("Specified severity '\(severityString)' for check '\(id)' unknown. Use one of [error, warning, info].", level: .error)
log.exit(status: .failure)
exit(EXIT_FAILURE) // only reachable in unit tests
}
guard let severity = Severity.from(string: severityString) else {
log.message("Specified severity '\(severityString)' for check '\(id)' unknown. Use one of [error, warning, info].", level: .error)
log.exit(status: .failure)
exit(EXIT_FAILURE) // only reachable in unit tests
}

self = CheckInfo(id: id, hint: hint, severity: severity)
} else {
let defaultSeverityRegex: Regex = [
"id": #"^[^@:]+"#,
"hintSeparator": #": ?"#,
"hint": #".*$"#,
]
self = CheckInfo(id: id, hint: hint, severity: severity)
} else {
let defaultSeverityRegex: Regex = [
"id": #"^[^@:]+"#,
"hintSeparator": #": ?"#,
"hint": #".*$"#,
]

guard let defaultSeverityMatch = defaultSeverityRegex.firstMatch(in: value) else {
log.message(
"Could not convert String literal '\(value)' to type CheckInfo. Please check the structure to be: <id>(@<severity>): <hint>",
level: .error
)
log.exit(status: .failure)
exit(EXIT_FAILURE) // only reachable in unit tests
}
guard let defaultSeverityMatch = defaultSeverityRegex.firstMatch(in: value) else {
log.message(
"Could not convert String literal '\(value)' to type CheckInfo. Please check the structure to be: <id>(@<severity>): <hint>",
level: .error
)
log.exit(status: .failure)
exit(EXIT_FAILURE) // only reachable in unit tests
}

let id = defaultSeverityMatch.captures[0]!
let hint = defaultSeverityMatch.captures[2]!
let id = defaultSeverityMatch.captures[0]!
let hint = defaultSeverityMatch.captures[2]!

self = CheckInfo(id: id, hint: hint)
}
}
self = CheckInfo(id: id, hint: hint)
}
}
}
2 changes: 1 addition & 1 deletion Sources/AnyLint/Checkers/Checker.swift
@@ -1,5 +1,5 @@
import Foundation

protocol Checker {
func performCheck() throws -> [Violation]
func performCheck() throws -> [Violation]
}

0 comments on commit 6ea226d

Please sign in to comment.