<commit_before>//
//  ExplicitAssociatedEnumValueRule.swift
//  SwiftLint
//
//  Created by <NAME> on 8/19/17.
//  Copyright © 2017 Realm. All rights reserved.
//

import Foundation
import SourceKittenFramework

public struct ExplicitAssociatedEnumValueRule: ASTRule, OptInRule, ConfigurationProviderRule {
    public var configuration = SeverityConfiguration(.warning)

    public init() {}

    public static let description = RuleDescription(
        identifier: "explicit_associated_enum_value",
        name: "Explicit Associated Enum Value",
        description: "Enums should be explicitly assigned their associated values.",
        kind: .idiomatic,
        nonTriggeringExamples: [
            "enum Numbers {\n case int(Int)\n case short(Int16)\n}\n",
            "enum Numbers: Int {\n case one = 1\n case two = 2\n}\n",
            "enum Numbers: Double {\n case one = 1.1\n case two = 2.2\n}\n",
            "enum Numbers: String {\n case one = \"one\"\n case two = \"two\"\n}\n",
            "enum Numbers: String {\n case one = \"ONE\"\n case two = \"TWO\"\n}\n"
        ],
        triggeringExamples: [
            "enum Numbers: Int {\n case one = 10, ↓two, three = 30\n}\n",
            "enum Numbers: String {\n case ↓one\n case ↓two\n}\n",
            "enum Numbers: String {\n case ↓one, two = \"two\"\n}\n",
            "enum Numbers: String {\n case ↓one, ↓two\n}\n"
        ]
    )

    public func validate(file: File, kind: SwiftDeclarationKind,
                         dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
        guard kind == .enum else {
            return []
        }

        // Check if it's an associated value enum
        guard !dictionary.inheritedTypes.isEmpty else {
            return []
        }

        let violations = violatingOffsetsForEnum(dictionary: dictionary, file: file)
        return violations.map {
            StyleViolation(ruleDescription: type(of: self).description,
                           severity: configuration.severity,
                           location: Location(file: file, byteOffset: $0))
        }
    }

    private func violatingOffsetsForEnum(dictionary: [String: SourceKitRepresentable], file: File) -> [Int] {

        let locs = substructureElements(of: dictionary, matching: .enumcase)
            .flatMap { substructureElements(of: $0, matching: .enumelement) }
            .flatMap(enumElementsMissingInitExpr(_:))
            .flatMap { $0.offset }

        return locs
    }

    private func substructureElements(of dict: [String: SourceKitRepresentable],
                                      matching kind: SwiftDeclarationKind) -> [[String: SourceKitRepresentable]] {
        return dict.substructure
            .filter { $0.kind.flatMap(SwiftDeclarationKind.init(rawValue:)) == kind }
    }

    private func enumElementsMissingInitExpr(
        _ enumElements: [[String: SourceKitRepresentable]]) -> [[String: SourceKitRepresentable]] {

        return enumElements
            .filter { !$0.elements.contains { $0.kind == "source.lang.swift.structure.elem.init_expr" } }
    }
}
<commit_msg>review changes (cleanup params)<commit_after>//
//  ExplicitAssociatedEnumValueRule.swift
//  SwiftLint
//
//  Created by <NAME> on 8/19/17.
//  Copyright © 2017 Realm. All rights reserved.
//

import Foundation
import SourceKittenFramework

public struct ExplicitAssociatedEnumValueRule: ASTRule, OptInRule, ConfigurationProviderRule {
    public var configuration = SeverityConfiguration(.warning)

    public init() {}

    public static let description = RuleDescription(
        identifier: "explicit_associated_enum_value",
        name: "Explicit Associated Enum Value",
        description: "Enums should be explicitly assigned their associated values.",
        kind: .idiomatic,
        nonTriggeringExamples: [
            "enum Numbers {\n case int(Int)\n case short(Int16)\n}\n",
            "enum Numbers: Int {\n case one = 1\n case two = 2\n}\n",
            "enum Numbers: Double {\n case one = 1.1\n case two = 2.2\n}\n",
            "enum Numbers: String {\n case one = \"one\"\n case two = \"two\"\n}\n",
            "enum Numbers: String {\n case one = \"ONE\"\n case two = \"TWO\"\n}\n"
        ],
        triggeringExamples: [
            "enum Numbers: Int {\n case one = 10, ↓two, three = 30\n}\n",
            "enum Numbers: String {\n case ↓one\n case ↓two\n}\n",
            "enum Numbers: String {\n case ↓one, two = \"two\"\n}\n",
            "enum Numbers: String {\n case ↓one, ↓two\n}\n"
        ]
    )

    public func validate(file: File, kind: SwiftDeclarationKind,
                         dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
        guard kind == .enum else {
            return []
        }

        // Check if it's an associated value enum
        guard !dictionary.inheritedTypes.isEmpty else {
            return []
        }

        let violations = violatingOffsetsForEnum(dictionary: dictionary, file: file)
        return violations.map {
            StyleViolation(ruleDescription: type(of: self).description,
                           severity: configuration.severity,
                           location: Location(file: file, byteOffset: $0))
        }
    }

    private func violatingOffsetsForEnum(dictionary: [String: SourceKitRepresentable], file: File) -> [Int] {

        let locs = substructureElements(of: dictionary, matching: .enumcase)
            .flatMap { substructureElements(of: $0, matching: .enumelement) }
            .flatMap(enumElementsMissingInitExpr)
            .flatMap { $0.offset }

        return locs
    }

    private func substructureElements(of dict: [String: SourceKitRepresentable],
                                      matching kind: SwiftDeclarationKind) -> [[String: SourceKitRepresentable]] {
        return dict.substructure
            .filter { $0.kind.flatMap(SwiftDeclarationKind.init) == kind }
    }

    private func enumElementsMissingInitExpr(
        _ enumElements: [[String: SourceKitRepresentable]]) -> [[String: SourceKitRepresentable]] {

        return enumElements
            .filter { !$0.elements.contains { $0.kind == "source.lang.swift.structure.elem.init_expr" } }
    }
}

{'fraction_non_alphanumeric': 0.11689351481184948, 'fraction_numerical': 0.008646917534027222, 'mean_word_length': 2.2735849056603774, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2144230', 'n_tokens_mistral': 1810, 'n_tokens_neox': 1684, 'n_words': 471}