-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathLegacyNSGeometryFunctionsRule.swift
More file actions
125 lines (119 loc) · 6.19 KB
/
LegacyNSGeometryFunctionsRule.swift
File metadata and controls
125 lines (119 loc) · 6.19 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import SwiftLintCore
@SwiftSyntaxRule(explicitRewriter: true)
struct LegacyNSGeometryFunctionsRule: Rule {
var configuration = SeverityConfiguration<Self>(.warning)
static let description = RuleDescription(
identifier: "legacy_nsgeometry_functions",
name: "Legacy NSGeometry Functions",
description: "Struct extension properties and methods are preferred over legacy functions",
rationale: """
The CGRect extension properties are a more modern API (and are available on NSRect, which
is a typealias for CGRect), and are supported on all Swift platforms.
The legacy functions are only supported on macOS and Mac Catalyst.
""",
kind: .idiomatic,
nonTriggeringExamples: [
Example("rect.width"),
Example("rect.height"),
Example("rect.minX"),
Example("rect.midX"),
Example("rect.maxX"),
Example("rect.minY"),
Example("rect.midY"),
Example("rect.maxY"),
Example("rect.isEmpty"),
Example("rect.integral"),
Example("rect.insetBy(dx: 5.0, dy: -7.0)"),
Example("rect.offsetBy(dx: 5.0, dy: -7.0)"),
Example("rect1.union(rect2)"),
Example("rect1.intersection(rect2)"),
// "rect.divide(atDistance: 10.2, fromEdge: edge)", No correction available for divide
Example("rect1.contains(rect2)"),
Example("rect.contains(point)"),
Example("rect1.intersects(rect2)"),
],
triggeringExamples: [
Example("↓NSWidth(rect)"),
Example("↓NSHeight(rect)"),
Example("↓NSMinX(rect)"),
Example("↓NSMidX(rect)"),
Example("↓NSMaxX(rect)"),
Example("↓NSMinY(rect)"),
Example("↓NSMidY(rect)"),
Example("↓NSMaxY(rect)"),
Example("↓NSEqualRects(rect1, rect2)"),
Example("↓NSEqualSizes(size1, size2)"),
Example("↓NSEqualPoints(point1, point2)"),
Example("↓NSEdgeInsetsEqual(insets2, insets2)"),
Example("↓NSIsEmptyRect(rect)"),
Example("↓NSIntegralRect(rect)"),
Example("↓NSInsetRect(rect, 10, 5)"),
Example("↓NSOffsetRect(rect, -2, 8.3)"),
Example("↓NSUnionRect(rect1, rect2)"),
Example("↓NSIntersectionRect(rect1, rect2)"),
Example("↓NSContainsRect(rect1, rect2)"),
Example("↓NSPointInRect(rect, point)"),
Example("↓NSIntersectsRect(rect1, rect2)"),
],
corrections: [
Example("↓NSWidth( rect )"): Example("rect.width"),
Example("↓NSHeight(rect )"): Example("rect.height"),
Example("↓NSMinX( rect)"): Example("rect.minX"),
Example("↓NSMidX( rect)"): Example("rect.midX"),
Example("↓NSMaxX( rect)"): Example("rect.maxX"),
Example("↓NSMinY(rect )"): Example("rect.minY"),
Example("↓NSMidY(rect )"): Example("rect.midY"),
Example("↓NSMaxY( rect )"): Example("rect.maxY"),
Example("↓NSEqualPoints( point1 , point2)"): Example("point1 == point2"),
Example("↓NSEqualSizes(size1,size2 )"): Example("size1 == size2"),
Example("↓NSEqualRects( rect1, rect2)"): Example("rect1 == rect2"),
Example("↓NSEdgeInsetsEqual(insets1, insets2)"): Example("insets1 == insets2"),
Example("↓NSIsEmptyRect( rect )"): Example("rect.isEmpty"),
Example("↓NSIntegralRect(rect )"): Example("rect.integral"),
Example("↓NSInsetRect(rect, 5.0, -7.0)"): Example("rect.insetBy(dx: 5.0, dy: -7.0)"),
Example("↓NSOffsetRect(rect, -2, 8.3)"): Example("rect.offsetBy(dx: -2, dy: 8.3)"),
Example("↓NSUnionRect(rect1, rect2)"): Example("rect1.union(rect2)"),
Example("↓NSContainsRect( rect1,rect2 )"): Example("rect1.contains(rect2)"),
Example("↓NSPointInRect(point ,rect)"): Example("rect.contains(point)"), // note order of arguments
Example("↓NSIntersectsRect( rect1,rect2 )"): Example("rect1.intersects(rect2)"),
Example("↓NSIntersectsRect(rect1, rect2 )\n↓NSWidth(rect )"):
Example("rect1.intersects(rect2)\nrect.width"),
Example("↓NSIntersectionRect(rect1, rect2)"): Example("rect1.intersection(rect2)"),
]
)
private static let legacyFunctions: [String: LegacyFunctionRewriteStrategy] = [
"NSHeight": .property(name: "height"),
"NSIntegralRect": .property(name: "integral"),
"NSIsEmptyRect": .property(name: "isEmpty"),
"NSMaxX": .property(name: "maxX"),
"NSMaxY": .property(name: "maxY"),
"NSMidX": .property(name: "midX"),
"NSMidY": .property(name: "midY"),
"NSMinX": .property(name: "minX"),
"NSMinY": .property(name: "minY"),
"NSWidth": .property(name: "width"),
"NSEqualPoints": .equal,
"NSEqualSizes": .equal,
"NSEqualRects": .equal,
"NSEdgeInsetsEqual": .equal,
"NSInsetRect": .function(name: "insetBy", argumentLabels: ["dx", "dy"]),
"NSOffsetRect": .function(name: "offsetBy", argumentLabels: ["dx", "dy"]),
"NSUnionRect": .function(name: "union", argumentLabels: [""]),
"NSContainsRect": .function(name: "contains", argumentLabels: [""]),
"NSIntersectsRect": .function(name: "intersects", argumentLabels: [""]),
"NSIntersectionRect": .function(name: "intersection", argumentLabels: [""]),
"NSPointInRect": .function(name: "contains", argumentLabels: [""], reversed: true),
]
}
private extension LegacyNSGeometryFunctionsRule {
final class Visitor: LegacyFunctionVisitor<ConfigurationType> {
init(configuration: ConfigurationType, file: SwiftLintFile) {
super.init(configuration: configuration, file: file, legacyFunctions: legacyFunctions)
}
}
final class Rewriter: LegacyFunctionRewriter<ConfigurationType> {
init(configuration: ConfigurationType, file: SwiftLintFile) {
super.init(configuration: configuration, file: file, legacyFunctions: legacyFunctions)
}
}
}