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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FXIOS-8821 [SwiftLint] for_where #19889

Closed

Conversation

cavaevinci
Copy link
Contributor

馃摐 Tickets

Jira ticket
Github issue

馃挕 Description

Implemented for_where swiftlint rule

馃摑 Checklist

You have to check all boxes before merging

  • [鉁旓笍] Filled in the above information (tickets numbers and description of your work)
  • [鉁旓笍] Updated the PR name to follow our PR naming guidelines
  • [鉁旓笍] Wrote unit tests and/or ensured the tests suite is passing
  • [鉁旓笍] When working on UI, I checked and implemented accessibility (minimum Dynamic Text and VoiceOver)
  • [鉁旓笍] If needed, I updated documentation / comments for complex code and public methods
  • [鉁旓笍] If needed, added a backport comment (example @Mergifyio backport release/v120)

@cavaevinci cavaevinci requested a review from a team as a code owner April 19, 2024 15:29
Copy link
Contributor

mergify bot commented Apr 19, 2024

This pull request has conflicts when rebasing. Could you fix it @cavaevinci? 馃檹

@cavaevinci
Copy link
Contributor Author

@thatswinnie , fixed merge conflicts

@mobiletest-ci-bot
Copy link

Messages
馃摉 Edited 7 files
馃摉 Created 0 files

Generated by 馃毇 Danger Swift against 215f226

continue domainSearch
}
for rule in rules {
guard let _ = rule.regex.firstMatch(in: resourceString, options: .anchored, range: resourceRange) else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add the comment back in:
// First, test the top-level filters to see if this URL might be blocked.

guard let _ = rule.regex.firstMatch(in: resourceString, options: .anchored, range: resourceRange) else {
continue
}
let hasDomainException = rule.domainExceptions?.contains { domainRegex in
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add the comment back here as well:

// Check the domain exceptions. If a domain exception matches, this filter does not apply.

guard !hasDomainException else {
continue
}
if let baseDomain = url.baseDomain, !permittedDomains.isEmpty {
Copy link
Contributor

Choose a reason for hiding this comment

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

And this comment should be added back too:

// Check the whitelist.

return nil
}
}
guard !isPermittedDomain else {
Copy link
Contributor

Choose a reason for hiding this comment

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

If it is a permitted domain we should return nil instead of continuing.

weakDelegate.delegate = delegate
return
}
if let availableSlot = delegates.first(where: { $0.delegate == nil }) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add the comment back:

// Reuse any existing slots that have been deallocated.

Comment on lines +30 to +31
if let lastIndex = currentStack.firstIndex(where: { !$0.isCurrentSearch }) {
currentStack.removeSubrange(lastIndex + 1..<currentStack.count)
Copy link
Contributor

Choose a reason for hiding this comment

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

This change doesn't look correct. The stack should only be cleaned up if the lastSearch is not the current search.

Suggested change
if let lastIndex = currentStack.firstIndex(where: { !$0.isCurrentSearch }) {
currentStack.removeSubrange(lastIndex + 1..<currentStack.count)
// Check whether the lastSearch is the current search. If not, remove subsequent searches
if let lastSearch = currentStack.last,
!lastSearch.isCurrentSearch,
let lastIndex = currentStack.firstIndex(where: { !$0.isCurrentSearch }) {
currentStack.removeSubrange(lastIndex + 1..<currentStack.count)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Indeed, the change might not be correct. We want go and look for searches to be removed only if the last search is not the current search. Then we look for the first search that is current search and remove everything after it (not including it). After that we mark everything as not being current search and only afteeer that we add the new search and mark it accordingly. Ugh that was a lot. A similar implementation the might be more clear can be:

static func pushSearchToStack(with searchedText: String) {
    // Check if the stack is empty and directly append if it is
    guard !currentStack.isEmpty else {
        currentStack.append(textSearched(text: searchedText, isCurrentSearch: true))
        return
    }

    // Find the last `current search` and truncate the stack beyond this point
    if let lastIndex = currentStack.lastIndex(where: { $0.isCurrentSearch }) {
        currentStack = Array(currentStack.prefix(upTo: lastIndex + 1))
    }

    // Mark all as not current in a single pass and append new search
    currentStack = currentStack.map { textSearched(text: $0.text, isCurrentSearch: false) }
    currentStack.append(textSearched(text: searchedText, isCurrentSearch: true))
}

To show an example we start with
0: "apple" (isCurrentSearch = false)
1: "banana" (isCurrentSearch = false)
2: "cherry" (isCurrentSearch = true)
3: "date" (isCurrentSearch = false)

Then we truncate after index 2
0: "apple" (isCurrentSearch = false)
1: "banana" (isCurrentSearch = false)
2: "cherry" (isCurrentSearch = true)

We set everything to false
0: "apple" (isCurrentSearch = false)
1: "banana" (isCurrentSearch = false)
2: "cherry" (isCurrentSearch = false)

Then we add the search
0: "apple" (isCurrentSearch = false)
1: "banana" (isCurrentSearch = false)
2: "cherry" (isCurrentSearch = false)
3: "elderberry" (isCurrentSearch = true)

let hasDomainException = rule.domainExceptions?.contains { domainRegex in
domainRegex.firstMatch(in: resourceString, options: [], range: resourceRange) != nil
} ?? false
guard !hasDomainException else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Although guarding can make the early exits more readable in this case the inversion makes me a bit confused.
I would use if hasDomainException { continue } implying that if we indeed find an exception we skip to the next rule.

Copy link
Contributor

mergify bot commented Apr 24, 2024

This pull request has conflicts when rebasing. Could you fix it @cavaevinci? 馃檹

Copy link
Contributor

github-actions bot commented May 9, 2024

This PR has been automatically marked as stale. Please leave any comment to keep this PR opened. It will be closed automatically if no further update occurs in the next 7 days. Thank you for your contributions!

@github-actions github-actions bot added the stale Stalebot use this label to stale issues and PRs label May 9, 2024
@github-actions github-actions bot closed this May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Stalebot use this label to stale issues and PRs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants