Skip to content

Commit

Permalink
Print invalid keys when configuration parsing fails (realm#5350)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny authored Nov 14, 2023
1 parent 9b25efa commit fb4ef95
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 12 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

#### Enhancements

* None.
* Print invalid keys when configuration parsing fails.
[SimplyDanny](https://github.com/SimplyDanny)
[#5347](https://github.com/realm/SwiftLint/pull/5347)

#### Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ extension Configuration {
// Log an error when supplying invalid keys in the configuration dictionary
let invalidKeys = Set(dict.keys).subtracting(validKeys(ruleList: ruleList))
if invalidKeys.isNotEmpty {
Issue.invalidConfigurationKeys(invalidKeys.sorted()).print()
Issue.invalidRuleIDs(invalidKeys).print()
}
}

Expand Down
19 changes: 16 additions & 3 deletions Source/SwiftLintCore/Models/Issue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ public enum Issue: LocalizedError, Equatable {
case invalidConfiguration(ruleID: String)

/// Some configuration keys are invalid.
case invalidConfigurationKeys([String])
case invalidConfigurationKeys(ruleID: String, keys: Set<String>)

/// Used rule IDs are invalid.
case invalidRuleIDs(Set<String>)

/// A generic warning specified by a string.
case genericWarning(String)
Expand Down Expand Up @@ -94,8 +97,10 @@ public enum Issue: LocalizedError, Equatable {
return "'\(old)' has been renamed to '\(new)' and will be completely removed in a future release."
case let .invalidConfiguration(id):
return "Invalid configuration for '\(id)'. Falling back to default."
case let .invalidConfigurationKeys(keys):
return "Configuration contains invalid keys \(keys.joined(separator: ", "))."
case let .invalidConfigurationKeys(id, keys):
return "Configuration for '\(id)' rule contains the invalid key(s) \(keys.formatted)."
case let .invalidRuleIDs(ruleIDs):
return "The key(s) \(ruleIDs.formatted) used as rule identifier(s) is/are invalid."
case let .genericWarning(message), let .genericError(message):
return message
case let .ruleDeprecated(id):
Expand Down Expand Up @@ -123,3 +128,11 @@ public enum Issue: LocalizedError, Equatable {
}
}
}

private extension Set where Element == String {
var formatted: String {
sorted()
.map { "'\($0)'" }
.joined(separator: ", ")
}
}
5 changes: 4 additions & 1 deletion Source/SwiftLintCore/Models/RuleList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@ public struct RuleList {
rule: configuredRule,
initializedWithNonEmptyConfiguration: isConfigured
)
continue
} catch let issue as Issue {
issue.print()
} catch {
Issue.invalidConfiguration(ruleID: identifier).print()
rules[identifier] = (ruleType.init(), false)
}
rules[identifier] = (ruleType.init(), false)
}

// Add remaining rules without configuring them
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintCoreMacros/RuleConfigurationMacros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ enum AutoApply: MemberMacro {
\(raw: elementsWithKeyUpdate.joined(separator: "\n"))
if !supportedKeys.isSuperset(of: configuration.keys) {
let unknownKeys = Set(configuration.keys).subtracting(supportedKeys)
throw Issue.invalidConfigurationKeys(unknownKeys.sorted())
throw Issue.invalidConfigurationKeys(ruleID: Parent.identifier, keys: unknownKeys)
}
}
"""
Expand Down
6 changes: 3 additions & 3 deletions Tests/MacroTests/AutoApplyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final class AutoApplyTests: XCTestCase {
if !supportedKeys.isSuperset(of: configuration.keys) {
let unknownKeys = Set(configuration.keys).subtracting(supportedKeys)
throw Issue.invalidConfigurationKeys(unknownKeys.sorted())
throw Issue.invalidConfigurationKeys(ruleID: Parent.identifier, keys: unknownKeys)
}
}
}
Expand Down Expand Up @@ -81,7 +81,7 @@ final class AutoApplyTests: XCTestCase {
if !supportedKeys.isSuperset(of: configuration.keys) {
let unknownKeys = Set(configuration.keys).subtracting(supportedKeys)
throw Issue.invalidConfigurationKeys(unknownKeys.sorted())
throw Issue.invalidConfigurationKeys(ruleID: Parent.identifier, keys: unknownKeys)
}
}
}
Expand Down Expand Up @@ -120,7 +120,7 @@ final class AutoApplyTests: XCTestCase {
try $e2.performAfterParseOperations()
if !supportedKeys.isSuperset(of: configuration.keys) {
let unknownKeys = Set(configuration.keys).subtracting(supportedKeys)
throw Issue.invalidConfigurationKeys(unknownKeys.sorted())
throw Issue.invalidConfigurationKeys(ruleID: Parent.identifier, keys: unknownKeys)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ExplicitTypeInterfaceConfigurationTests: SwiftLintTestCase {

func testInvalidKeyInCustomConfiguration() {
var config = ExplicitTypeInterfaceConfiguration()
checkError(Issue.invalidConfigurationKeys(["invalidKey"])) {
checkError(Issue.invalidConfigurationKeys(ruleID: ExplicitTypeInterfaceRule.identifier, keys: ["invalidKey"])) {
try config.apply(configuration: ["invalidKey": "error"])
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ class RuleConfigurationDescriptionTests: XCTestCase {
func testInvalidKeys() throws {
var configuration = TestConfiguration()

checkError(Issue.invalidConfigurationKeys(["unknown", "unsupported"])) {
checkError(Issue.invalidConfigurationKeys(ruleID: "RuleMock", keys: ["unknown", "unsupported"])) {
try configuration.apply(configuration: [
"severity": "error",
"warning": 3,
Expand Down

0 comments on commit fb4ef95

Please sign in to comment.