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: TipKit rule triggers an empty_count violation #5918

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@

#### Bug Fixes

* Ignore TipKit Rule Macro in `empty_count` rule.
[Ueeek](https://github.com/Ueeek)
[#5883](https://github.com/realm/SwiftLint/issues/5883)

* Ignore super calls with trailing closures in `unneeded_override` rule.
[SimplyDanny](https://github.com/SimplyDanny)
[#5886](ttps://github.com/realm/SwiftLint/issues/5886)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct EmptyCountRule: Rule {
Example("[Int]().count == 0o07"),
Example("discount == 0"),
Example("order.discount == 0"),
Example("let rule = #Rule(Tips.Event(id: \"someTips\")) { $0.donations.count == 0 }"),
],
triggeringExamples: [
Example("[Int]().↓count == 0"),
Expand All @@ -32,6 +33,7 @@ struct EmptyCountRule: Rule {
Example("[Int]().↓count == 0b00"),
Example("[Int]().↓count == 0o00"),
Example("↓count == 0"),
Example("#ExampleMacro { $0.list.↓count == 0 }"),
],
corrections: [
Example("[].↓count == 0"):
Expand Down Expand Up @@ -62,6 +64,8 @@ struct EmptyCountRule: Rule {
Example("isEmpty && [Int]().isEmpty"),
Example("[Int]().count != 3 && [Int]().↓count != 0 || ↓count == 0 && [Int]().count > 2"):
Example("[Int]().count != 3 && ![Int]().isEmpty || isEmpty && [Int]().count > 2"),
Example("#ExampleMacro { $0.list.↓count == 0 }"):
Example("#ExampleMacro { $0.list.isEmpty }"),
]
)
}
Expand All @@ -77,6 +81,10 @@ private extension EmptyCountRule {
violations.append(position)
}
}

override func visit(_ node: MacroExpansionExprSyntax) -> SyntaxVisitorContinueKind {
node.isTipsRuleMacro ? .skipChildren : .visitChildren
}
}

final class Rewriter: ViolationsSyntaxRewriter<ConfigurationType> {
Expand Down Expand Up @@ -105,6 +113,14 @@ private extension EmptyCountRule {
}
return super.visit(node)
}

override func visit(_ node: MacroExpansionExprSyntax) -> ExprSyntax {
if node.isTipsRuleMacro {
ExprSyntax(node)
} else {
super.visit(node)
}
}
}
}

Expand Down Expand Up @@ -137,6 +153,14 @@ private extension TokenSyntax {
}
}

private extension MacroExpansionExprSyntax {
var isTipsRuleMacro: Bool {
macroName.text == "Rule" &&
arguments.count == 1 &&
trailingClosure.map { $0.statements.onlyElement?.item.is(ReturnStmtSyntax.self) == false } ?? false
}
}

private extension ExprSyntaxProtocol {
var negated: ExprSyntax {
ExprSyntax(PrefixOperatorExprSyntax(operator: .prefixOperator("!"), expression: self))
Expand Down
4 changes: 4 additions & 0 deletions Tests/SwiftLintFrameworkTests/EmptyCountRuleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class EmptyCountRuleTests: SwiftLintTestCase {
Example("discount == 0\n"),
Example("order.discount == 0\n"),
Example("count == 0\n"),
Example("let rule = #Rule(Tips.Event(id: \"someTips\")) { $0.donations.isEmpty }"),
]
let triggeringExamples = [
Example("[Int]().↓count == 0\n"),
Expand All @@ -23,6 +24,7 @@ final class EmptyCountRuleTests: SwiftLintTestCase {
Example("[Int]().↓count == 0x00_00\n"),
Example("[Int]().↓count == 0b00\n"),
Example("[Int]().↓count == 0o00\n"),
Example("#ExampleMacro { $0.list.↓count == 0 }"),
]

let corrections = [
Expand Down Expand Up @@ -54,6 +56,8 @@ final class EmptyCountRuleTests: SwiftLintTestCase {
Example("count == 0 && [Int]().isEmpty"),
Example("[Int]().count != 3 && [Int]().↓count != 0 || count == 0 && [Int]().count > 2"):
Example("[Int]().count != 3 && ![Int]().isEmpty || count == 0 && [Int]().count > 2"),
Example("#ExampleMacro { $0.list.↓count == 0 }"):
Example("#ExampleMacro { $0.list.isEmpty }"),
]

let description = EmptyCountRule.description
Expand Down