forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use Sourcery to generate MasterRuleList.swift
- Loading branch information
Showing
6 changed files
with
103 additions
and
77 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,11 @@ | ||
// | ||
// MasterRuleList.swift | ||
// SwiftLint | ||
// | ||
// Created by Scott Hoyt on 12/28/15. | ||
// Copyright © 2015 Realm. All rights reserved. | ||
// | ||
|
||
public let masterRuleList = RuleList(rules: | ||
{% for rule in types.structs where rule.name|hasSuffix:"Rule" or rule.name|hasSuffix:"Rules" %} {{ rule.name }}.self{% if not forloop.last %},{% endif %} | ||
{% endfor %}) |
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,74 @@ | ||
// | ||
// RuleList.swift | ||
// SwiftLint | ||
// | ||
// Created by JP Simard on 5/31/17. | ||
// Copyright © 2017 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public enum RuleListError: Error { | ||
case duplicatedConfigurations(rule: Rule.Type) | ||
} | ||
|
||
public struct RuleList { | ||
public let list: [String: Rule.Type] | ||
private let aliases: [String: String] | ||
|
||
public init(rules: Rule.Type...) { | ||
self.init(rules: rules) | ||
} | ||
|
||
public init(rules: [Rule.Type]) { | ||
var tmpList = [String: Rule.Type]() | ||
var tmpAliases = [String: String]() | ||
|
||
for rule in rules { | ||
let identifier = rule.description.identifier | ||
tmpList[identifier] = rule | ||
for alias in rule.description.deprecatedAliases { | ||
tmpAliases[alias] = identifier | ||
} | ||
tmpAliases[identifier] = identifier | ||
} | ||
list = tmpList | ||
aliases = tmpAliases | ||
} | ||
|
||
internal func configuredRules(with dictionary: [String: Any]) throws -> [Rule] { | ||
var rules = [String: Rule]() | ||
|
||
for (key, configuration) in dictionary { | ||
guard let identifier = identifier(for: key), let ruleType = list[identifier] else { | ||
continue | ||
} | ||
guard rules[identifier] == nil else { | ||
throw RuleListError.duplicatedConfigurations(rule: ruleType) | ||
} | ||
do { | ||
let configuredRule = try ruleType.init(configuration: configuration) | ||
rules[identifier] = configuredRule | ||
} catch { | ||
queuedPrintError("Invalid configuration for '\(identifier)'. Falling back to default.") | ||
rules[identifier] = ruleType.init() | ||
} | ||
} | ||
|
||
for (identifier, ruleType) in list where rules[identifier] == nil { | ||
rules[identifier] = ruleType.init() | ||
} | ||
|
||
return Array(rules.values) | ||
} | ||
|
||
internal func identifier(for alias: String) -> String? { | ||
return aliases[alias] | ||
} | ||
|
||
internal func allValidIdentifiers() -> [String] { | ||
return list.flatMap { (_, rule) -> [String] in | ||
rule.description.allIdentifiers | ||
} | ||
} | ||
} |
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