mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
* 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.
93 lines
3.8 KiB
Swift
93 lines
3.8 KiB
Swift
import Foundation
|
|
import SourceKittenFramework
|
|
|
|
public struct ExplicitTopLevelACLRule: OptInRule, ConfigurationProviderRule, AutomaticTestableRule {
|
|
public var configuration = SeverityConfiguration(.warning)
|
|
|
|
public init() {}
|
|
|
|
public static let description = RuleDescription(
|
|
identifier: "explicit_top_level_acl",
|
|
name: "Explicit Top Level ACL",
|
|
description: "Top-level declarations should specify Access Control Level keywords explicitly.",
|
|
kind: .idiomatic,
|
|
nonTriggeringExamples: [
|
|
Example("internal enum A {}\n"),
|
|
Example("public final class B {}\n"),
|
|
Example("private struct C {}\n"),
|
|
Example("internal enum A {\n enum B {}\n}"),
|
|
Example("internal final class Foo {}"),
|
|
Example("internal\nclass Foo {}"),
|
|
Example("internal func a() {}\n"),
|
|
Example("extension A: Equatable {}"),
|
|
Example("extension A {}")
|
|
],
|
|
triggeringExamples: [
|
|
Example("enum A {}\n"),
|
|
Example("final class B {}\n"),
|
|
Example("struct C {}\n"),
|
|
Example("func a() {}\n"),
|
|
Example("internal let a = 0\nfunc b() {}\n")
|
|
]
|
|
)
|
|
|
|
public func validate(file: SwiftLintFile) -> [StyleViolation] {
|
|
let extensionKinds: Set<SwiftDeclarationKind> = [.extension, .extensionClass, .extensionEnum,
|
|
.extensionProtocol, .extensionStruct]
|
|
|
|
// find all top-level types marked as internal (either explictly or implictly)
|
|
let dictionary = file.structureDictionary
|
|
let internalTypesOffsets = dictionary.substructure.compactMap { element -> ByteCount? in
|
|
// ignore extensions
|
|
guard let kind = element.declarationKind,
|
|
!extensionKinds.contains(kind) else {
|
|
return nil
|
|
}
|
|
|
|
if element.accessibility == .internal {
|
|
return element.offset
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
guard !internalTypesOffsets.isEmpty else {
|
|
return []
|
|
}
|
|
|
|
// find all "internal" tokens
|
|
let contents = file.stringView
|
|
let allInternalRanges = file.match(pattern: "internal", with: [.attributeBuiltin]).compactMap {
|
|
contents.NSRangeToByteRange(start: $0.location, length: $0.length)
|
|
}
|
|
|
|
let violationOffsets = internalTypesOffsets.filter { typeOffset in
|
|
// find the last "internal" token before the type
|
|
guard let previousInternalByteRange = lastInternalByteRange(before: typeOffset,
|
|
in: allInternalRanges) else {
|
|
// didn't find a candidate token, so we are sure it's a violation
|
|
return true
|
|
}
|
|
|
|
// the "internal" token correspond to the type if there're only
|
|
// attributeBuiltin (`final` for example) tokens between them
|
|
let length = typeOffset - previousInternalByteRange.location
|
|
let range = ByteRange(location: previousInternalByteRange.location, length: length)
|
|
let internalDoesntBelongToType = Set(file.syntaxMap.kinds(inByteRange: range)) != [.attributeBuiltin]
|
|
|
|
return internalDoesntBelongToType
|
|
}
|
|
|
|
return violationOffsets.map {
|
|
StyleViolation(ruleDescription: type(of: self).description,
|
|
severity: configuration.severity,
|
|
location: Location(file: file, byteOffset: $0))
|
|
}
|
|
}
|
|
|
|
private func lastInternalByteRange(before typeOffset: ByteCount, in ranges: [ByteRange]) -> ByteRange? {
|
|
let firstPartition = ranges.prefix(while: { typeOffset > $0.location })
|
|
return firstPartition.last
|
|
}
|
|
}
|