-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f65377
commit 48577fe
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
Source/SwiftLintBuiltInRules/Rules/Lint/UIImageIncludesBundleRule.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
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) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private extension TupleExprElementListSyntax { | ||
func containsArgument(named name: String) -> Bool { | ||
contains { arg in | ||
arg.label?.tokenKind == .identifier(name) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters