Skip to content

Commit

Permalink
Fix: TipKit rule triggers an empty_count violation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ueeek committed Dec 28, 2024
1 parent 1d8af83 commit e69e152
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
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("let predicate = #Predicate<SwiftDataModel> { $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("let predicate = #Predicate<SwiftDataModel> { $0.list.↓count == 0 }"):
Example("let predicate = #Predicate<SwiftDataModel> { $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(Syntax(node).cast(MacroExpansionExprSyntax.self))
} else {
super.visit(node)
}
}
}
}

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

private extension MacroExpansionExprSyntax {
var isTipsRuleMacro: Bool {
self.macroName.text == "Rule" &&
self.arguments.isNotEmpty &&
self.trailingClosure != nil
}
}

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("let predicate = #Predicate<SwiftDataModel> { $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("let predicate = #Predicate<SwiftDataModel> { $0.list.↓count == 0 }"):
Example("let predicate = #Predicate<SwiftDataModel> { $0.list.isEmpty }"),
]

let description = EmptyCountRule.description
Expand Down

0 comments on commit e69e152

Please sign in to comment.