Skip to content
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

SwiftUI: Prefer scaledToFit()/scaledToFill() over aspectRatio(contentMode:) #5768

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Source/SwiftLintBuiltInRules/Models/BuiltInRules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public let builtInRules: [any Rule.Type] = [
PeriodSpacingRule.self,
PreferKeyPathRule.self,
PreferNimbleRule.self,
PreferScaledToFitAndScaledToFill.self,
PreferSelfInStaticReferencesRule.self,
PreferSelfTypeOverTypeOfSelfRule.self,
PreferTypeCheckingRule.self,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import SwiftSyntax

@SwiftSyntaxRule(explicitRewriter: true)
struct PreferScaledToFitAndScaledToFill: Rule {
var configuration = SeverityConfiguration<Self>(.warning)

static let description = RuleDescription(
identifier: "prefer_scaled_to_fit_and_scaled_to_fill",
name: "Prefer `scaledToFit()` and `scaledToFill()`",
description: "Prefer `scaledToFit` and `scaledToFill` to `aspectRatio`",
kind: .idiomatic,
nonTriggeringExamples: [
Example("""
let ratio = CGSize(width: 1, height: 1)
view.aspectRatio(ratio, contentMode: .fit)
view.aspectRatio(ratio, contentMode: .fill)
"""),
Example("""
let contentMode = ContentMode.fit
view.aspectRatio(contentMode: contentMode)
"""),
Example("""
let shouldFit = true
view.aspectRatio(contentMode: shouldFit ? .fit : .fill)
"""),
],
triggeringExamples: [
Example("view.↓aspectRatio(contentMode: .fit)"),
Example("view.↓aspectRatio(contentMode: .fill)"),
Example("↓aspectRatio(contentMode: .fit)"),
Example("↓aspectRatio(contentMode: .fill)"),
],
corrections: [
Example("view.↓aspectRatio(contentMode: .fit)"): Example("view.scaledToFit()"),
Example("view.↓aspectRatio(contentMode: .fill)"): Example("view.scaledToFill()"),
Example("↓aspectRatio(contentMode: .fit)"): Example("scaledToFit()"),
Example("↓aspectRatio(contentMode: .fill)"): Example("scaledToFill()"),
]
)
}

private extension PreferScaledToFitAndScaledToFill {
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
override func visitPost(_ node: FunctionCallExprSyntax) {
if node.hasViolation {
violations.append(node.positionAfterSkippingLeadingTrivia)
}
// if let expr = node.calledExpression.as(DeclReferenceExprSyntax.self),
// expr.baseName.text.starts(with: "XCTAssert") {
// violations.append(node.positionAfterSkippingLeadingTrivia)
// }
}
}

final class Rewriter: ViolationsSyntaxRewriter<ConfigurationType> {
override func visit(_ node: FunctionCallExprSyntax) -> ExprSyntax {
if node.hasViolation {
return ExprSyntax(stringLiteral: "A is C")
.with(\.leadingTrivia, node.leadingTrivia)
.with(\.trailingTrivia, node.trailingTrivia)
}

return ExprSyntax(stringLiteral: "A is B")
.with(\.leadingTrivia, node.leadingTrivia)
.with(\.trailingTrivia, node.trailingTrivia)
}
}
}

private extension FunctionCallExprSyntax {
var hasViolation: Bool {
name == "aspectRatio"
&& argumentNames == ["contentMode"]
&& argumentIsFitOrFill
}

var name: String? {
guard let expr = calledExpression.as(DeclReferenceExprSyntax.self) else {
return nil
}

return expr.baseName.text
}

var argumentNames: [String?] {
arguments.map(\.label?.text)
}

var argumentIsFitOrFill: Bool {
true
}
}
2 changes: 2 additions & 0 deletions Tests/IntegrationTests/default_rule_configurations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ prefer_key_path:
restrict_to_standard_functions: true
prefer_nimble:
severity: warning
prefer_scaled_to_fit_and_scaled_to_fill:
severity: warning
prefer_self_in_static_references:
severity: warning
prefer_self_type_over_type_of_self:
Expand Down