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

Respect SyntaxVisitorContinueKind of rules when run in Pipeline #659

Merged
merged 2 commits into from
Oct 26, 2023
Merged
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
24 changes: 23 additions & 1 deletion Sources/SwiftFormat/Core/LintPipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ extension LintPipeline {
_ visitor: (Rule) -> (Node) -> SyntaxVisitorContinueKind, for node: Node
) {
guard context.isRuleEnabled(Rule.self, node: Syntax(node)) else { return }
let ruleId = ObjectIdentifier(Rule.self)
guard self.shouldSkipChildren[ruleId] == nil else { return }
let rule = self.rule(Rule.self)
_ = visitor(rule)(node)
let continueKind = visitor(rule)(node)
if case .skipChildren = continueKind {
self.shouldSkipChildren[ruleId] = node
}
}

/// Calls the `visit` method of a rule for the given node if that rule is enabled for the node.
Expand All @@ -50,10 +55,27 @@ extension LintPipeline {
// cannot currently be expressed as constraints without duplicating this function for each of
// them individually.
guard context.isRuleEnabled(Rule.self, node: Syntax(node)) else { return }
guard self.shouldSkipChildren[ObjectIdentifier(Rule.self)] == nil else { return }
let rule = self.rule(Rule.self)
_ = visitor(rule)(node)
}

/// Cleans up any state associated with `rule` when we leave syntax node `node`
///
/// - Parameters:
/// - rule: The type of the syntax rule we're cleaning up.
/// - node: The syntax node htat our traversal has left.
func onVisitPost<R: Rule, Node: SyntaxProtocol>(
rule: R.Type, for node: Node
) {
let rule = ObjectIdentifier(rule)
if case .some(let skipNode) = self.shouldSkipChildren[rule] {
if node.id == skipNode.id {
self.shouldSkipChildren.removeValue(forKey: rule)
}
}
}

/// Retrieves an instance of a lint or format rule based on its type.
///
/// There is at most 1 instance of each rule allocated per `LintPipeline`. This method will
Expand Down
Loading