From 4fa417e06f6625a11c168b409f78defe0ee12927 Mon Sep 17 00:00:00 2001 From: Marcelo Fabri Date: Sun, 23 Dec 2018 23:00:08 -0800 Subject: [PATCH] Add configuration to discouraged_object_literal Fixes #2439 --- CHANGELOG.md | 5 ++- .../DiscouragedObjectLiteralRule.swift | 14 +++++-- SwiftLint.xcodeproj/project.pbxproj | 5 ++- Tests/LinuxMain.swift | 4 +- .../AutomaticRuleTests.generated.swift | 6 --- .../DiscouragedObjectLiteralRuleTests.swift | 38 +++++++++++++++++++ 6 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 Tests/SwiftLintFrameworkTests/DiscouragedObjectLiteralRuleTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index 63d3842a8f..275ff0cab6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,10 @@ #### Enhancements -* None. +* Allow configuring `discouraged_object_literal` rule to only discourage one kind + of object literal. + [Marcelo Fabri](https://github.com/marcelofabri) + [#2439](https://github.com/realm/SwiftLint/issues/2439) #### Bug Fixes diff --git a/Source/SwiftLintFramework/Rules/Idiomatic/DiscouragedObjectLiteralRule.swift b/Source/SwiftLintFramework/Rules/Idiomatic/DiscouragedObjectLiteralRule.swift index 4d877e225b..1742b6cb4f 100644 --- a/Source/SwiftLintFramework/Rules/Idiomatic/DiscouragedObjectLiteralRule.swift +++ b/Source/SwiftLintFramework/Rules/Idiomatic/DiscouragedObjectLiteralRule.swift @@ -1,7 +1,7 @@ import SourceKittenFramework -public struct DiscouragedObjectLiteralRule: ASTRule, OptInRule, ConfigurationProviderRule, AutomaticTestableRule { - public var configuration = SeverityConfiguration(.warning) +public struct DiscouragedObjectLiteralRule: ASTRule, OptInRule, ConfigurationProviderRule { + public var configuration = ObjectLiteralConfiguration() public init() {} @@ -29,8 +29,16 @@ public struct DiscouragedObjectLiteralRule: ASTRule, OptInRule, ConfigurationPro dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { guard let offset = dictionary.offset, kind == .objectLiteral else { return [] } + if !configuration.imageLiteral && dictionary.name == "imageLiteral" { + return [] + } + + if !configuration.colorLiteral && dictionary.name == "colorLiteral" { + return [] + } + return [StyleViolation(ruleDescription: type(of: self).description, - severity: configuration.severity, + severity: configuration.severityConfiguration.severity, location: Location(file: file, byteOffset: offset))] } } diff --git a/SwiftLint.xcodeproj/project.pbxproj b/SwiftLint.xcodeproj/project.pbxproj index 1855a5bbab..e84a1d3ad2 100644 --- a/SwiftLint.xcodeproj/project.pbxproj +++ b/SwiftLint.xcodeproj/project.pbxproj @@ -219,6 +219,7 @@ D40FE89D1F867BFF006433E2 /* OverrideInExtensionRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = D40FE89C1F867BFF006433E2 /* OverrideInExtensionRule.swift */; }; D4130D971E16183F00242361 /* IdentifierNameRuleExamples.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4130D961E16183F00242361 /* IdentifierNameRuleExamples.swift */; }; D4130D991E16CC1300242361 /* TypeNameRuleExamples.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4130D981E16CC1300242361 /* TypeNameRuleExamples.swift */; }; + D414D6AC21D0B77F00960935 /* DiscouragedObjectLiteralRuleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D414D6AB21D0B77F00960935 /* DiscouragedObjectLiteralRuleTests.swift */; }; D41B57781ED8CEE0007B0470 /* ExtensionAccessModifierRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = D41B57771ED8CEE0007B0470 /* ExtensionAccessModifierRule.swift */; }; D41E7E0B1DF9DABB0065259A /* RedundantStringEnumValueRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = D41E7E0A1DF9DABB0065259A /* RedundantStringEnumValueRule.swift */; }; D4246D6D1F30D8620097E658 /* PrivateOverFilePrivateRuleConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4246D6C1F30D8620097E658 /* PrivateOverFilePrivateRuleConfiguration.swift */; }; @@ -668,6 +669,7 @@ D40FE89C1F867BFF006433E2 /* OverrideInExtensionRule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OverrideInExtensionRule.swift; sourceTree = ""; }; D4130D961E16183F00242361 /* IdentifierNameRuleExamples.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IdentifierNameRuleExamples.swift; sourceTree = ""; }; D4130D981E16CC1300242361 /* TypeNameRuleExamples.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TypeNameRuleExamples.swift; sourceTree = ""; }; + D414D6AB21D0B77F00960935 /* DiscouragedObjectLiteralRuleTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiscouragedObjectLiteralRuleTests.swift; sourceTree = ""; }; D41B57771ED8CEE0007B0470 /* ExtensionAccessModifierRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExtensionAccessModifierRule.swift; sourceTree = ""; }; D41E7E0A1DF9DABB0065259A /* RedundantStringEnumValueRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RedundantStringEnumValueRule.swift; sourceTree = ""; }; D4246D6C1F30D8620097E658 /* PrivateOverFilePrivateRuleConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PrivateOverFilePrivateRuleConfiguration.swift; sourceTree = ""; }; @@ -1177,7 +1179,6 @@ D0D1211A19E87861005E4BAA /* swiftlint */, D0D1216E19E87B05005E4BAA /* SwiftLintFramework */, D0D1217B19E87B05005E4BAA /* SwiftLintFrameworkTests */, - 8FB2AE2D21A1F99200D380F3 /* Frameworks */, ); indentWidth = 4; sourceTree = ""; @@ -1324,6 +1325,7 @@ CCD8B87720559C4A00B75847 /* DisableAllTests.swift */, 62AF35D71F30B183009B11EE /* DiscouragedDirectInitRuleTests.swift */, D48B51221F4F5E4B0068AB98 /* DocumentationTests.swift */, + D414D6AB21D0B77F00960935 /* DiscouragedObjectLiteralRuleTests.swift */, 125CE52E20425EFD001635E5 /* ExplicitTypeInterfaceConfigurationTests.swift */, 12E3D4DB2042729300B3E30E /* ExplicitTypeInterfaceRuleTests.swift */, 02FD8AEE1BFC18D60014BFFB /* ExtendedNSStringTests.swift */, @@ -2089,6 +2091,7 @@ B25DCD101F7EF6DC0028A199 /* MultilineArgumentsRuleTests.swift in Sources */, 3BB47D871C51DE6E00AE6A10 /* CustomRulesTests.swift in Sources */, E812249A1B04F85B001783D2 /* TestHelpers.swift in Sources */, + D414D6AC21D0B77F00960935 /* DiscouragedObjectLiteralRuleTests.swift in Sources */, 3B20CD0C1EB699C20069EF2E /* TypeNameRuleTests.swift in Sources */, CCD8B87920559D1E00B75847 /* DisableAllTests.swift in Sources */, 3B3A9A331EA3DFD90075B121 /* IdentifierNameRuleTests.swift in Sources */, diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index d3d1aabe13..0ce5f208f3 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -260,7 +260,9 @@ extension DiscouragedDirectInitRuleTests { extension DiscouragedObjectLiteralRuleTests { static var allTests: [(String, (DiscouragedObjectLiteralRuleTests) -> () throws -> Void)] = [ - ("testWithDefaultConfiguration", testWithDefaultConfiguration) + ("testWithDefaultConfiguration", testWithDefaultConfiguration), + ("testWithImageLiteral", testWithImageLiteral), + ("testWithColorLiteral", testWithColorLiteral) ] } diff --git a/Tests/SwiftLintFrameworkTests/AutomaticRuleTests.generated.swift b/Tests/SwiftLintFrameworkTests/AutomaticRuleTests.generated.swift index b8e127aea3..620ee666f1 100644 --- a/Tests/SwiftLintFrameworkTests/AutomaticRuleTests.generated.swift +++ b/Tests/SwiftLintFrameworkTests/AutomaticRuleTests.generated.swift @@ -90,12 +90,6 @@ class DiscardedNotificationCenterObserverRuleTests: XCTestCase { } } -class DiscouragedObjectLiteralRuleTests: XCTestCase { - func testWithDefaultConfiguration() { - verifyRule(DiscouragedObjectLiteralRule.description) - } -} - class DiscouragedOptionalBooleanRuleTests: XCTestCase { func testWithDefaultConfiguration() { verifyRule(DiscouragedOptionalBooleanRule.description) diff --git a/Tests/SwiftLintFrameworkTests/DiscouragedObjectLiteralRuleTests.swift b/Tests/SwiftLintFrameworkTests/DiscouragedObjectLiteralRuleTests.swift new file mode 100644 index 0000000000..8137633546 --- /dev/null +++ b/Tests/SwiftLintFrameworkTests/DiscouragedObjectLiteralRuleTests.swift @@ -0,0 +1,38 @@ +import SwiftLintFramework +import XCTest + +class DiscouragedObjectLiteralRuleTests: XCTestCase { + func testWithDefaultConfiguration() { + verifyRule(DiscouragedObjectLiteralRule.description) + } + + func testWithImageLiteral() { + let baseDescription = DiscouragedObjectLiteralRule.description + let nonTriggeringExamples = baseDescription.nonTriggeringExamples + [ + "let color = #colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)" + ] + let triggeringExamples = [ + "let image = ↓#imageLiteral(resourceName: \"image.jpg\")" + ] + + let description = baseDescription.with(nonTriggeringExamples: nonTriggeringExamples, + triggeringExamples: triggeringExamples) + + verifyRule(description, ruleConfiguration: ["image_literal": true, "color_literal": false]) + } + + func testWithColorLiteral() { + let baseDescription = DiscouragedObjectLiteralRule.description + let nonTriggeringExamples = baseDescription.nonTriggeringExamples + [ + "let image = #imageLiteral(resourceName: \"image.jpg\")" + ] + let triggeringExamples = [ + "let color = ↓#colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)" + ] + + let description = baseDescription.with(nonTriggeringExamples: nonTriggeringExamples, + triggeringExamples: triggeringExamples) + + verifyRule(description, ruleConfiguration: ["image_literal": false, "color_literal": true]) + } +}