Skip to content
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

Fix nested disable commands improved #5797

Conversation

mildm8nnered
Copy link
Collaborator

@mildm8nnered mildm8nnered commented Sep 15, 2024

Addresses #5788

The problem is that Regions are created with all of the rule identifiers that are disabled in them, with a new region being created for each swiftlint: command, more or less.

So an inner region may have multiple rules disabled, when disabled commands are nested, but not all of those rules might be violated within that specific region.

I guess people don't actually nest commands much in practice, at least for custom rules, or this would have created more noise.

This was not as hard to fix as I was afraid it might have been.

The basic idea is to remap the regions inside superfluousDisableCommandViolations, so that the new regions describe the specific region that an individual rule is disabled within.

@SwiftLintBot
Copy link

SwiftLintBot commented Sep 15, 2024

17 Messages
📖 Linting Aerial with this PR took 0.92s vs 0.93s on main (1% faster)
📖 Linting Alamofire with this PR took 1.26s vs 1.27s on main (0% faster)
📖 Linting Brave with this PR took 7.17s vs 7.26s on main (1% faster)
📖 Linting DuckDuckGo with this PR took 4.97s vs 5.05s on main (1% faster)
📖 Linting Firefox with this PR took 10.44s vs 10.58s on main (1% faster)
📖 Linting Kickstarter with this PR took 9.78s vs 9.79s on main (0% faster)
📖 Linting Moya with this PR took 0.53s vs 0.55s on main (3% faster)
📖 Linting NetNewsWire with this PR took 2.61s vs 2.63s on main (0% faster)
📖 Linting Nimble with this PR took 0.77s vs 0.78s on main (1% faster)
📖 Linting PocketCasts with this PR took 8.38s vs 8.39s on main (0% faster)
📖 Linting Quick with this PR took 0.44s vs 0.45s on main (2% faster)
📖 Linting Realm with this PR took 4.5s vs 4.53s on main (0% faster)
📖 Linting Sourcery with this PR took 2.29s vs 2.32s on main (1% faster)
📖 Linting Swift with this PR took 4.46s vs 4.5s on main (0% faster)
📖 Linting VLC with this PR took 1.25s vs 1.27s on main (1% faster)
📖 Linting Wire with this PR took 17.38s vs 17.58s on main (1% faster)
📖 Linting WordPress with this PR took 11.47s vs 11.55s on main (0% faster)

Generated by 🚫 Danger

@mildm8nnered mildm8nnered marked this pull request as ready for review September 16, 2024 21:52
@mildm8nnered mildm8nnered force-pushed the mildm8nnered-fix-nested-disable-commands-improved branch 2 times, most recently from 593b3ad to 2f6c930 Compare September 21, 2024 18:46
@mildm8nnered mildm8nnered force-pushed the mildm8nnered-fix-nested-disable-commands-improved branch from 4b50fd5 to cba6a63 Compare September 29, 2024 22:15
@mildm8nnered mildm8nnered force-pushed the mildm8nnered-fix-nested-disable-commands-improved branch 2 times, most recently from d44b112 to 15a4304 Compare October 8, 2024 18:03
@mildm8nnered mildm8nnered force-pushed the mildm8nnered-fix-nested-disable-commands-improved branch from 15a4304 to 723c11a Compare October 11, 2024 18:25
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.

The tests proof that things work as intended. That's probably sufficient here. 😅

@@ -145,6 +147,54 @@ private extension Rule {
ruleTime: ruleTime,
deprecatedToValidIDPairs: deprecatedToValidIDPairs)
}

// Produces one region for each disabled rule identifier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps you can add some more documentation and examples here, because I'm having a hard time understanding what's happening and why.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Let me have think about what to add. The long-winded explanation is this:

Given these rules:

"rule1": [
    "regex": "pattern1"
],
"rule2": [
    "regex": "pattern2"
],

and this input:

// swiftlint:disable rule1
// swiftlint:disable rule2
let pattern2 = ""
// swiftlint:enable rule2
let pattern1 = ""
// swiftlint:enable rule1

The regions of the file will be (index: start - end disabled rules)

0: 1,27 - 2,26 rule1
1: 2,27 - 4,25 rule1, rule2        rule2 is violated in this region
2: 4,26 - 6,25 rule1                  rule1 is violated in this region
3: 6,26 - EOF  no rules disabled

These regions are disjoint/non-overlapping. Our algorithm for detecting superfluous disable violations works fine when the disabled areas are non-overlapping, or non-nested, but in this particular nested case, we will get a false superfluous disable command for rule1 in region 0 and region 1.

decompose is perhaps not quite the right word - what we do here is rewrite the regions, so that there is a region for each rule. These regions can be overlapping. In this above case, we get:

1,27 - 6,25 rule1                rule1 is violated in this region
2,27 - 4,25 rule2               rule2 is violated in this region
6,26 - EOF no rules disabled

So we correctly get no superfluous disable command violations in this case. I didn't want to change how we represent regions elsewhere, so we just convert them here for this special case.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Okay. Got that. I wonder if we should then generally treat regions like that right from the beginning.

I didn't want to change how we represent regions elsewhere, so we just convert them here for this special case.

Understandable. We should keep that in mind, however. Looks like we can simplify a few things in this area.

@mildm8nnered mildm8nnered merged commit 9c2d0d5 into realm:main Oct 15, 2024
14 checks passed
@mildm8nnered mildm8nnered deleted the mildm8nnered-fix-nested-disable-commands-improved branch October 15, 2024 17:56
@Patrick-Kladek
Copy link

@SimplyDanny can we create a new (bugfix) release now that this is merged?

We use https://github.com/lukepistrol/SwiftLintPlugin, which bundles a precompiled version of Swiftlint via SPM to speed up builds. The plugin repo mirrors the releases from this repo and automatically creates a precompiled version via GitHub actions.

This would give everyone the benefits of this PR with fast builds.

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.

4 participants