Add prefer_asset_symbols rule#6261
Merged
SimplyDanny merged 12 commits intorealm:mainfrom Oct 10, 2025
Merged
Conversation
Generated by 🚫 Danger |
SimplyDanny
requested changes
Oct 6, 2025
Collaborator
SimplyDanny
left a comment
There was a problem hiding this comment.
Thanks! Please consider my comments.
Source/SwiftLintBuiltInRules/Rules/Idiomatic/PreferAssetSymbolsRule.swift
Outdated
Show resolved
Hide resolved
Source/SwiftLintBuiltInRules/Rules/Idiomatic/PreferAssetSymbolsRule.swift
Outdated
Show resolved
Hide resolved
Source/SwiftLintBuiltInRules/Rules/Idiomatic/PreferAssetSymbolsRule.swift
Outdated
Show resolved
Hide resolved
Source/SwiftLintBuiltInRules/Rules/Idiomatic/PreferAssetSymbolsRule.swift
Outdated
Show resolved
Hide resolved
Source/SwiftLintBuiltInRules/Rules/Idiomatic/PreferAssetSymbolsRule.swift
Outdated
Show resolved
Hide resolved
Source/SwiftLintBuiltInRules/Rules/Idiomatic/PreferAssetSymbolsRule.swift
Outdated
Show resolved
Hide resolved
Source/SwiftLintBuiltInRules/Rules/Idiomatic/PreferAssetSymbolsRule.swift
Outdated
Show resolved
Hide resolved
Implements a new SwiftLint rule that suggests using asset symbols over
string-based image initialization to avoid typos and enable compile-time
checking.
The rule detects:
- UIImage(named: "string") calls
- SwiftUI Image("string") calls
And suggests using asset symbols like:
- UIImage(resource: .imageName)
- Image(.imageName)
Resolves realm#5939
- Replace string-based matching with proper SwiftSyntax node matching - Add isUIImageCall() and isImageCall() methods that match on syntax tree structure - Improves reliability and performance by avoiding string conversions - Addresses PR feedback about not relying on string representations
- Replace manual argument counting with node.arguments.onlyElement - Ensures exactly one argument for both UIImage(named:) and Image(_:) - Eliminates need for separate checks for additional parameters - More concise and reliable than previous implementation - Addresses PR feedback about argument position validation
- Remove onlyElement restriction to allow bundle parameter detection - Update UIImage logic to find any argument with 'named' label - Update SwiftUI Image logic to check first argument regardless of additional params - Add bundle image cases to triggeringExamples - Remove bundle cases from nonTriggeringExamples - ImageResource generates assets across all bundles, so violations should be triggered
- Change from finding any 'named' argument to checking first argument specifically - Ensures UIImage(named:) pattern is correctly identified - More precise matching for UIImage initializer signatures - Maintains same behavior but with better argument position validation
d69efde to
4f5a426
Compare
- Change @SwiftSyntaxRule(optIn: false) to @SwiftSyntaxRule(optIn: true) - Rule now requires explicit enablement in configuration - Follows SwiftLint guidelines for new rules being opt-in by default
…s opt-in - Change opt-in: false to opt-in: true in integration test configuration - Ensures consistency between rule implementation and test expectations - Fixes integration test validation for the opt-in rule setting
SimplyDanny
reviewed
Oct 9, 2025
Collaborator
SimplyDanny
left a comment
There was a problem hiding this comment.
A few more comments. 😉
Source/SwiftLintBuiltInRules/Rules/Idiomatic/PreferAssetSymbolsRule.swift
Outdated
Show resolved
Hide resolved
Source/SwiftLintBuiltInRules/Rules/Idiomatic/PreferAssetSymbolsRule.swift
Outdated
Show resolved
Hide resolved
Source/SwiftLintBuiltInRules/Rules/Idiomatic/PreferAssetSymbolsRule.swift
Outdated
Show resolved
Hide resolved
- Merged isUIImageCall and isImageCall into a single isImageCall function with className parameter - Merged isUIImageNamedInit and isSwiftUIImageInit into a single isImageInit function with className and argumentLabel parameters - Reduced code duplication and improved maintainability
SimplyDanny
approved these changes
Oct 10, 2025
Collaborator
SimplyDanny
left a comment
There was a problem hiding this comment.
That looks good now. Thanks @danglingP0inter!
Linting checks but complain about a few trailing spaces in the CHANGELOG.
Contributor
Author
Fixed. Removed trailing spaces. Thanks, @SimplyDanny! |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements a new SwiftLint rule that suggests using asset symbols over string-based image initialization to avoid typos and enable compile-time checking.
The rule detects:
And suggests using asset symbols like:
Resolves #5939