Skip to content

only rule should work for individual custom rules #6057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

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 25050.62 KiB vs 25052.02 KiB when built on main (-1% smaller).
📖 Linting Aerial with this PR took 1.06 s vs 1.02 s on main (3% slower).
📖 Linting Alamofire with this PR took 1.39 s vs 1.37 s on main (1% slower).
📖 Linting Brave with this PR took 9.51 s vs 9.47 s on main (0% slower).
📖 Linting DuckDuckGo with this PR took 23.75 s vs 23.67 s on main (0% slower).
📖 Linting Firefox with this PR took 11.41 s vs 11.39 s on main (0% slower).
📖 Linting Kickstarter with this PR took 10.73 s vs 10.69 s on main (0% slower).
📖 Linting Moya with this PR took 0.55 s vs 0.55 s on main (0% slower).
📖 Linting NetNewsWire with this PR took 3.14 s vs 3.12 s on main (0% slower).
📖 Linting Nimble with this PR took 0.82 s vs 0.82 s on main (0% slower).
📖 Linting PocketCasts with this PR took 9.04 s vs 8.93 s on main (1% slower).
📖 Linting Quick with this PR took 0.47 s vs 0.47 s on main (0% slower).
📖 Linting Realm with this PR took 4.78 s vs 4.77 s on main (0% slower).
📖 Linting Sourcery with this PR took 2.44 s vs 2.42 s on main (0% slower).
📖 Linting Swift with this PR took 5.59 s vs 5.57 s on main (0% slower).
📖 Linting VLC with this PR took 1.45 s vs 1.44 s on main (0% slower).
📖 Linting Wire with this PR took 21.36 s vs 21.27 s on main (0% slower).
📖 Linting WordPress with this PR took 12.63 s vs 12.56 s on main (0% slower).

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 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
@@ -11,11 +11,12 @@ internal extension Configuration {
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 04d22aa to 497fe5f Compare May 24, 2025 10:48
@mildm8nnered mildm8nnered requested a review from SimplyDanny May 29, 2025 20:31
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
2 participants