forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLegacyConstantRule.swift
88 lines (75 loc) · 3.1 KB
/
LegacyConstantRule.swift
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
82
83
84
85
86
87
88
//
// LegacyConstantRule.swift
// SwiftLint
//
// Created by Aaron McTavish on 12/01/2016.
// Copyright © 2016 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
public struct LegacyConstantRule: CorrectableRule, ConfigurationProviderRule {
public var configuration = SeverityConfiguration(.Warning)
public init() {}
public static let description = RuleDescription(
identifier: "legacy_constant",
name: "Legacy Constant",
description: "Struct-scoped constants are preferred over legacy global constants.",
nonTriggeringExamples: [
"CGRect.infinite",
"CGPoint.zero",
"CGRect.zero",
"CGSize.zero",
"CGRect.null"
],
triggeringExamples: [
"↓CGRectInfinite",
"↓CGPointZero",
"↓CGRectZero",
"↓CGSizeZero",
"↓CGRectNull"
],
corrections: [
"↓CGRectInfinite\n": "CGRect.infinite\n",
"↓CGPointZero\n": "CGPoint.zero\n",
"↓CGRectZero\n": "CGRect.zero\n",
"↓CGSizeZero\n": "CGSize.zero\n",
"↓CGRectInfinite\n↓CGRectNull\n": "CGRect.infinite\nCGRect.null\n"
]
)
public func validateFile(file: File) -> [StyleViolation] {
let constants = ["CGRectInfinite", "CGPointZero", "CGRectZero", "CGSizeZero",
"CGRectNull"]
let pattern = "\\b(" + constants.joinWithSeparator("|") + ")\\b"
return file.matchPattern(pattern, withSyntaxKinds: [.Identifier]).map {
StyleViolation(ruleDescription: self.dynamicType.description,
severity: configuration.severity,
location: Location(file: file, characterOffset: $0.location))
}
}
public func correctFile(file: File) -> [Correction] {
let patterns = [
"CGRectInfinite": "CGRect.infinite",
"CGPointZero": "CGPoint.zero",
"CGRectZero": "CGRect.zero",
"CGSizeZero": "CGSize.zero",
"CGRectNull": "CGRect.null"
]
let description = self.dynamicType.description
var corrections = [Correction]()
var contents = file.contents
let matches = patterns.map({ pattern, template in
file.matchPattern(pattern, withSyntaxKinds: [.Identifier])
.map { ($0, pattern, template) }
}).flatten().sort { $0.0.location > $1.0.location } // reversed
for (range, pattern, template) in matches {
contents = regex(pattern).stringByReplacingMatchesInString(contents,
options: [],
range: range,
withTemplate: template)
let location = Location(file: file, characterOffset: range.location)
corrections.append(Correction(ruleDescription: description, location: location))
}
file.write(contents)
return corrections
}
}