Enable/disable rules in #Preview macros
New Issue Checklist
- [x] I've Updated SwiftLint to the latest version.
- [x] I've searched for existing GitHub issues.
Feature or Enhancement Proposal
I would like to be able to enable/disable specific rules in #Preview macros. I saw this and the associated PR but it doesn't seem to give users a way to configure this. The solution proposed in that issue (but didn't make it to the implementation) would work. Would it be possible to expose such a config to users?
The solution proposed in the linked issue doesn't work for SwiftLint since it cannot see the inlined macro. For some rules, we have decided that they don't make sense for #Preview macros and hard-coded their exclusion.
Which rules would you like to have excluded as well? Or are you talking about a way (that works for all rules) of configuring that a rule shall ignore the macro body?
Thanks for the quick answer @SimplyDanny!
The solution proposed in the linked issue doesn't work for SwiftLint since it cannot see the inlined macro.
I see, I just wanted to provide one way it could work for us, no problem if the proposed solution isn't feasible.
Or are you talking about a way (that works for all rules) of configuring that a rule shall ignore the macro body?
Exactly, we want to be able to specify rules that are excluded in the macro body. Too many to list and we want to have the flexibility to add or remove rules at will. The aim is to try to have laxer rules for previews, but we want to be able to be strict again if it doesn't work well after a trial period.
On top of that, if possible at all, we want to be able to not count the preview lines when calculating file_length. It would be super amazing if this wasn't specific to only the preview macro but more generalizable to other macros.
On top of that, it would be double super amazing if we were able to exclude some #if DEBUG bodies as well.
One other way I imagine this could work is to be able to define a regex in the config, define the rules to exclude for that regex, and define how the exclusions will be applied: 1- It can be applied only in the scope directly after the regex match, or 2- it can be applied for the rest of the file after that regex match.
In other words, 1 would be equivalent to
// swiftlint:disable <rules>
<regex match> {
// some code
}
// swiftlint:enable <rules>
and 2 would be equivalent to
// swiftlint:disable <rules>
<regex match>
Just to be clear, I'm not sure how feasible these are, I'm just trying to brainstorm a generalizable solution. Thanks again for taking the time to respond!
Excluding certain scopes from file_length would be feasible. There is not much to think about, I guess.
The other idea is much more involved as there's currently nothing similar available. Other concepts are conceivable, like "categories of rules" which could be disabled/enabled as a group.
Another one we keep hitting is redundant_discardable_let inside #Preview, which like SwiftUI result builders does not support the _ = something() syntax, you have to provide let _ = something().
Another one we keep hitting is
redundant_discardable_letinside #Preview, which like SwiftUI result builders does not support the_ = something()syntax, you have to providelet _ = something().
They should be ignored by setting ignore_swiftui_view_bodies: true since version 0.60.0. If there's a gap, please report it in a separate ticket.