Skip to content

Commit

Permalink
Add the ability to add comment commands on the same line as an import…
Browse files Browse the repository at this point in the history
… statement
  • Loading branch information
jszumski committed Feb 14, 2025
1 parent 239c7f6 commit 92a96ee
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/SyntaxAnalysis/ImportSyntaxVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public final class ImportSyntaxVisitor: PeripherySyntaxVisitor {
isTestable: attributes.contains("testable"),
isExported: attributes.contains("_exported") || node.modifiers.contains { $0.name.text == "public" },
location: location,
commentCommands: CommentCommand.parseCommands(in: node.leadingTrivia)
commentCommands: CommentCommand.parseCommands(in: node.leadingTrivia.merging(node.trailingTrivia))
)
importStatements.append(statement)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Foundation
// periphery:ignore
import CoreGraphics
import Swift // periphery:ignore
43 changes: 43 additions & 0 deletions Tests/PeripheryTests/Syntax/ImportVisitTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import Foundation
@testable import SourceGraph
@testable import SyntaxAnalysis
@testable import TestShared
import XCTest

final class ImportVisitTest: XCTestCase {
private var results: [ImportStatement]!

override func setUpWithError() throws {
try super.setUpWithError()
let multiplexingVisitor = try MultiplexingSyntaxVisitor(file: fixturePath)
let visitor = multiplexingVisitor.add(ImportSyntaxVisitor.self)
multiplexingVisitor.visit()
results = visitor.importStatements
}

override func tearDown() {
results = nil
super.tearDown()
}

func testCommentCommands() {
let expectedIgnored = ["CoreGraphics", "Swift"]
let actualIgnored = results.filter { $0.commentCommands.contains(.ignore) }.map(\.module)
XCTAssertEqual(actualIgnored, expectedIgnored, "Ignored modules did not match the expected set")

let actualUnignored = results.filter { !$0.commentCommands.contains(.ignore) }.map(\.module)
let expectedUnignored = ["Foundation"]
XCTAssertEqual(actualUnignored, expectedUnignored, "Unignored modules did not match the expected set")
}

// MARK: - Private

private var fixturePath: SourceFile {
let path = FixturesProjectPath.appending("Sources/DeclarationVisitorFixtures/ImportFixture.swift")
return SourceFile(path: path, modules: ["DeclarationVisitorFixtures"])
}

private func fixtureLocation(line: Int, column: Int = 9) -> Location {
Location(file: fixturePath, line: line, column: column)
}
}

0 comments on commit 92a96ee

Please sign in to comment.