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

Add new uiimage_requires_bundle Rule #5177

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -207,6 +207,7 @@ public let builtInRules: [Rule.Type] = [
UnavailableConditionRule.self,
UnavailableFunctionRule.self,
UnhandledThrowingTaskRule.self,
UIImageIncludesBundleRule.self,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this and the lines in GeneratedTests.swift to check the rule, but I'm sure there's a more appropriate way to get Sourcery involved here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sourcery

UnneededBreakInSwitchRule.self,
UnneededOverrideRule.self,
UnneededParenthesesInClosureArgumentRule.self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,3 @@ private extension NSLocalizedStringRequireBundleRule {
}
}

private extension TupleExprElementListSyntax {
func containsArgument(named name: String) -> Bool {
contains { arg in
arg.label?.tokenKind == .identifier(name)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import SwiftSyntax

struct UIImageIncludesBundleRule: SwiftSyntaxRule, OptInRule, ConfigurationProviderRule {
var configuration = SeverityConfiguration<Self>(.warning)

static let description = RuleDescription(
identifier: "uiimage_requires_bundle",
name: "UIImage Requires Bundle",
description: "`UIImage(named:)` must specify a bundle via the `in:` parameter",
kind: .lint,
nonTriggeringExamples: [
Example("""
UIImage(named: "image", in: Bundle.main)
"""),
Example("""
UIImageView(image: UIImage(named: "image", in: Bundle(for A.self)))
"""),
Example("""
UIImage(named: "image", in: Bundle(for A.self))
"""),
Example("""
UIImage(named: "image", in: Bundle.main, compatibleWith: aCollection)
"""),
Example("""
UIImage(named: "image", in: Bundle.main, with: aConfiguration)
"""),
Example("""
UIImage(systemName: "systemImage")
"""),
Example("""
UIImage(systemName: "systemImage", compatibleWith: aCollection)
"""),
Example("""
UIImage(systemName: "systemImage", withConfiguration: aConfiguration)
"""),
Example("""
UIImage(systemName: "systemImage", variableValue: 0.5)
"""),
Example("""
UIImage(systemName: "systemImage", variableValue: 0.5, configuration: aConfiguration)
"""),
Example("""
UIImage(
named: "image",
in: Bundle.main
)
"""),
Example("""
arbitraryFunctionCall("something")
"""),
Example("""
UIImageView(image: UIImage(named: "hashtag", in: Bundle.main))
""")
],
triggeringExamples: [
Example("""
↓UIImage(named: "image")
"""),
Example("""
UIImageView(image: ↓UIImage(named: "image"))
"""),
Example("""
↓UIImage(named: "image", compatibleWith: aCollection)
"""),
Example("""
↓UIImage(named: "image", with: aConfiguration)
"""),
Example("""
↓UIImage(
named: "image"
)
""")
]
)

func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
Visitor(viewMode: .sourceAccurate)
}
}

private extension UIImageIncludesBundleRule {
final class Visitor: ViolationsSyntaxVisitor {
override func visitPost(_ node: FunctionCallExprSyntax) {
if let identifierExpr = node.calledExpression.as(IdentifierExprSyntax.self),
identifierExpr.identifier.tokenKind == .identifier("UIImage"),
node.argumentList.containsArgument(named: "named"),
!node.argumentList.containsArgument(named: "in") {
violations.append(node.positionAfterSkippingLeadingTrivia)
}
}
}
}
8 changes: 8 additions & 0 deletions Source/SwiftLintCore/Extensions/SwiftSyntax+SwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,14 @@ public extension AccessorBlockSyntax {
}
}

public extension TupleExprElementListSyntax {
func containsArgument(named name: String) -> Bool {
contains { arg in
arg.label?.tokenKind == .identifier(name)
}
}
}

public extension TypeInheritanceClauseSyntax? {
func containsInheritedType(inheritedTypes: Set<String>) -> Bool {
self?.inheritedTypeCollection.contains { elem in
Expand Down
6 changes: 6 additions & 0 deletions Tests/GeneratedTests/GeneratedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,12 @@ class UnhandledThrowingTaskRuleGeneratedTests: SwiftLintTestCase {
}
}

class UIImageIncludesBundleRuleGeneratedTests: SwiftLintTestCase {
func testWithDefaultConfiguration() {
verifyRule(UIImageIncludesBundleRule.description)
}
}

class UnneededBreakInSwitchRuleGeneratedTests: SwiftLintTestCase {
func testWithDefaultConfiguration() {
verifyRule(UnneededBreakInSwitchRule.description)
Expand Down
Loading