From dde2fb049dc3a50e35c17480caa1e4b415acc2a3 Mon Sep 17 00:00:00 2001 From: Vladimir Burdukov Date: Thu, 12 Sep 2024 17:19:12 +0300 Subject: [PATCH] Do not throw deprecation warning if deprecated property is not presented in configuration --- CHANGELOG.md | 5 ++++ .../RuleConfigurationMacros.swift | 4 ++- Tests/MacroTests/AutoConfigParserTests.swift | 26 +++++++++++++------ .../RuleConfigurationDescriptionTests.swift | 11 +++++--- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43b2f1aeb0..8349e5b825 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,11 @@ [SimplyDanny](https://github.com/SimplyDanny) [#5787](https://github.com/realm/SwiftLint/issues/5787) +* Do not throw deprecation warning if deprecated property is not + presented in configuration. + [chipp](https://github.com/chipp) + [#5791](https://github.com/realm/SwiftLint/issues/5791) + ## 0.57.0: Squeaky Clean Cycle #### Breaking diff --git a/Source/SwiftLintCoreMacros/RuleConfigurationMacros.swift b/Source/SwiftLintCoreMacros/RuleConfigurationMacros.swift index 665a9e4335..e1a44256e8 100644 --- a/Source/SwiftLintCoreMacros/RuleConfigurationMacros.swift +++ b/Source/SwiftLintCoreMacros/RuleConfigurationMacros.swift @@ -74,7 +74,9 @@ enum AutoConfigParser: MemberMacro { """ for option in nonInlinedOptions { """ - try \(raw: option).apply(configuration[$\(raw: option).key], ruleID: Parent.identifier) + if let value = configuration[$\(raw: option).key] { + try \(raw: option).apply(value, ruleID: Parent.identifier) + } """ } """ diff --git a/Tests/MacroTests/AutoConfigParserTests.swift b/Tests/MacroTests/AutoConfigParserTests.swift index 39d9505032..4164eb411f 100644 --- a/Tests/MacroTests/AutoConfigParserTests.swift +++ b/Tests/MacroTests/AutoConfigParserTests.swift @@ -80,8 +80,12 @@ final class AutoConfigParserTests: XCTestCase { guard let configuration = configuration as? [String: Any] else { throw Issue.invalidConfiguration(ruleID: Parent.identifier) } - try eA.apply(configuration[$eA.key], ruleID: Parent.identifier) - try eB.apply(configuration[$eB.key], ruleID: Parent.identifier) + if let value = configuration[$eA.key] { + try eA.apply(value, ruleID: Parent.identifier) + } + if let value = configuration[$eB.key] { + try eB.apply(value, ruleID: Parent.identifier) + } if !supportedKeys.isSuperset(of: configuration.keys) { let unknownKeys = Set(configuration.keys).subtracting(supportedKeys) Issue.invalidConfigurationKeys(ruleID: Parent.identifier, keys: unknownKeys).print() @@ -131,8 +135,12 @@ final class AutoConfigParserTests: XCTestCase { guard let configuration = configuration as? [String: Any] else { return } - try eA.apply(configuration[$eA.key], ruleID: Parent.identifier) - try eC.apply(configuration[$eC.key], ruleID: Parent.identifier) + if let value = configuration[$eA.key] { + try eA.apply(value, ruleID: Parent.identifier) + } + if let value = configuration[$eC.key] { + try eC.apply(value, ruleID: Parent.identifier) + } if !supportedKeys.isSuperset(of: configuration.keys) { let unknownKeys = Set(configuration.keys).subtracting(supportedKeys) Issue.invalidConfigurationKeys(ruleID: Parent.identifier, keys: unknownKeys).print() @@ -177,7 +185,6 @@ final class AutoConfigParserTests: XCTestCase { } func testSeverityAppliedTwice() { - // swiftlint:disable line_length assertMacroExpansion( """ @AutoConfigParser @@ -211,8 +218,12 @@ final class AutoConfigParserTests: XCTestCase { guard let configuration = configuration as? [String: Any] else { return } - try severityConfiguration.apply(configuration[$severityConfiguration.key], ruleID: Parent.identifier) - try foo.apply(configuration[$foo.key], ruleID: Parent.identifier) + if let value = configuration[$severityConfiguration.key] { + try severityConfiguration.apply(value, ruleID: Parent.identifier) + } + if let value = configuration[$foo.key] { + try foo.apply(value, ruleID: Parent.identifier) + } if !supportedKeys.isSuperset(of: configuration.keys) { let unknownKeys = Set(configuration.keys).subtracting(supportedKeys) Issue.invalidConfigurationKeys(ruleID: Parent.identifier, keys: unknownKeys).print() @@ -222,6 +233,5 @@ final class AutoConfigParserTests: XCTestCase { """, macros: macros ) - // swiftlint:enable line_length } } diff --git a/Tests/SwiftLintFrameworkTests/RuleConfigurationDescriptionTests.swift b/Tests/SwiftLintFrameworkTests/RuleConfigurationDescriptionTests.swift index a4fc6bd707..a656666d60 100644 --- a/Tests/SwiftLintFrameworkTests/RuleConfigurationDescriptionTests.swift +++ b/Tests/SwiftLintFrameworkTests/RuleConfigurationDescriptionTests.swift @@ -499,6 +499,12 @@ final class RuleConfigurationDescriptionTests: XCTestCase { ) } + func testNoDeprecationWarningIfNoDeprecatedPropertySet() throws { + var configuration = TestConfiguration() + + XCTAssert(try Issue.captureConsole { try configuration.apply(configuration: ["flag": false]) }.isEmpty) + } + func testInvalidKeys() throws { var configuration = TestConfiguration() @@ -511,10 +517,7 @@ final class RuleConfigurationDescriptionTests: XCTestCase { "unsupported": true, ]) }, - """ - warning: Configuration option 'set' in 'my_rule' rule is deprecated. Use the option 'other_opt' instead. - warning: Configuration for 'RuleMock' rule contains the invalid key(s) 'unknown', 'unsupported'. - """ + "warning: Configuration for 'RuleMock' rule contains the invalid key(s) 'unknown', 'unsupported'." ) }