-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Configure SwiftLint via a YAML file #99
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
included: | ||
- Source |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
github "Carthage/Commandant" "swift-2.0" | ||
github "jspahrsummers/xcconfigs" >= 0.8 | ||
github "jpsim/YamlSwift" "master" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
github "antitypical/Result" "0.6-beta.1" | ||
github "drmohundro/SWXMLHash" "050154bfc56032f04fcca37464693a17f897200a" | ||
github "jpsim/SwiftXPC" "d32e70f1b35cfa833be85fd40e70401f4190f5b0" | ||
github "jpsim/YamlSwift" "ed060df018b5693a045f207088b95ce15f433065" | ||
github "jspahrsummers/xcconfigs" "0.8.1" | ||
github "Carthage/Commandant" "40f503c33121431dc098adb7c44d9496e3a1de2f" | ||
github "jpsim/SourceKitten" "ace729472170a4ec2996b8f921fc66784e5ef4fe" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// | ||
// Configuration.swift | ||
// SwiftLint | ||
// | ||
// Created by JP Simard on 2015-08-23. | ||
// Copyright (c) 2015 Realm. All rights reserved. | ||
// | ||
|
||
import Yaml | ||
|
||
extension Yaml { | ||
var arrayOfStrings: [Swift.String]? { | ||
return array?.flatMap { $0.string } | ||
} | ||
} | ||
|
||
public struct Configuration { | ||
public let disabledRules: [String] // disabled_rules | ||
public let included: [String] // included | ||
public let excluded: [String] // excluded | ||
|
||
public var rules: [Rule] { | ||
return allRules.filter { !disabledRules.contains($0.identifier) } | ||
} | ||
|
||
public init?(disabledRules: [String] = [], included: [String] = [], excluded: [String] = []) { | ||
self.disabledRules = disabledRules | ||
self.included = included | ||
self.excluded = excluded | ||
|
||
// Validate that all rule identifiers map to a defined rule | ||
|
||
let validRuleIdentifiers = allRules.map { $0.identifier } | ||
|
||
let ruleSet = Set(disabledRules) | ||
let invalidRules = ruleSet.filter({ !validRuleIdentifiers.contains($0) }) | ||
if invalidRules.count > 0 { | ||
for invalidRule in invalidRules { | ||
fputs("config error: '\(invalidRule)' is not a valid rule identifier\n", stderr) | ||
let listOfValidRuleIdentifiers = validRuleIdentifiers.joinWithSeparator("\n") | ||
fputs("Valid rule identifiers:\n\(listOfValidRuleIdentifiers)\n", stderr) | ||
} | ||
return nil | ||
} | ||
|
||
// Validate that rule identifiers aren't listed multiple times | ||
|
||
if ruleSet.count != disabledRules.count { | ||
let duplicateRules = disabledRules.reduce([String: Int]()) { (var accu, element) in | ||
accu[element] = accu[element]?.successor() ?? 1 | ||
return accu | ||
}.filter { | ||
$0.1 > 1 | ||
} | ||
for duplicateRule in duplicateRules { | ||
fputs("config error: '\(duplicateRule.0)' is listed \(duplicateRule.1) times\n", | ||
stderr) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
public init?(yaml: String) { | ||
guard let yamlConfig = Yaml.load(yaml).value else { | ||
return nil | ||
} | ||
self.init( | ||
disabledRules: yamlConfig["disabled_rules"].arrayOfStrings ?? [], | ||
included: yamlConfig["included"].arrayOfStrings ?? [], | ||
excluded: yamlConfig["excluded"].arrayOfStrings ?? [] | ||
) | ||
} | ||
|
||
public init(path: String = ".swiftlint.yml", optional: Bool = true) { | ||
let fullPath = (path as NSString).absolutePathRepresentation() | ||
let failIfRequired = { | ||
if !optional { fatalError("Could not read configuration file at path '\(fullPath)'") } | ||
} | ||
if path.isEmpty { | ||
failIfRequired() | ||
self.init()! | ||
} else { | ||
if !NSFileManager.defaultManager().fileExistsAtPath(fullPath) { | ||
failIfRequired() | ||
self.init()! | ||
return | ||
} | ||
do { | ||
let yamlContents = try NSString(contentsOfFile: fullPath, | ||
encoding: NSUTF8StringEncoding) as String | ||
if let _ = Configuration(yaml: yamlContents) { | ||
print("Loading configuration from '\(path)'") | ||
self.init(yaml: yamlContents)! | ||
} else { | ||
self.init()! | ||
} | ||
} catch { | ||
failIfRequired() | ||
self.init()! | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// ConfigurationTests.swift | ||
// SwiftLint | ||
// | ||
// Created by JP Simard on 8/23/15. | ||
// Copyright © 2015 Realm. All rights reserved. | ||
// | ||
|
||
import SwiftLintFramework | ||
import XCTest | ||
|
||
class ConfigurationTests: XCTestCase { | ||
func testInit() { | ||
XCTAssert(Configuration(yaml: "") != nil, | ||
"initializing Configuration with empty YAML string should succeed") | ||
XCTAssert(Configuration(yaml: "a: 1\nb: 2") != nil, | ||
"initializing Configuration with valid YAML string should succeed") | ||
XCTAssert(Configuration(yaml: "|\na") == nil, | ||
"initializing Configuration with invalid YAML string should fail") | ||
} | ||
|
||
func testEmptyConfiguration() { | ||
guard let config = Configuration(yaml: "") else { | ||
XCTFail("empty YAML string should yield non-nil Configuration") | ||
return | ||
} | ||
XCTAssertEqual(config.disabledRules, []) | ||
XCTAssertEqual(config.included, []) | ||
XCTAssertEqual(config.excluded, []) | ||
} | ||
|
||
func testDisabledRules() { | ||
XCTAssert(Configuration(yaml: "disabled_rules:\n - a") == nil, | ||
"initializing Configuration with invalid rules in YAML string should fail") | ||
let disabledConfig = Configuration(yaml: "disabled_rules:\n - nesting\n - todo")! | ||
XCTAssertEqual(disabledConfig.disabledRules, | ||
["nesting", "todo"], | ||
"initializing Configuration with valid rules in YAML string should succeed") | ||
let expectedIdentifiers = allRules | ||
.map({ $0.identifier }) | ||
.filter({ !["nesting", "todo"].contains($0) }) | ||
let configuredIdentifiers = disabledConfig.rules.map({ $0.identifier }) | ||
XCTAssertEqual(expectedIdentifiers, configuredIdentifiers) | ||
|
||
// Duplicate | ||
let duplicateConfig = Configuration( yaml: "disabled_rules:\n - todo\n - todo") | ||
XCTAssert(duplicateConfig == nil, "initializing Configuration with duplicate rules in " + | ||
" YAML string should fail") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this print some error about an invalid config?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see this error below