Skip to content

Commit

Permalink
Respect some xclint configs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Nov 2, 2023
1 parent bda87a9 commit b7fe06d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
18 changes: 18 additions & 0 deletions Sources/XCLinting/ConfigurationFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,22 @@ extension Configuration: Decodable {

self.rules = decodedRules
}

public func validate() throws {
let allIdentifiers = XCLinter.ruleIdentifiers

for id in disabledRules {
if allIdentifiers.contains(id) == false {
throw XCLintError.unrecognizedRuleName(id)
}
}
}
}

extension Configuration {
public var enabledRules: Set<String> {
let defaultIdentifiers = XCLinter.defaultRuleIdentifiers

return defaultIdentifiers.subtracting(disabledRules)
}
}
5 changes: 4 additions & 1 deletion Sources/XCLinting/XCLintError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ public enum XCLintError: Error {
case noProjectFileSpecified
case projectFileNotFound(String)
case badProjectFile(String)

case unrecognizedRuleName(String)

public var localizedDescription: String {
switch self {
case .noProjectFileSpecified:
Expand All @@ -11,6 +12,8 @@ public enum XCLintError: Error {
return "Project file not found at '\(path)'."
case let .badProjectFile(message):
return "Bad project file: \(message)."
case let .unrecognizedRuleName(name):
return "Unrecognized rule name: \(name)"
}
}
}
28 changes: 23 additions & 5 deletions Sources/XCLinting/XCLinter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,19 @@ public struct XCLinter {
public init(projectPath: String, configuration: Configuration) throws {
let env = try Environment(projectPath: projectPath, configuration: configuration)

self.init(environment: env)
try self.init(environment: env)
}

public init(environment: Environment, rules: [Rule] = XCLinter.defaultRules) {
public init(environment: Environment) throws {
let config = environment.configuration
let enabledRules = config.enabledRules

let rules = Self.ruleMap.filter({ enabledRules.contains($0.key) }).values

self.init(environment: environment, rules: Array(rules))
}

public init(environment: Environment, rules: [Rule]) {
self.environment = environment
self.rules = rules
}
Expand Down Expand Up @@ -56,8 +65,17 @@ extension XCLinter.Environment {
}

extension XCLinter {
public static let defaultRules: [Rule] = [
embeddedBuildSettingsRule,
{ try BuildFilesAreOrderedRule().run($0) }
public static let defaultRuleIdentifiers: Set<String> = [
"embedded_build_setting",
"build_files_ordered"
]

public static let defaultRules: [Rule] = Array(ruleMap.filter({ defaultRuleIdentifiers.contains($0.0) }).values)
public static let ruleIdentifiers: Set<String> = Set(ruleMap.keys)

public static let ruleMap: [String: Rule] = [
"embedded_build_setting": embeddedBuildSettingsRule,
"build_files_ordered": { try BuildFilesAreOrderedRule().run($0) },
"groups_sorted": groupsAreSortedRule,
]
}
5 changes: 4 additions & 1 deletion Sources/clitool/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ struct XCLintCommand: ParsableCommand {

// find the effective environment
let config = try resolvedConfiguration(projectRootURL: URL(fileURLWithPath: projPath))

try config.validate()

let env = try XCLinter.Environment(projectPath: projPath, configuration: config)

let linter = XCLinter(environment: env)
let linter = try XCLinter(environment: env)

let violations = try linter.run()

Expand Down

0 comments on commit b7fe06d

Please sign in to comment.