mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
fcf848608e
* Add Example wrapper in order to display test failures inline when running in Xcode. * Stop using Swift 5.1-only features so we can compile on Xcode 10.2. * Wrap strings in Example. * Add Changelog entry. * Wrap all examples in Example struct. * Better and more complete capturing of line numbers. * Fix broken test. * Better test traceability. * Address or disable linting warnings. * Add documentation comments. * Disable linter for a few cases. * Limit mutability and add copy-and-mutate utility functions. * Limit scope of mutability.
109 lines
4.1 KiB
Swift
109 lines
4.1 KiB
Swift
import SourceKittenFramework
|
|
|
|
private extension SwiftLintFile {
|
|
func missingDocOffsets(in dictionary: SourceKittenDictionary,
|
|
acls: [AccessControlLevel]) -> [(ByteCount, AccessControlLevel)] {
|
|
if dictionary.enclosedSwiftAttributes.contains(.override) ||
|
|
!dictionary.inheritedTypes.isEmpty {
|
|
return []
|
|
}
|
|
let substructureOffsets = dictionary.substructure.flatMap {
|
|
missingDocOffsets(in: $0, acls: acls)
|
|
}
|
|
let extensionKinds: Set<SwiftDeclarationKind> = [.extension, .extensionEnum, .extensionClass,
|
|
.extensionStruct, .extensionProtocol]
|
|
guard let kind = dictionary.declarationKind,
|
|
!extensionKinds.contains(kind),
|
|
case let isDeinit = kind == .functionMethodInstance && dictionary.name == "deinit",
|
|
!isDeinit,
|
|
let offset = dictionary.offset,
|
|
let acl = dictionary.accessibility,
|
|
acls.contains(acl) else {
|
|
return substructureOffsets
|
|
}
|
|
if dictionary.docLength != nil {
|
|
return substructureOffsets
|
|
}
|
|
return substructureOffsets + [(offset, acl)]
|
|
}
|
|
}
|
|
|
|
public struct MissingDocsRule: OptInRule, ConfigurationProviderRule, AutomaticTestableRule {
|
|
public init() {
|
|
configuration = MissingDocsRuleConfiguration(
|
|
parameters: [RuleParameter<AccessControlLevel>(severity: .warning, value: .open),
|
|
RuleParameter<AccessControlLevel>(severity: .warning, value: .public)])
|
|
}
|
|
|
|
public typealias ConfigurationType = MissingDocsRuleConfiguration
|
|
public var configuration: MissingDocsRuleConfiguration
|
|
|
|
public static let description = RuleDescription(
|
|
identifier: "missing_docs",
|
|
name: "Missing Docs",
|
|
description: "Declarations should be documented.",
|
|
kind: .lint,
|
|
minSwiftVersion: .fourDotOne,
|
|
nonTriggeringExamples: [
|
|
// locally-defined superclass member is documented, but subclass member is not
|
|
Example("""
|
|
/// docs
|
|
public class A {
|
|
/// docs
|
|
public func b() {}
|
|
}
|
|
/// docs
|
|
public class B: A { override public func b() {} }
|
|
"""),
|
|
// externally-defined superclass member is documented, but subclass member is not
|
|
Example("""
|
|
import Foundation
|
|
/// docs
|
|
public class B: NSObject {
|
|
// no docs
|
|
override public var description: String { fatalError() } }
|
|
"""),
|
|
Example("""
|
|
/// docs
|
|
public class A {
|
|
deinit {}
|
|
}
|
|
"""),
|
|
Example("""
|
|
public extension A {}
|
|
""")
|
|
],
|
|
triggeringExamples: [
|
|
// public, undocumented
|
|
Example("public func a() {}\n"),
|
|
// public, undocumented
|
|
Example("// regular comment\npublic func a() {}\n"),
|
|
// public, undocumented
|
|
Example("/* regular comment */\npublic func a() {}\n"),
|
|
// protocol member and inherited member are both undocumented
|
|
Example("""
|
|
/// docs
|
|
public protocol A {
|
|
// no docs
|
|
var b: Int { get } }
|
|
/// docs
|
|
public struct C: A {
|
|
|
|
public let b: Int
|
|
}
|
|
""")
|
|
]
|
|
)
|
|
|
|
public func validate(file: SwiftLintFile) -> [StyleViolation] {
|
|
let acls = configuration.parameters.map { $0.value }
|
|
let dict = file.structureDictionary
|
|
return file.missingDocOffsets(in: dict, acls: acls).map { offset, acl in
|
|
StyleViolation(ruleDescription: type(of: self).description,
|
|
severity: configuration.parameters.first { $0.value == acl }?.severity ?? .warning,
|
|
location: Location(file: file, byteOffset: offset),
|
|
reason: "\(acl.description) declarations should be documented.")
|
|
}
|
|
}
|
|
}
|