SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

Enable/disable rules in #Preview macros

Open akaralar opened this issue 8 months ago • 3 comments

New Issue Checklist

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?

akaralar avatar Jul 03 '25 11:07 akaralar

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?

SimplyDanny avatar Jul 03 '25 16:07 SimplyDanny

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!

akaralar avatar Jul 04 '25 14:07 akaralar

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.

SimplyDanny avatar Jul 23 '25 21:07 SimplyDanny

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().

marcpalmer avatar Dec 15 '25 12:12 marcpalmer

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().

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.

SimplyDanny avatar Dec 17 '25 19:12 SimplyDanny