Skip to content

[5.1]Introduce SyntaxVisitorBase #134

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

Merged
merged 1 commit into from
Jul 9, 2019
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
29 changes: 29 additions & 0 deletions Sources/SwiftSyntax/SyntaxRewriter.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,35 @@ public extension SyntaxAnyVisitor {
}
}

/// A class version of the `SyntaxVisitor` protocol. This is useful if you
/// intend to have subclasses overriding specific methods of a common base
/// `SyntaxVisitor` class.
///
/// It workarounds the issue of not being able to override the default
/// implementations of the protocol (see https://bugs.swift.org/browse/SR-103).
open class SyntaxVisitorBase: SyntaxVisitor {
public init() {}

% for node in SYNTAX_NODES:
% if is_visitable(node):
open func visit(_ node: ${node.name}) -> SyntaxVisitorContinueKind {
return .visitChildren
}
open func visitPost(_ node: ${node.name}) {}
% end
% end

open func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind {
return .visitChildren
}
open func visitPost(_ node: TokenSyntax) {}

open func visit(_ node: UnknownSyntax) -> SyntaxVisitorContinueKind {
return .visitChildren
}
open func visitPost(_ node: UnknownSyntax) {}
}

extension _SyntaxBase {
func walk<Visitor>(_ visitor: inout Visitor) where Visitor : SyntaxVisitor {
guard isPresent else { return }
Expand Down
19 changes: 19 additions & 0 deletions Tests/SwiftSyntaxTest/VisitorTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class SyntaxVisitorTestCase: XCTestCase {
("testRewritingNodeWithEmptyChild", testRewritingNodeWithEmptyChild),
("testSyntaxRewriterVisitAny", testSyntaxRewriterVisitAny),
("testSyntaxRewriterVisitCollection", testSyntaxRewriterVisitCollection),
("testVisitorClass", testVisitorClass),
]

public func testBasic() {
Expand Down Expand Up @@ -83,4 +84,22 @@ public class SyntaxVisitorTestCase: XCTestCase {
XCTAssertEqual(4, visitor.numberOfCodeBlockItems)
}())
}

public func testVisitorClass() {
class FuncCounter: SyntaxVisitorBase {
var funcCount = 0
override func visit(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
funcCount += 1
return super.visit(node)
}
}
XCTAssertNoThrow(try {
let parsed = try SyntaxParser.parse(getInput("visitor.swift"))
var counter = FuncCounter()
let hashBefore = parsed.hashValue
parsed.walk(&counter)
XCTAssertEqual(counter.funcCount, 3)
XCTAssertEqual(hashBefore, parsed.hashValue)
}())
}
}