Skip to content

Commit

Permalink
Exclude unused exported imports from results
Browse files Browse the repository at this point in the history
  • Loading branch information
ileitch authored and rofle100lvl committed Aug 28, 2024
1 parent 573afcd commit c6d40c7
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
##### Bug Fixes

- Enums with the `@main` attribute are now retained.
- Unused public/exported imports are excluded from the results even if unused in the declaring file as the exported symbols may be referenced in other files, and thus removing the import would result in a build failure.

## 2.21.0 (2024-06-15)

Expand Down
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax",
"state" : {
"revision" : "2bc86522d115234d1f588efe2bcb4ce4be8f8b82",
"version" : "510.0.3"
"revision" : "515f79b522918f83483068d99c68daeb5116342d",
"version" : "600.0.0-prerelease-2024-08-20"
}
},
{
Expand All @@ -77,8 +77,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/tuist/xcodeproj",
"state" : {
"revision" : "2b14180320b6c709597760ffde7b4c5a4a639153",
"version" : "8.23.0"
"revision" : "c15e011dc0ea74f7833bf33c2791483beb9c7577",
"version" : "8.23.1"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var dependencies: [Package.Dependency] = [
.package(url: "https://github.com/tadija/AEXML", from: "4.0.0"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
.package(url: "https://github.com/ileitch/swift-indexstore", from: "9.0.4"),
.package(url: "https://github.com/apple/swift-syntax", from: "510.0.3"),
.package(url: "https://github.com/apple/swift-syntax", from: "600.0.0-prerelease-2024-08-14"),
.package(url: "https://github.com/ileitch/swift-filename-matcher", from: "0.0.0"),
]

Expand Down
8 changes: 4 additions & 4 deletions Sources/Indexer/SourceFileCollector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public struct SourceFileCollector {

let file = FilePath.makeAbsolute(filePath, relativeTo: currentFilePath)

guard file.exists else {
throw PeripheryError.pathDoesNotExist(path: file.string)
}

if !isExcluded(file) {
guard file.exists else {
throw PeripheryError.pathDoesNotExist(path: file.string)
}

let module = try indexStore.moduleName(for: unit)
if let module, excludedTargets.contains(module) {
return nil
Expand Down
9 changes: 6 additions & 3 deletions Sources/SourceGraph/Mutators/UnusedImportMarker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ final class UnusedImportMarker: SourceGraphMutator {

let unreferencedImports = file.importStatements
.filter {
// Only consider modules that have been indexed as we need to see which modules
// they export.
graph.indexedModules.contains($0.module) &&
// Exclude exported/public imports because even though they may be unreferenced
// in the current file, their exported symbols may be referenced in others.
!$0.isExported &&
// Consider modules that have been indexed as we need to see which modules
// they export.
graph.indexedModules.contains($0.module) &&
!referencedModules.contains($0.module)
}

Expand Down
10 changes: 8 additions & 2 deletions Sources/SyntaxAnalysis/ImportSyntaxVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ public final class ImportSyntaxVisitor: PeripherySyntaxVisitor {
public func visit(_ node: ImportDeclSyntax) {
let parts = node.path.map(\.name.text)
let module = parts.first ?? ""
let attributes = node.attributes.compactMap { $0.as(AttributeSyntax.self)?.attributeName.trimmedDescription }
let attributes = node.attributes.compactMap {
if case let .attribute(attr) = $0 {
attr.attributeName.trimmedDescription
} else {
nil
}
}
let location = sourceLocationBuilder.location(at: node.positionAfterSkippingLeadingTrivia)
let statement = ImportStatement(
module: module,
isTestable: attributes.contains("testable"),
isExported: attributes.contains("_exported"),
isExported: attributes.contains("_exported") || node.modifiers.contains { $0.name.text == "public" },
location: location
)
importStatements.append(statement)
Expand Down

0 comments on commit c6d40c7

Please sign in to comment.