Skip to content

only rule should work for individual custom rules#6057

Merged
mildm8nnered merged 23 commits intorealm:mainfrom
mildm8nnered:mildm8nnered-only-rule-custom-rule
Sep 18, 2025
Merged

only rule should work for individual custom rules#6057
mildm8nnered merged 23 commits intorealm:mainfrom
mildm8nnered:mildm8nnered-only-rule-custom-rule

Conversation

@mildm8nnered
Copy link
Collaborator

@mildm8nnered mildm8nnered commented Apr 21, 2025

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.

@SwiftLintBot
Copy link

SwiftLintBot commented Apr 21, 2025

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

Generated by 🚫 Danger

@mildm8nnered mildm8nnered changed the title Mildm8nnered only rule custom rule only rule should work for individual custom rules Apr 21, 2025
@mildm8nnered mildm8nnered requested a review from Copilot April 22, 2025 12:18
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses issues #6029 and #6058 by allowing individual custom rules to be enabled via the only_rules configuration (or the --only-rule command line option) without requiring a separate custom_rules configuration. It also adds a convenience property for retrieving CustomRules, sorts custom rule violations deterministically, and updates tests/documentation accordingly.

  • Enables specifying individual custom rules in only_rules.
  • Introduces lazy evaluation of valid rule identifiers and a new extension for accessing CustomRules.
  • Ensures deterministic ordering of custom rule violations.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
Tests/FrameworkTests/CustomRulesTests.swift Adds tests for only_rules support with custom rules enabling
Tests/FrameworkTests/ConfigurationTests.swift Adds test verifying specific custom rules behavior in the configuration
Source/SwiftLintFramework/Rules/CustomRules.swift Sorts customRuleConfigurations to guarantee deterministic violation order
Source/SwiftLintFramework/Configuration/Configuration+RulesWrapper.swift Refactors rule identifier handling and customRules appending logic
Source/SwiftLintFramework/Configuration/Configuration+RulesMode.swift Updates CustomRules handling in rules mode configurations
Source/SwiftLintFramework/Configuration/Configuration+Parsing.swift Updates parsing and validation to support custom rule identifiers
CHANGELOG.md Documents the bug fix and behavior change for custom_rules configuration

@mildm8nnered mildm8nnered marked this pull request as ready for review April 22, 2025 15:10
private let aliasResolver: (String) -> String

private var invalidRuleIdsWarnedAbout: Set<String> = []
private var customRulesIdentifiers: Set<String> {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had these as lazy initially, which should be safe, as resultingRules is locked, and the only other place they are called from is when we're merging configurations, but making them non-lazy seemed safer.

@mildm8nnered mildm8nnered force-pushed the mildm8nnered-only-rule-custom-rule branch from e07e075 to 04d22aa Compare April 23, 2025 09:58
@mildm8nnered mildm8nnered force-pushed the mildm8nnered-only-rule-custom-rule branch from 04d22aa to 497fe5f Compare May 24, 2025 10:48
@mildm8nnered mildm8nnered requested a review from SimplyDanny May 29, 2025 20:31
@mildm8nnered mildm8nnered force-pushed the mildm8nnered-only-rule-custom-rule branch from 497fe5f to 16aafde Compare July 12, 2025 19:54
Copy link
Collaborator

@SimplyDanny SimplyDanny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a nit. Otherwise looks good to me!

Comment on lines +163 to +168
guard let resultingCustomRules = config.rules.first(where: { $0 is CustomRules }) as? CustomRules
else {
XCTFail("Custom rules are expected to be present")
return
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
guard let resultingCustomRules = config.rules.first(where: { $0 is CustomRules }) as? CustomRules
else {
XCTFail("Custom rules are expected to be present")
return
}
let resultingCustomRules = config.rules.customRules
XCTAssertNotNil(resultingCustomRules)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the existing extension on [ConfigurationRuleWrapper] was not quite right here - it retrieves the custom rules before disablement, but I added a similar extension for [any Rule], which enables config.rules.customRules and retrieves the configured rules, and replaces eight cases of rules.first(where: { $0 is CustomRules }) as? CustomRules in the other tests.

@mildm8nnered mildm8nnered force-pushed the mildm8nnered-only-rule-custom-rule branch from 16aafde to 6f84579 Compare September 15, 2025 10:21
@mildm8nnered mildm8nnered force-pushed the mildm8nnered-only-rule-custom-rule branch from c8434bd to befab0b Compare September 18, 2025 08:14
@mildm8nnered mildm8nnered merged commit 13c0c23 into realm:main Sep 18, 2025
21 checks passed
@mildm8nnered mildm8nnered deleted the mildm8nnered-only-rule-custom-rule branch September 18, 2025 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

custom_rules violations are not reported in a deterministic order OnlyRule not work with custom_rules

4 participants