Skip to content

Commit 6255ff2

Browse files
committed
Fix isInPatternMatchingPatternPosition
1 parent 19db2ad commit 6255ff2

File tree

1 file changed

+8
-64
lines changed

1 file changed

+8
-64
lines changed

Source/SwiftLintBuiltInRules/Rules/Lint/MultilineCallArgumentsRule.swift

Lines changed: 8 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -178,71 +178,15 @@ private extension MultilineCallArgumentsRule {
178178
}
179179
}
180180

181-
// MARK: - Pattern filtering (precise, pattern-part only)
182-
183181
private extension FunctionCallExprSyntax {
184-
/// `true` only when this FunctionCall is used inside a *pattern* (e.g. `.caseOne(...)`),
185-
/// not just somewhere inside `if case` / `switch case` bodies.
182+
/// Returns `true` if this call appears in a pattern position (e.g., `case .foo(a)`).
183+
///
184+
/// Works because SwiftSyntax wraps pattern expressions in `ExpressionPatternSyntax`:
185+
/// - `if case let .foo(a) = x` → parent is ExpressionPatternSyntax
186+
/// - `switch x { case let .foo(a): }` → parent is ExpressionPatternSyntax
187+
/// - `for case let .foo(a) in items` → parent is ExpressionPatternSyntax
188+
/// - `catch .foo(1, 2)` → parent is ExpressionPatternSyntax
186189
var isInPatternMatchingPatternPosition: Bool {
187-
let selfSyntax = Syntax(self)
188-
var current: Syntax? = parent
189-
190-
var checkedExpressionPattern = false
191-
var checkedValueBindingPattern = false
192-
193-
while let node = current {
194-
if !checkedExpressionPattern, let expressionPattern = node.as(ExpressionPatternSyntax.self) {
195-
checkedExpressionPattern = true
196-
if selfSyntax.isInside(Syntax(expressionPattern.expression)) { return true }
197-
}
198-
199-
if !checkedValueBindingPattern, let valueBindingPattern = node.as(ValueBindingPatternSyntax.self) {
200-
checkedValueBindingPattern = true
201-
if selfSyntax.isInside(Syntax(valueBindingPattern.pattern)) { return true }
202-
}
203-
204-
if let condition = node.as(MatchingPatternConditionSyntax.self) {
205-
if selfSyntax.isInside(Syntax(condition.pattern)) { return true }
206-
break
207-
}
208-
209-
if let caseItem = node.as(SwitchCaseItemSyntax.self) {
210-
if selfSyntax.isInside(Syntax(caseItem.pattern)) { return true }
211-
break
212-
}
213-
214-
if let forStmt = node.as(ForStmtSyntax.self) {
215-
if selfSyntax.isInside(Syntax(forStmt.pattern)) { return true }
216-
break
217-
}
218-
219-
if let catchClause = node.as(CatchClauseSyntax.self) {
220-
for item in catchClause.catchItems {
221-
if let pattern = item.pattern,
222-
selfSyntax.isInside(Syntax(pattern)) {
223-
return true
224-
}
225-
}
226-
break
227-
}
228-
229-
current = node.parent
230-
}
231-
232-
return false
233-
}
234-
}
235-
236-
// MARK: - Generic helpers
237-
238-
private extension Syntax {
239-
/// Returns `true` if `self` is the `ancestor` node itself or is located inside its subtree.
240-
func isInside(_ ancestor: Syntax) -> Bool {
241-
var current: Syntax? = self
242-
while let node = current {
243-
if node.id == ancestor.id { return true }
244-
current = node.parent
245-
}
246-
return false
190+
parent?.is(ExpressionPatternSyntax.self) == true
247191
}
248192
}

0 commit comments

Comments
 (0)