|
| 1 | +import SwiftSyntax |
| 2 | + |
| 3 | +@SwiftSyntaxRule(optIn: true) |
| 4 | +struct LegacyUIGraphicsFunctionRule: Rule { |
| 5 | + var configuration = SeverityConfiguration<Self>(.warning) |
| 6 | + |
| 7 | + static let description = RuleDescription( |
| 8 | + identifier: "legacy_uigraphics_function", |
| 9 | + name: "Legacy UIGraphics Function", |
| 10 | + description: "Prefer using `UIGraphicsImageRenderer` over legacy functions", |
| 11 | + rationale: "The modern replacement is safer, cleaner, Retina-aware and more performant.", |
| 12 | + kind: .idiomatic, |
| 13 | + nonTriggeringExamples: [ |
| 14 | + Example(""" |
| 15 | + let renderer = UIGraphicsImageRenderer(size: bounds.size) |
| 16 | + let screenshot = renderer.image { _ in |
| 17 | + myUIView.drawHierarchy(in: bounds, afterScreenUpdates: true) |
| 18 | + } |
| 19 | + """), |
| 20 | + |
| 21 | + Example(""" |
| 22 | + let renderer = UIGraphicsImageRenderer(size: newSize) |
| 23 | + let combined = renderer.image { _ in |
| 24 | + background.draw(in: CGRect(origin: .zero, size: newSize)) |
| 25 | + watermark.draw(in: CGRect(origin: .zero, size: watermarkSize)) |
| 26 | + } |
| 27 | + """), |
| 28 | + |
| 29 | + Example(""" |
| 30 | + UIGraphicsImageRenderer(size: newSize, format: UIGraphicsImageRendererFormat()).image { _ in |
| 31 | + image.draw(in: CGRect(origin: .zero, size: newSize)) |
| 32 | + } |
| 33 | + """), |
| 34 | + ], |
| 35 | + triggeringExamples: [ |
| 36 | + Example(""" |
| 37 | + ↓UIGraphicsBeginImageContext(newSize) |
| 38 | + myUIView.drawHierarchy(in: bounds, afterScreenUpdates: false) |
| 39 | + let optionalScreenshot = ↓UIGraphicsGetImageFromCurrentImageContext() |
| 40 | + ↓UIGraphicsEndImageContext() |
| 41 | + """), |
| 42 | + |
| 43 | + Example(""" |
| 44 | + ↓UIGraphicsBeginImageContext(newSize) |
| 45 | + background.draw(in: CGRect(origin: .zero, size: newSize)) |
| 46 | + watermark.draw(in: CGRect(origin: .zero, size: watermarkSize)) |
| 47 | + let optionalOutput = ↓UIGraphicsGetImageFromCurrentImageContext() |
| 48 | + ↓UIGraphicsEndImageContext() |
| 49 | + """), |
| 50 | + |
| 51 | + Example(""" |
| 52 | + ↓UIGraphicsBeginImageContextWithOptions(newSize, true, 1.0) |
| 53 | + image.draw(in: CGRect(origin: .zero, size: newSize)) |
| 54 | + let optionalOutput = ↓UIGraphicsGetImageFromCurrentImageContext() |
| 55 | + ↓UIGraphicsEndImageContext() |
| 56 | + """), |
| 57 | + ] |
| 58 | + ) |
| 59 | +} |
| 60 | + |
| 61 | +private extension LegacyUIGraphicsFunctionRule { |
| 62 | + final class Visitor: LegacyFunctionVisitor<ConfigurationType> { |
| 63 | + private static let legacyUIGraphicsFunctions: [String: LegacyFunctionRewriteStrategy] = [ |
| 64 | + "UIGraphicsBeginImageContext": .noRewrite, |
| 65 | + "UIGraphicsBeginImageContextWithOptions": .noRewrite, |
| 66 | + "UIGraphicsGetImageFromCurrentImageContext": .noRewrite, |
| 67 | + "UIGraphicsEndImageContext": .noRewrite, |
| 68 | + ] |
| 69 | + |
| 70 | + init(configuration: ConfigurationType, file: SwiftLintFile) { |
| 71 | + super.init(configuration: configuration, file: file, legacyFunctions: Self.legacyUIGraphicsFunctions) |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments