Skip to content

Commit

Permalink
Add configuration to discouraged_object_literal
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelofabri authored and sjavora committed Mar 9, 2019
1 parent 51caf3e commit 1dfc2b6
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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() {}

Expand Down Expand Up @@ -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))]
}
}
5 changes: 4 additions & 1 deletion SwiftLint.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -668,6 +669,7 @@
D40FE89C1F867BFF006433E2 /* OverrideInExtensionRule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OverrideInExtensionRule.swift; sourceTree = "<group>"; };
D4130D961E16183F00242361 /* IdentifierNameRuleExamples.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IdentifierNameRuleExamples.swift; sourceTree = "<group>"; };
D4130D981E16CC1300242361 /* TypeNameRuleExamples.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TypeNameRuleExamples.swift; sourceTree = "<group>"; };
D414D6AB21D0B77F00960935 /* DiscouragedObjectLiteralRuleTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiscouragedObjectLiteralRuleTests.swift; sourceTree = "<group>"; };
D41B57771ED8CEE0007B0470 /* ExtensionAccessModifierRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExtensionAccessModifierRule.swift; sourceTree = "<group>"; };
D41E7E0A1DF9DABB0065259A /* RedundantStringEnumValueRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RedundantStringEnumValueRule.swift; sourceTree = "<group>"; };
D4246D6C1F30D8620097E658 /* PrivateOverFilePrivateRuleConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PrivateOverFilePrivateRuleConfiguration.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1177,7 +1179,6 @@
D0D1211A19E87861005E4BAA /* swiftlint */,
D0D1216E19E87B05005E4BAA /* SwiftLintFramework */,
D0D1217B19E87B05005E4BAA /* SwiftLintFrameworkTests */,
8FB2AE2D21A1F99200D380F3 /* Frameworks */,
);
indentWidth = 4;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down
4 changes: 3 additions & 1 deletion Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,9 @@ extension DiscouragedDirectInitRuleTests {

extension DiscouragedObjectLiteralRuleTests {
static var allTests: [(String, (DiscouragedObjectLiteralRuleTests) -> () throws -> Void)] = [
("testWithDefaultConfiguration", testWithDefaultConfiguration)
("testWithDefaultConfiguration", testWithDefaultConfiguration),
("testWithImageLiteral", testWithImageLiteral),
("testWithColorLiteral", testWithColorLiteral)
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,6 @@ class DiscardedNotificationCenterObserverRuleTests: XCTestCase {
}
}

class DiscouragedObjectLiteralRuleTests: XCTestCase {
func testWithDefaultConfiguration() {
verifyRule(DiscouragedObjectLiteralRule.description)
}
}

class DiscouragedOptionalBooleanRuleTests: XCTestCase {
func testWithDefaultConfiguration() {
verifyRule(DiscouragedOptionalBooleanRule.description)
Expand Down
Original file line number Diff line number Diff line change
@@ -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])
}
}

0 comments on commit 1dfc2b6

Please sign in to comment.