From 8cf9d65658a34e37639f65f0a6e0cd3001bf6350 Mon Sep 17 00:00:00 2001 From: Matt <85322+mattmassicotte@users.noreply.github.com> Date: Mon, 22 Jan 2024 13:21:23 -0500 Subject: [PATCH] Remove fatalError usage from BuildFilesAreOrderedRule --- .../Rules/BuildFilesAreOrderedRule.swift | 57 +++++++++++++------ 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/Sources/XCLinting/Rules/BuildFilesAreOrderedRule.swift b/Sources/XCLinting/Rules/BuildFilesAreOrderedRule.swift index 0190e79..4cebb95 100644 --- a/Sources/XCLinting/Rules/BuildFilesAreOrderedRule.swift +++ b/Sources/XCLinting/Rules/BuildFilesAreOrderedRule.swift @@ -22,16 +22,25 @@ struct BuildFilesAreOrderedRule { // verify that there is more than one item in this section, otherwise no violations guard var previousLine = lines.first, lines.count > 1 else { return [] } - var previousId = getId(from: previousLine) - + guard var previousId = getId(from: previousLine) else { return [] } + var violations = [Violation]() for line in lines.dropFirst() { - let id = getId(from: line) - + guard let id = getId(from: line) else { + continue + } + // compare the identifiers of this line with the previous line if verify(previousId, isLessThan: id) == false { - violations.append(.init("\(sectionType) \(violationNote(from: line)) is out of order with \(violationNote(from: previousLine)).")) + guard + let lineNote = violationNote(from: line), + let previousLineNote = violationNote(from: previousLine) + else { + continue + } + + violations.append(.init("\(sectionType) \(lineNote) is out of order with \(previousLineNote).")) } previousId = id previousLine = line @@ -51,23 +60,39 @@ struct BuildFilesAreOrderedRule { private let lineRegex = try! NSRegularExpression(pattern: #"^\s*([A-Z0-9]{24})\s+\/\*\s([^\*]*)\s\*\/"#, options: []) /// This function will find the `Substring` for the id of the PBXBuildFile or PBXFileReference - private func getId(from line: String) -> Substring { - guard let match = lineRegex.firstMatch(in: line, options: [], range: line.nsrange), - let idRange = Range(match.range(at: 1), in: line) - else { fatalError() } + private func getId(from line: String) -> Substring? { + guard + let match = lineRegex.firstMatch(in: line, options: [], range: line.nsrange), + let idRange = Range(match.range(at: 1), in: line) + else { + print("BuildFilesAreOrderedRule failed to match on: ", line) + + return nil + } return line[idRange] } - private func getFileInfo(from line: String) -> Substring { - guard let match = lineRegex.firstMatch(in: line, options: [], range: line.nsrange), - let infoRange = Range(match.range(at: 2), in: line) - else { fatalError() } + private func getFileInfo(from line: String) -> Substring? { + guard + let match = lineRegex.firstMatch(in: line, options: [], range: line.nsrange), + let infoRange = Range(match.range(at: 2), in: line) + else { + print("BuildFilesAreOrderedRule failed to match on: ", line) + + return nil + } + return line[infoRange] } - private func violationNote(from line: String) -> String { - let id = getId(from: line) - let info = getFileInfo(from: line) + private func violationNote(from line: String) -> String? { + guard + let id = getId(from: line), + let info = getFileInfo(from: line) + else { + return nil + } + return "'(\(id)) \(info)'" }