Skip to content

Change visit(TokenSyntax) function of SyntaxRewriter to return TokenSyntax #981

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
Oct 19, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private func createTokenFormatFunction() -> FunctionDecl {

)
]),
output: "Syntax"
output: "TokenSyntax"
)
) {
VariableDecl("var leadingTrivia = node.leadingTrivia")
Expand Down Expand Up @@ -225,6 +225,6 @@ private func createTokenFormatFunction() -> FunctionDecl {
"""
)
SequenceExpr("lastRewrittenToken = rewritten")
ReturnStmt("return Syntax(rewritten)")
ReturnStmt("return rewritten")
}
}
4 changes: 2 additions & 2 deletions Sources/SwiftBasicFormat/generated/BasicFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2755,7 +2755,7 @@ open class BasicFormat: SyntaxRewriter {
return Syntax(VersionTupleSyntax(unexpectedBeforeMajorMinor, majorMinor: majorMinor, unexpectedBetweenMajorMinorAndPatchPeriod, patchPeriod: patchPeriod, unexpectedBetweenPatchPeriodAndPatchVersion, patchVersion: patchVersion))
}

open override func visit(_ node: TokenSyntax) -> Syntax {
open override func visit(_ node: TokenSyntax) -> TokenSyntax {
var leadingTrivia = node.leadingTrivia
var trailingTrivia = node.trailingTrivia
switch node.tokenKind {
Expand Down Expand Up @@ -3215,6 +3215,6 @@ open class BasicFormat: SyntaxRewriter {
presence: node.presence
)
lastRewrittenToken = rewritten
return Syntax(rewritten)
return rewritten
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ extension FixIt.Changes {
/// Makes the `token` present, moving it in front of the previous token's trivia.
static func makePresentBeforeTrivia(_ token: TokenSyntax) -> Self {
if let previousToken = token.previousToken(viewMode: .sourceAccurate) {
var presentToken = PresentMaker().visit(token).as(TokenSyntax.self)!
var presentToken = PresentMaker().visit(token)
if !previousToken.trailingTrivia.isEmpty {
presentToken = presentToken.withTrailingTrivia(previousToken.trailingTrivia)
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/SwiftParser/Diagnostics/PresenceUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension SyntaxProtocol {

/// Transforms a syntax tree by making all missing tokens present.
class PresentMaker: SyntaxRewriter {
override func visit(_ token: TokenSyntax) -> Syntax {
override func visit(_ token: TokenSyntax) -> TokenSyntax {
if token.presence == .missing {
let presentToken: TokenSyntax
let (rawKind, text) = token.tokenKind.decomposeToRaw()
Expand All @@ -57,7 +57,7 @@ class PresentMaker: SyntaxRewriter {
}
return BasicFormat().visit(presentToken)
} else {
return Syntax(token)
return token
}
}

Expand Down Expand Up @@ -126,10 +126,10 @@ class PresentMaker: SyntaxRewriter {
}

class MissingMaker: SyntaxRewriter {
override func visit(_ node: TokenSyntax) -> Syntax {
override func visit(_ node: TokenSyntax) -> TokenSyntax {
guard node.presence == .present else {
return Syntax(node)
return node
}
return Syntax(TokenSyntax(node.tokenKind, presence: .missing))
return TokenSyntax(node.tokenKind, presence: .missing)
}
}
6 changes: 3 additions & 3 deletions Sources/SwiftSyntax/SyntaxRewriter.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ open class SyntaxRewriter {
/// Visit a `TokenSyntax`.
/// - Parameter node: the node that is being visited
/// - Returns: the rewritten node
open func visit(_ token: TokenSyntax) -> Syntax {
return Syntax(token)
open func visit(_ token: TokenSyntax) -> TokenSyntax {
return token
}

/// Visit an `UnknownSyntax`.
Expand Down Expand Up @@ -123,7 +123,7 @@ open class SyntaxRewriter {
visitPre(node._syntaxNode)
defer { visitPost(node._syntaxNode) }
if let newNode = visitAny(node._syntaxNode) { return newNode }
return visit(node)
return Syntax(visit(node))
}

/// Implementation detail of visit(_:). Do not call directly.
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftSyntax/gyb_generated/SyntaxRewriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1973,8 +1973,8 @@ open class SyntaxRewriter {
/// Visit a `TokenSyntax`.
/// - Parameter node: the node that is being visited
/// - Returns: the rewritten node
open func visit(_ token: TokenSyntax) -> Syntax {
return Syntax(token)
open func visit(_ token: TokenSyntax) -> TokenSyntax {
return token
}

/// Visit an `UnknownSyntax`.
Expand Down Expand Up @@ -4884,7 +4884,7 @@ open class SyntaxRewriter {
visitPre(node._syntaxNode)
defer { visitPost(node._syntaxNode) }
if let newNode = visitAny(node._syntaxNode) { return newNode }
return visit(node)
return Syntax(visit(node))
}

/// Implementation detail of visit(_:). Do not call directly.
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftSyntaxBuilder/Indenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ class Indenter: SyntaxRewriter {
return Indenter(indentation: indentation).visit(Syntax(node)).as(SyntaxType.self)!
}

public override func visit(_ token: TokenSyntax) -> Syntax {
return Syntax(TokenSyntax(
public override func visit(_ token: TokenSyntax) -> TokenSyntax {
return TokenSyntax(
token.tokenKind,
leadingTrivia: token.leadingTrivia.indented(indentation: indentation),
trailingTrivia: token.trailingTrivia.indented(indentation: indentation),
presence: token.presence
))
)
}
}
4 changes: 2 additions & 2 deletions Sources/lit-test-helper/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,13 @@ func getSwiftLanguageVersionInfo(args: CommandLineArguments) -> (languageVersion

/// Rewrites a parsed tree with all constructed nodes.
class TreeReconstructor : SyntaxRewriter {
override func visit(_ token: TokenSyntax) -> Syntax {
override func visit(_ token: TokenSyntax) -> TokenSyntax {
let token = TokenSyntax(
token.tokenKind,
leadingTrivia: token.leadingTrivia,
trailingTrivia: token.trailingTrivia,
presence: token.presence)
return Syntax(token)
return token
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftParserTest/Assertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class FixItApplier: SyntaxRewriter {
return nil
}

override func visit(_ node: TokenSyntax) -> Syntax {
override func visit(_ node: TokenSyntax) -> TokenSyntax {
var modifiedNode = node
for change in changes {
switch change {
Expand All @@ -156,7 +156,7 @@ class FixItApplier: SyntaxRewriter {
break
}
}
return Syntax(modifiedNode)
return modifiedNode
}

/// If `messages` is `nil`, applies all Fix-Its in `diagnostics` to `tree` and returns the fixed syntax tree.
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftSyntaxParserTest/SyntaxVisitorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public class SyntaxVisitorTests: XCTestCase {

public func testRewriteTrivia() {
class TriviaRemover: SyntaxRewriter {
override func visit(_ token: TokenSyntax) -> Syntax {
return Syntax(token.withTrailingTrivia(.zero))
override func visit(_ token: TokenSyntax) -> TokenSyntax {
return token.withTrailingTrivia(.zero)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftSyntaxTest/VisitorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ public class VisitorTests: XCTestCase {
class RewritingTreePrinter: SyntaxRewriter {
var out = ""

override func visit(_ node: TokenSyntax) -> Syntax {
override func visit(_ node: TokenSyntax) -> TokenSyntax {
out += node.leadingTrivia.description
out += node.text
out += node.trailingTrivia.description
return Syntax(node)
return node
}


Expand Down