Add prefer_scaled_to_fit opt-in rule#6609
Conversation
Generated by 🚫 Danger |
|
@SimplyDanny It seems like some CI checks are stuck in "Waiting for status to be reported" for a while. Is there anything I need to do on my end, or is this expected? |
|
Gentle ping @SimplyDanny — the 14 pending checks haven't changed in over a week. Could you take a look when you have a moment? Thanks! |
1ce8651 to
cfe828d
Compare
|
@DemiDevv: I rebased your branch. Checks are running now. |
|
@SimplyDanny Gentle ping — this has been ready for review for a few weeks now with all checks passing. Would appreciate a look when you have a moment! |
SimplyDanny
left a comment
There was a problem hiding this comment.
Thanks for the work and sorry for the delay! Please see my findings (and questions).
| import SwiftSyntax | ||
|
|
||
| @SwiftSyntaxRule(optIn: true) | ||
| struct PreferScaledToFitRule: Rule { |
There was a problem hiding this comment.
There are a few Legacy... rules already. I wonder whether we should name this one in a similar way. What do you think?
All of them are enabled by default (not opt-in) and support auto-correction using LegacyFunctionVisitor.
|
|
||
| static let description = RuleDescription( | ||
| identifier: "prefer_scaled_to_fit", | ||
| name: "Prefer Scaled To Fit", |
There was a problem hiding this comment.
Title style:
| name: "Prefer Scaled To Fit", | |
| name: "Prefer Scaled to Fit", |
| node.arguments.count == 1, | ||
| let argument = node.arguments.first, |
There was a problem hiding this comment.
| node.arguments.count == 1, | |
| let argument = node.arguments.first, | |
| let argument = node.arguments.onlyElement, |
| let valueName = memberValue.declName.baseName.text as String?, | ||
| valueName == "fit" || valueName == "fill" |
There was a problem hiding this comment.
| let valueName = memberValue.declName.baseName.text as String?, | |
| valueName == "fit" || valueName == "fill" | |
| ["fit, "fill"].contains(memberValue.declName.baseName.text) |
| } | ||
|
|
||
| guard | ||
| let memberValue = argument.expression.as(MemberAccessExprSyntax.self), |
There was a problem hiding this comment.
Should we also check that base is nil or ContentMode.
Closes #5713
Adds a new opt-in rule
prefer_scaled_to_fitthat suggests usingscaledToFit()orscaledToFill()instead ofaspectRatio(contentMode:)when the content mode is a constant(
.fitor.fill).As noted in Apple's documentation,
scaledToFit()is equivalent toaspectRatio(nil, contentMode: .fit), so the shorter form ispreferred when no custom aspect ratio is provided.