-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
14 changed files
with
275 additions
and
83 deletions.
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
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
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 = "\n".join(validRuleIdentifiers) | ||
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.