Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print invalid keys when configuration parsing fails #5350

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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