<commit_before>//
//  ClassDelegateProtocolRule.swift
//  SwiftLint
//
//  Created by <NAME> on 12/23/16.
//  Copyright © 2016 Realm. All rights reserved.
//

import Foundation
import SourceKittenFramework

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

    public init() {}

    public static let description = RuleDescription(
        identifier: "class_delegate_protocol",
        name: "Class Delegate Protocol",
        description: "Delegate protocols should be class-only so they can be weakly referenced.",
        nonTriggeringExamples: [
            "protocol FooDelegate: class {}\n",
            "protocol FooDelegate: class, BarDelegate {}\n",
            "protocol Foo {}\n",
            "class FooDelegate {}\n",
            "@objc protocol FooDelegate {}\n",
            "@objc(MyFooDelegate)\n protocol FooDelegate {}\n",
            "protocol FooDelegate: BarDelegate {}\n"
        ],
        triggeringExamples: [
            "↓protocol FooDelegate {}\n",
            "↓protocol FooDelegate: Bar {}\n"
        ]
    )

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

        // Check if name contains "Delegate"
        guard let name = dictionary.name, isDelegateProtocol(name) else {
            return []
        }

        // Check if @objc
        let objcAttributes: Set<String> = ["source.decl.attribute.objc",
                                           "source.decl.attribute.objc.name"]
        let isObjc = !objcAttributes.intersection(dictionary.enclosedSwiftAttributes).isEmpty
        guard !isObjc else {
            return []
        }

        // Check if inherits from another Delegate protocol
        guard dictionary.inheritedTypes.filter(isDelegateProtocol).isEmpty else {
            return []
        }

        // Check if : class
        guard let offset = dictionary.offset,
            let nameOffset = dictionary.nameOffset,
            let nameLength = dictionary.nameLength,
            let bodyOffset = dictionary.bodyOffset,
            case let contents = file.contents.bridge(),
            case let start = nameOffset + nameLength,
            let range = contents.byteRangeToNSRange(start: start, length: bodyOffset - start),
            !isClassProtocol(file: file, range: range) else {
            return []
        }

        return [
            StyleViolation(ruleDescription: type(of: self).description,
                           severity: configuration.severity,
                           location: Location(file: file, byteOffset: offset))
        ]
    }

    private func isClassProtocol(file: File, range: NSRange) -> Bool {
        return !file.match(pattern: "\\bclass\\b", with: [.keyword], range: range).isEmpty
    }

    private func isDelegateProtocol(_ name: String) -> Bool {
        return name.hasSuffix("Delegate")
    }

}
<commit_msg>Update ClassDelegateProtocolRule to accept AnyObject and NSObjectProtocol.<commit_after>//
//  ClassDelegateProtocolRule.swift
//  SwiftLint
//
//  Created by <NAME> on 12/23/16.
//  Copyright © 2016 Realm. All rights reserved.
//

import Foundation
import SourceKittenFramework

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

    public init() {}

    public static let description = RuleDescription(
        identifier: "class_delegate_protocol",
        name: "Class Delegate Protocol",
        description: "Delegate protocols should be class-only so they can be weakly referenced.",
        nonTriggeringExamples: [
            "protocol FooDelegate: class {}\n",
            "protocol FooDelegate: class, BarDelegate {}\n",
            "protocol Foo {}\n",
            "class FooDelegate {}\n",
            "@objc protocol FooDelegate {}\n",
            "@objc(MyFooDelegate)\n protocol FooDelegate {}\n",
            "protocol FooDelegate: BarDelegate {}\n",
            "protocol FooDelegate: AnyObject {}\n",
            "protocol FooDelegate: NSObjectProtocol {}\n"
        ],
        triggeringExamples: [
            "↓protocol FooDelegate {}\n",
            "↓protocol FooDelegate: Bar {}\n"
        ]
    )

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

        // Check if name contains "Delegate"
        guard let name = dictionary.name, isDelegateProtocol(name) else {
            return []
        }

        // Check if @objc
        let objcAttributes: Set<String> = ["source.decl.attribute.objc",
                                           "source.decl.attribute.objc.name"]
        let isObjc = !objcAttributes.intersection(dictionary.enclosedSwiftAttributes).isEmpty
        guard !isObjc else {
            return []
        }

        // Check if inherits from another Delegate protocol
        guard dictionary.inheritedTypes.filter(isDelegateProtocol).isEmpty else {
            return []
        }

        // Check if : class
        guard let offset = dictionary.offset,
            let nameOffset = dictionary.nameOffset,
            let nameLength = dictionary.nameLength,
            let bodyOffset = dictionary.bodyOffset,
            case let contents = file.contents.bridge(),
            case let start = nameOffset + nameLength,
            let range = contents.byteRangeToNSRange(start: start, length: bodyOffset - start),
            !(isClassProtocol(file: file, range: range) || isReferenceProtocol(file: file, range: range)) else {
            return []
        }

        return [
            StyleViolation(ruleDescription: type(of: self).description,
                           severity: configuration.severity,
                           location: Location(file: file, byteOffset: offset))
        ]
    }

    private func isClassProtocol(file: File, range: NSRange) -> Bool {
        return !file.match(pattern: "\\bclass\\b", with: [.keyword], range: range).isEmpty
    }
    
    private func isReferenceProtocol(file: File, range: NSRange) -> Bool {
        return !file.match(pattern: "\\b(AnyObject|NSObjectProtocol)\\b", with: [.typeidentifier], range: range).isEmpty
    }

    private func isDelegateProtocol(_ name: String) -> Bool {
        return name.hasSuffix("Delegate")
    }

}

{'fraction_non_alphanumeric': 0.10142079806529625, 'fraction_numerical': 0.003022974607013301, 'mean_word_length': 2.1345333964945525, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 14, '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': '1217410', 'n_tokens_mistral': 1714, 'n_tokens_neox': 1627, 'n_words': 458}