forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrefixedTopLevelConstantRule.swift
More file actions
81 lines (74 loc) · 2.53 KB
/
PrefixedTopLevelConstantRule.swift
File metadata and controls
81 lines (74 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// PrefixedTopLevelConstantRule.swift
// SwiftLint
//
// Created by Ornithologist Coder on 1/5/18.
// Copyright © 2018 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
public struct PrefixedTopLevelConstantRule: ASTRule, OptInRule, ConfigurationProviderRule {
public var configuration = SeverityConfiguration(.warning)
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: [
"private let kFoo = 20.0",
"public let kFoo = false",
"internal let kFoo = \"Foo\"",
"let kFoo = true",
"struct Foo {\n" +
" let bar = 20.0\n" +
"}",
"private var foo = 20.0",
"public var foo = false",
"internal var foo = \"Foo\"",
"var foo = true",
"var foo = true, bar = true",
"var foo = true, let kFoo = true",
"let\n" +
" kFoo = true",
"var foo: Int {\n" +
" return a + b\n" +
"}",
"let kFoo = {\n" +
" return a + b\n" +
"}()"
],
triggeringExamples: [
"private let ↓Foo = 20.0",
"public let ↓Foo = false",
"internal let ↓Foo = \"Foo\"",
"let ↓Foo = true",
"let ↓foo = 2, ↓bar = true",
"var foo = true, let ↓Foo = true",
"let\n" +
" ↓foo = true",
"let ↓foo = {\n" +
" return a + b\n" +
"}()"
]
)
public func validate(file: File,
kind: SwiftDeclarationKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
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.severity,
location: Location(file: file, byteOffset: nameOffset))
]
}
}