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.
78 lines
2.7 KiB
Swift
78 lines
2.7 KiB
Swift
import SourceKittenFramework
|
|
|
|
public struct PrefixedTopLevelConstantRule: ASTRule, OptInRule, ConfigurationProviderRule {
|
|
public var configuration = PrefixedConstantRuleConfiguration(onlyPrivateMembers: false)
|
|
|
|
private let topLevelPrefix = "k"
|
|
|
|
public init() {}
|
|
|
|
public static let description = RuleDescription(
|
|
identifier: "prefixed_toplevel_constant",
|
|
name: "Prefixed Top-Level Constant",
|
|
description: "Top-level constants should be prefixed by `k`.",
|
|
kind: .style,
|
|
nonTriggeringExamples: [
|
|
Example("private let kFoo = 20.0"),
|
|
Example("public let kFoo = false"),
|
|
Example("internal let kFoo = \"Foo\""),
|
|
Example("let kFoo = true"),
|
|
Example("struct Foo {\n" +
|
|
" let bar = 20.0\n" +
|
|
"}"),
|
|
Example("private var foo = 20.0"),
|
|
Example("public var foo = false"),
|
|
Example("internal var foo = \"Foo\""),
|
|
Example("var foo = true"),
|
|
Example("var foo = true, bar = true"),
|
|
Example("var foo = true, let kFoo = true"),
|
|
Example("let\n" +
|
|
" kFoo = true"),
|
|
Example("var foo: Int {\n" +
|
|
" return a + b\n" +
|
|
"}"),
|
|
Example("let kFoo = {\n" +
|
|
" return a + b\n" +
|
|
"}()")
|
|
],
|
|
triggeringExamples: [
|
|
Example("private let ↓Foo = 20.0"),
|
|
Example("public let ↓Foo = false"),
|
|
Example("internal let ↓Foo = \"Foo\""),
|
|
Example("let ↓Foo = true"),
|
|
Example("let ↓foo = 2, ↓bar = true"),
|
|
Example("var foo = true, let ↓Foo = true"),
|
|
Example("let\n" +
|
|
" ↓foo = true"),
|
|
Example("let ↓foo = {\n" +
|
|
" return a + b\n" +
|
|
"}()")
|
|
]
|
|
)
|
|
|
|
public func validate(file: SwiftLintFile,
|
|
kind: SwiftDeclarationKind,
|
|
dictionary: SourceKittenDictionary) -> [StyleViolation] {
|
|
if configuration.onlyPrivateMembers,
|
|
let acl = dictionary.accessibility, !acl.isPrivate {
|
|
return []
|
|
}
|
|
|
|
guard
|
|
kind == .varGlobal,
|
|
dictionary.setterAccessibility == nil,
|
|
dictionary.bodyLength == nil,
|
|
dictionary.name?.hasPrefix(topLevelPrefix) == false,
|
|
let nameOffset = dictionary.nameOffset
|
|
else {
|
|
return []
|
|
}
|
|
|
|
return [
|
|
StyleViolation(ruleDescription: type(of: self).description,
|
|
severity: configuration.severityConfiguration.severity,
|
|
location: Location(file: file, byteOffset: nameOffset))
|
|
]
|
|
}
|
|
}
|