mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Based on the feedbacks from our early adopters, separating syntax tree analysis with transformation is a common pattern. Thus, we introduce a read-only syntax tree visitor to help the analysis phase. This visitor never alters the content of a tree being visited, in contrast to SyntaxRewriter which always does.
38 lines
911 B
Swift
38 lines
911 B
Swift
// RUN: %target-run-simple-swift
|
|
// REQUIRES: executable_test
|
|
// REQUIRES: OS=macosx
|
|
// REQUIRES: objc_interop
|
|
|
|
import StdlibUnittest
|
|
import Foundation
|
|
import SwiftSyntax
|
|
|
|
func getInput(_ file: String) -> URL {
|
|
var result = URL(fileURLWithPath: #file)
|
|
result.deleteLastPathComponent()
|
|
result.appendPathComponent("Inputs")
|
|
result.appendPathComponent(file)
|
|
return result
|
|
}
|
|
|
|
var VisitorTests = TestSuite("SyntaxVisitor")
|
|
|
|
VisitorTests.test("Basic") {
|
|
class FuncCounter: SyntaxVisitor {
|
|
var funcCount = 0
|
|
override func visit(_ node: FunctionDeclSyntax) {
|
|
funcCount += 1
|
|
super.visit(node)
|
|
}
|
|
}
|
|
expectDoesNotThrow({
|
|
let parsed = try Syntax.parse(getInput("visitor.swift"))
|
|
let counter = FuncCounter()
|
|
let hashBefore = parsed.hashValue
|
|
counter.visit(parsed)
|
|
expectEqual(counter.funcCount, 3)
|
|
expectEqual(hashBefore, parsed.hashValue)
|
|
})
|
|
}
|
|
|
|
runAllTests() |