-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add legacy_uigraphics_functions rule
#6269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,82 @@ | ||||||
| import SwiftSyntax | ||||||
|
|
||||||
| @SwiftSyntaxRule | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rule should be opt-in as you have described in the issue:
Suggested change
|
||||||
| struct LegacyUIGraphicsFunctionsRule: Rule { | ||||||
| var configuration = SeverityConfiguration<Self>(.warning) | ||||||
|
|
||||||
| static let description = RuleDescription( | ||||||
| identifier: "legacy_uigraphics_functions", | ||||||
| name: "Legacy UIGraphics Functions", | ||||||
| description: "Prefer using `UIGraphicsImageRenderer` over legacy functions", | ||||||
| rationale: "The modern replacement is safer, cleaner, Retina-aware and more performant", | ||||||
| kind: .idiomatic, | ||||||
| nonTriggeringExamples: [ | ||||||
| Example(""" | ||||||
| let renderer = UIGraphicsImageRenderer(size: bounds.size) | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests can be a bit easier, as the rule effectively only checks function names. So the |
||||||
| let screenshot = renderer.image { _ in | ||||||
| myUIView.drawHierarchy(in: bounds, afterScreenUpdates: true) | ||||||
| } | ||||||
| """), | ||||||
|
|
||||||
| Example(""" | ||||||
| let renderer = UIGraphicsImageRenderer(size: newSize) | ||||||
| let combined = renderer.image { _ in | ||||||
| background.draw(in: CGRect(origin: .zero, size: newSize)) | ||||||
| watermark.draw(in: CGRect(origin: .zero, size: watermarkSize)) | ||||||
| } | ||||||
| """), | ||||||
|
|
||||||
| Example(""" | ||||||
| let format = UIGraphicsImageRendererFormat() | ||||||
| format.scale = 1.0 | ||||||
| format.opaque = true | ||||||
|
|
||||||
| let renderer = UIGraphicsImageRenderer(size: newSize, format: format) | ||||||
| return renderer.image { _ in | ||||||
| image.draw(in: CGRect(origin: .zero, size: newSize)) | ||||||
| } | ||||||
| """), | ||||||
| ], | ||||||
| triggeringExamples: [ | ||||||
| Example(""" | ||||||
| ↓UIGraphicsBeginImageContext(newSize) | ||||||
| myUIView.drawHierarchy(in: bounds, afterScreenUpdates: false) | ||||||
| let optionalScreenshot = ↓UIGraphicsGetImageFromCurrentImageContext() | ||||||
| ↓UIGraphicsEndImageContext() | ||||||
| """), | ||||||
|
|
||||||
| Example(""" | ||||||
| ↓UIGraphicsBeginImageContext(newSize) | ||||||
| background.draw(in: CGRect(origin: .zero, size: newSize)) | ||||||
| watermark.draw(in: CGRect(origin: .zero, size: watermarkSize)) | ||||||
| let optionalOutput = ↓UIGraphicsGetImageFromCurrentImageContext() | ||||||
| ↓UIGraphicsEndImageContext() | ||||||
| """), | ||||||
|
|
||||||
| Example(""" | ||||||
| ↓UIGraphicsBeginImageContextWithOptions(newSize, true, 1.0) | ||||||
| image.draw(in: CGRect(origin: .zero, size: newSize)) | ||||||
| let optionalOutput = ↓UIGraphicsGetImageFromCurrentImageContext() | ||||||
| ↓UIGraphicsEndImageContext() | ||||||
| """), | ||||||
| ] | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| private extension LegacyUIGraphicsFunctionsRule { | ||||||
| final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> { | ||||||
| private static let legacyUIGraphicsFunctions: Set<String> = [ | ||||||
| "UIGraphicsBeginImageContext", | ||||||
| "UIGraphicsBeginImageContextWithOptions", | ||||||
| "UIGraphicsGetImageFromCurrentImageContext", | ||||||
| "UIGraphicsEndImageContext", | ||||||
| ] | ||||||
|
|
||||||
| override func visitPost(_ node: FunctionCallExprSyntax) { | ||||||
| if let function = node.calledExpression.as(DeclReferenceExprSyntax.self)?.baseName.text, | ||||||
| Self.legacyUIGraphicsFunctions.contains(function) { | ||||||
| violations.append(node.positionAfterSkippingLeadingTrivia) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.