SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

only rule should work for individual custom rules

Open mildm8nnered opened this issue 8 months ago • 1 comments

Addresses #6029 and #6058

Allows individual custom rules to be specified in the only_rules configuration, or the --only-rule command line option, without also having to specify custom_rules.

I think this may bring individual custom_rules to be almost equivalent to "normal" rules, as they are now supported in swiftlint:disable commands, and most other cases as well.

The key change here is that when we calculate the resulting rules, if CustomRules is not in there, we manually add it in.

if !resultingRules.contains(where: { $0 is CustomRules }),
   !customRulesIdentifiers.isDisjoint(with: onlyRulesRuleIdentifiers),
   let customRules {
    resultingRules.append(customRules)
}

There was already pre-existing code later on to filter which custom rules are enabled.

Some further changes were required to make sure that the warning about configured but not enabled rules was fired for this configuration in the only_rules case.

Other changes

We were calling (allRulesWrapped.first { $0.rule is CustomRules })?.rule as? CustomRules in about five different places, so I added a convenience var to [ConfigurationRuleWrapper].

validRuleIdentifiers is now calculated lazily.

Order of custom_rules violations

The order of violations reported by custom_rules was not deterministic - see #6058 - violations are now reported sorted by the custom rule identifier.

There is still some weirdness in the order that violations are output in, but I'll raise those issues elsewhere - they should at least be deterministic now.

mildm8nnered avatar Apr 21 '25 02:04 mildm8nnered

18 Messages
:book: Building this branch resulted in a binary size of 26894.15 KiB vs 26895.4 KiB when built on main (-1% smaller).
:book: Linting Aerial with this PR took 0.81 s vs 0.82 s on main (1% faster).
:book: Linting Alamofire with this PR took 1.02 s vs 1.01 s on main (0% slower).
:book: Linting Brave with this PR took 6.95 s vs 6.89 s on main (0% slower).
:book: Linting DuckDuckGo with this PR took 20.89 s vs 20.93 s on main (0% faster).
:book: Linting Firefox with this PR took 10.63 s vs 10.57 s on main (0% slower).
:book: Linting Kickstarter with this PR took 7.71 s vs 7.72 s on main (0% faster).
:book: Linting Moya with this PR took 0.45 s vs 0.44 s on main (2% slower).
:book: Linting NetNewsWire with this PR took 2.37 s vs 2.37 s on main (0% slower).
:book: Linting Nimble with this PR took 0.63 s vs 0.63 s on main (0% slower).
:book: Linting PocketCasts with this PR took 7.23 s vs 7.32 s on main (1% faster).
:book: Linting Quick with this PR took 0.41 s vs 0.41 s on main (0% slower).
:book: Linting Realm with this PR took 3.48 s vs 3.46 s on main (0% slower).
:book: Linting Sourcery with this PR took 1.83 s vs 1.85 s on main (1% faster).
:book: Linting Swift with this PR took 4.26 s vs 4.26 s on main (0% slower).
:book: Linting VLC with this PR took 1.12 s vs 1.13 s on main (0% faster).
:book: Linting Wire with this PR took 17.18 s vs 17.24 s on main (0% faster).
:book: Linting WordPress with this PR took 11.22 s vs 11.27 s on main (0% faster).

Generated by :no_entry_sign: Danger

SwiftLintBot avatar Apr 21 '25 02:04 SwiftLintBot