Skip to content

Commit

Permalink
Preserve header comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalonunez committed Mar 31, 2023
1 parent 96d3406 commit 83d299d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
5 changes: 5 additions & 0 deletions Sources/ross/CommentRewriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ final class CommentRewriter: SyntaxRewriter {
DeclSyntax(node.cleaned(shouldRemovePlainComments: shouldRemovePlainComments))
}

// Imports are a special case. We want to preserve header comments.
override func visit(_ node: ImportDeclSyntax) -> DeclSyntax {
DeclSyntax(node)
}

override func visit(_ node: DeinitializerDeclSyntax) -> DeclSyntax {
DeclSyntax(node.cleaned(shouldRemovePlainComments: shouldRemovePlainComments))
}
Expand Down
54 changes: 43 additions & 11 deletions Tests/RossTests/RossTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ final class RossTests: XCTestCase {
try await cli.run()

let expected = #"""
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Foundation
import SwiftSyntax
Expand Down Expand Up @@ -374,6 +374,38 @@ final class RossTests: XCTestCase {
XCTAssertEqual(actual, expected)
}

func test_PreservesHeaderComment() async throws {
let fileURL = examplesDirectory.appendingPathComponent("Test.swift")

let file = #"""
/**
* Plot
* Copyright (c) John Sundell 2019
* MIT license, see LICENSE file for details
*/
import Foundation
"""#

XCTAssert(fileManager.createFile(atPath: fileURL.path, contents: file.data(using: .utf8)))

var cli = Ross(directory: examplesDirectory.path)
try await cli.run()

let expected = #"""
/**
* Plot
* Copyright (c) John Sundell 2019
* MIT license, see LICENSE file for details
*/
import Foundation
"""#

let actual = try String(contentsOf: fileURL, encoding: .utf8)
XCTAssertEqual(actual, expected)
}

// MARK: Private

private let fileManager = FileManager.default
Expand Down

0 comments on commit 83d299d

Please sign in to comment.