Skip to content

[JExtract] Santize trivia for declartion signature string #250

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
Jun 5, 2025
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
54 changes: 47 additions & 7 deletions Sources/JExtractSwift/Convenience/SwiftSyntax+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,19 @@ extension DeclSyntaxProtocol {
var signatureString: String {
return switch DeclSyntax(self.detached).as(DeclSyntaxEnum.self) {
case .functionDecl(let node):
node.with(\.body, nil).trimmedDescription
node.with(\.body, nil).triviaSanitizedDescription
case .initializerDecl(let node):
node.with(\.body, nil).trimmedDescription
node.with(\.body, nil).triviaSanitizedDescription
case .classDecl(let node):
node.with(\.memberBlock, "").trimmedDescription
node.with(\.memberBlock, "").triviaSanitizedDescription
case .structDecl(let node):
node.with(\.memberBlock, "").trimmedDescription
node.with(\.memberBlock, "").triviaSanitizedDescription
case .protocolDecl(let node):
node.with(\.memberBlock, "").trimmedDescription
node.with(\.memberBlock, "").triviaSanitizedDescription
case .accessorDecl(let node):
node.with(\.body, nil).trimmedDescription
node.with(\.body, nil).triviaSanitizedDescription
case .subscriptDecl(let node):
node.with(\.accessorBlock, nil).triviaSanitizedDescription
case .variableDecl(let node):
node
.with(\.bindings, PatternBindingListSyntax(
Expand All @@ -197,9 +199,47 @@ extension DeclSyntaxProtocol {
.with(\.initializer, nil)
}
))
.trimmedDescription
.triviaSanitizedDescription
default:
fatalError("unimplemented \(self.kind)")
}
}

/// Syntax text but without unnecessary trivia.
///
/// Connective trivia are condensed into a single whitespace, but no space
/// after opening or before closing parentheses
var triviaSanitizedDescription: String {
let visitor = TriviaSanitizingDescriptionVisitor(viewMode: .sourceAccurate)
visitor.walk(self.trimmed)
return visitor.result
}
}

class TriviaSanitizingDescriptionVisitor: SyntaxVisitor {
var result: String = ""

var prevTokenKind: TokenKind = .endOfFile
var prevHadTrailingSpace: Bool = false

override func visit(_ node: TokenSyntax) -> SyntaxVisitorContinueKind {
let tokenKind = node.tokenKind
switch (prevTokenKind, tokenKind) {
case
// No whitespace after open parentheses.
(.leftAngle, _), (.leftParen, _), (.leftSquare, _), (.endOfFile, _),
// No whitespace before close parentheses.
(_, .rightAngle), (_, .rightParen), (_, .rightSquare):
break
default:
if prevHadTrailingSpace || !node.leadingTrivia.isEmpty {
result += " "
}
}
result += node.text
prevTokenKind = tokenKind
prevHadTrailingSpace = !node.trailingTrivia.isEmpty

return .skipChildren
}
}
1 change: 0 additions & 1 deletion Sources/JExtractSwift/ImportedDecls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public final class ImportedFunc: ImportedDecl, CustomStringConvertible {
var translatedSignature: TranslatedFunctionSignature

public var signatureString: String {
// FIXME: Remove comments and normalize trivia.
self.swiftDecl.signatureString
}

Expand Down
9 changes: 7 additions & 2 deletions Tests/JExtractSwiftTests/MethodImportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ final class MethodImportTests {
import _StringProcessing
import _SwiftConcurrencyShims

public func helloWorld()
/// Hello World!
public func /*comment*/helloWorld()

public func globalTakeInt(i: Int)

public func globalTakeIntLongString(i32: Int32, l: Int64, s: String)
public func globalTakeIntLongString(
i32: Int32,
l: Int64,
s: String
)

public func globalReturnClass() -> MySwiftClass

Expand Down