-
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.
Merge pull request #1888 from marcelofabri/override-extension
Add override_in_extension rule
- Loading branch information
Showing
9 changed files
with
227 additions
and
46 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
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
64 changes: 64 additions & 0 deletions
64
Source/SwiftLintFramework/Helpers/NamespaceCollector.swift
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,64 @@ | ||
// | ||
// NamespaceCollector.swift | ||
// SwiftLint | ||
// | ||
// Created by Marcelo Fabri on 10/07/17. | ||
// Copyright © 2017 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SourceKittenFramework | ||
|
||
struct NamespaceCollector { | ||
struct Element { | ||
let name: String | ||
let kind: SwiftDeclarationKind | ||
let offset: Int | ||
let dictionary: [String: SourceKitRepresentable] | ||
|
||
init?(dictionary: [String: SourceKitRepresentable], namespace: [String]) { | ||
guard let name = dictionary.name, | ||
let kind = dictionary.kind.flatMap(SwiftDeclarationKind.init), | ||
let offset = dictionary.offset else { | ||
return nil | ||
} | ||
|
||
self.name = (namespace + [name]).joined(separator: ".") | ||
self.kind = kind | ||
self.offset = offset | ||
self.dictionary = dictionary | ||
} | ||
} | ||
|
||
private let dictionary: [String: SourceKitRepresentable] | ||
|
||
init(dictionary: [String: SourceKitRepresentable]) { | ||
self.dictionary = dictionary | ||
} | ||
|
||
func findAllElements(of types: Set<SwiftDeclarationKind>, | ||
namespace: [String] = []) -> [Element] { | ||
return findAllElements(in: dictionary, of: types, namespace: namespace) | ||
} | ||
|
||
private func findAllElements(in dictionary: [String: SourceKitRepresentable], | ||
of types: Set<SwiftDeclarationKind>, | ||
namespace: [String] = []) -> [Element] { | ||
|
||
return dictionary.substructure.flatMap { subDict -> [Element] in | ||
|
||
var elements: [Element] = [] | ||
guard let element = Element(dictionary: subDict, namespace: namespace) else { | ||
return elements | ||
} | ||
|
||
if types.contains(element.kind) { | ||
elements.append(element) | ||
} | ||
|
||
elements += findAllElements(in: subDict, of: types, namespace: [element.name]) | ||
|
||
return elements | ||
} | ||
} | ||
} |
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
68 changes: 68 additions & 0 deletions
68
Source/SwiftLintFramework/Rules/OverrideInExtensionRule.swift
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,68 @@ | ||
// | ||
// OverrideInExtensionRule.swift | ||
// SwiftLint | ||
// | ||
// Created by Marcelo Fabri on 10/05/17. | ||
// Copyright © 2017 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public struct OverrideInExtensionRule: ConfigurationProviderRule, OptInRule { | ||
public var configuration = SeverityConfiguration(.warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "override_in_extension", | ||
name: "Override in Extension", | ||
description: "Extensions shouldn't override declarations.", | ||
kind: .lint, | ||
nonTriggeringExamples: [ | ||
"extension Person {\n var age: Int { return 42 }\n}\n", | ||
"extension Person {\n func celebrateBirthday() {}\n}\n", | ||
"class Employee: Person {\n override func celebrateBirthday() {}\n}\n", | ||
"class Foo: NSObject {}\n" + | ||
"extension Foo {\n" + | ||
" override var description: String { return \"\" }\n" + | ||
"}\n", | ||
"struct Foo {\n" + | ||
" class Bar: NSObject {}\n" + | ||
"}\n" + | ||
"extension Foo.Bar {\n" + | ||
" override var description: String { return \"\" }\n" + | ||
"}\n" | ||
], | ||
triggeringExamples: [ | ||
"extension Person {\n override ↓var age: Int { return 42 }\n}\n", | ||
"extension Person {\n override ↓func celebrateBirthday() {}\n}\n" | ||
] | ||
) | ||
|
||
public func validate(file: File) -> [StyleViolation] { | ||
let collector = NamespaceCollector(dictionary: file.structure.dictionary) | ||
let elements = collector.findAllElements(of: [.class, .struct, .enum, .extension]) | ||
|
||
let susceptibleNames = Set(elements.flatMap { $0.kind == .class ? $0.name : nil }) | ||
|
||
return elements | ||
.filter { $0.kind == .extension && !susceptibleNames.contains($0.name) } | ||
.flatMap { element in | ||
return element.dictionary.substructure.flatMap { element -> Int? in | ||
guard element.kind.flatMap(SwiftDeclarationKind.init) != nil, | ||
element.enclosedSwiftAttributes.contains("source.decl.attribute.override"), | ||
let offset = element.offset else { | ||
return nil | ||
} | ||
|
||
return offset | ||
} | ||
} | ||
.map { | ||
StyleViolation(ruleDescription: type(of: self).description, | ||
severity: configuration.severity, | ||
location: Location(file: file, byteOffset: $0)) | ||
} | ||
} | ||
} |
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