<commit_before>//
//  Configuration.swift
//  SwiftLint
//
//  Created by <NAME> on 8/23/15.
//  Copyright © 2015 Realm. All rights reserved.
//

import Foundation
import SourceKittenFramework

private let fileManager = FileManager.default

private enum ConfigurationKey: String {
    case disabledRules = "disabled_rules"
    case enabledRules = "enabled_rules" // deprecated in favor of OptInRules
    case excluded = "excluded"
    case included = "included"
    case optInRules = "opt_in_rules"
    case reporter = "reporter"
    case useNestedConfigs = "use_nested_configs" // deprecated
    case whitelistRules = "whitelist_rules"
    case warningThreshold = "warning_threshold"
}

public struct Configuration: Equatable {
    public static let fileName = ".swiftlint.yml"
    public let included: [String]             // included
    public let excluded: [String]             // excluded
    public let reporter: String               // reporter (xcode, json, csv, checkstyle)
    public var warningThreshold: Int?         // warning threshold
    public let rules: [Rule]
    public var rootPath: String?              // the root path to search for nested configurations
    public var configurationPath: String?     // if successfully loaded from a path

    public init?(disabledRules: [String] = [],
                 optInRules: [String] = [],
                 whitelistRules: [String] = [],
                 included: [String] = [],
                 excluded: [String] = [],
                 warningThreshold: Int? = nil,
                 reporter: String = XcodeReporter.identifier,
                 ruleList: RuleList = masterRuleList,
                 configuredRules: [Rule]? = nil) {

        self.included = included
        self.excluded = excluded
        self.reporter = reporter

        let configuredRules = configuredRules
            ?? (try? ruleList.configuredRules(with: [:]))
            ?? []

        let handleAliasWithRuleList = { (alias: String) -> String in
            return ruleList.identifier(for: alias) ?? alias
        }

        let disabledRules = disabledRules.map(handleAliasWithRuleList)
        let optInRules = optInRules.map(handleAliasWithRuleList)
        let whitelistRules = whitelistRules.map(handleAliasWithRuleList)

        // Validate that all rule identifiers map to a defined rule
        let validRuleIdentifiers = validateRuleIdentifiers(configuredRules: configuredRules,
                                                           disabledRules: disabledRules)
        let validDisabledRules = disabledRules.filter { validRuleIdentifiers.contains($0) }

        // Validate that rule identifiers aren't listed multiple times
        if containsDuplicatedRuleIdentifiers(validDisabledRules) {
            return nil
        }

        // set the config threshold to the threshold provided in the config file
        self.warningThreshold = warningThreshold<commit_msg>pass function reference rather than create a small closure<commit_after>//
//  Configuration.swift
//  SwiftLint
//
//  Created by <NAME> on 8/23/15.
//  Copyright © 2015 Realm. All rights reserved.
//

import Foundation
import SourceKittenFramework

private let fileManager = FileManager.default

private enum ConfigurationKey: String {
    case disabledRules = "disabled_rules"
    case enabledRules = "enabled_rules" // deprecated in favor of OptInRules
    case excluded = "excluded"
    case included = "included"
    case optInRules = "opt_in_rules"
    case reporter = "reporter"
    case useNestedConfigs = "use_nested_configs" // deprecated
    case whitelistRules = "whitelist_rules"
    case warningThreshold = "warning_threshold"
}

public struct Configuration: Equatable {
    public static let fileName = ".swiftlint.yml"
    public let included: [String]             // included
    public let excluded: [String]             // excluded
    public let reporter: String               // reporter (xcode, json, csv, checkstyle)
    public var warningThreshold: Int?         // warning threshold
    public let rules: [Rule]
    public var rootPath: String?              // the root path to search for nested configurations
    public var configurationPath: String?     // if successfully loaded from a path

    public init?(disabledRules: [String] = [],
                 optInRules: [String] = [],
                 whitelistRules: [String] = [],
                 included: [String] = [],
                 excluded: [String] = [],
                 warningThreshold: Int? = nil,
                 reporter: String = XcodeReporter.identifier,
                 ruleList: RuleList = masterRuleList,
                 configuredRules: [Rule]? = nil) {

        self.included = included
        self.excluded = excluded
        self.reporter = reporter

        let configuredRules = configuredRules
            ?? (try? ruleList.configuredRules(with: [:]))
            ?? []

        let handleAliasWithRuleList = { (alias: String) -> String in
            return ruleList.identifier(for: alias) ?? alias
        }

        let disabledRules = disabledRules.map(handleAliasWithRuleList)
        let optInRules = optInRules.map(handleAliasWithRuleList)
        let whitelistRules = whitelistRules.map(handleAliasWithRuleList)

        // Validate that all rule identifiers map to a defined rule
        let validRuleIdentifiers = validateRuleIdentifiers(configuredRules: configuredRules,
                                                           disabledRules: disabledRules)
        let validDisabledRules = disabledRules.filter(validRuleIdentifiers.contains)

        // Validate that rule identifiers aren't listed multiple times
        if containsDuplicatedRuleIdentifiers(validDisabledRules) {
            return nil
        }

        // set the config threshold to the threshold provided in the config file
        self.warningThreshold = warningThreshold
{'fraction_non_alphanumeric': 0.07847457627118644, 'fraction_numerical': 0.0032203389830508474, 'mean_word_length': 2.3604783599088837, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2885967', 'n_tokens_mistral': 1444, 'n_tokens_neox': 1397, 'n_words': 444}