diff --git a/Sources/Frontend/Commands/ScanBehavior.swift b/Sources/Frontend/Commands/ScanBehavior.swift index fb68ee6b5..37d19e123 100644 --- a/Sources/Frontend/Commands/ScanBehavior.swift +++ b/Sources/Frontend/Commands/ScanBehavior.swift @@ -71,7 +71,7 @@ final class ScanBehavior { if !filteredResults.isEmpty, let baselinePath = configuration.writeBaseline { let usrs = filteredResults.flatMapSet { $0.usrs } - let baseline = Baseline.v1(usrs: usrs) + let baseline = Baseline.v1(usrs: usrs.sorted()) let data = try JSONEncoder().encode(baseline) try data.write(to: baselinePath.url) } diff --git a/Sources/PeripheryKit/Indexer/SourceLocation.swift b/Sources/PeripheryKit/Indexer/SourceLocation.swift index 6af1a8eae..650073a40 100644 --- a/Sources/PeripheryKit/Indexer/SourceLocation.swift +++ b/Sources/PeripheryKit/Indexer/SourceLocation.swift @@ -1,4 +1,5 @@ import Foundation +import SystemPackage class SourceLocation { let file: SourceFile @@ -14,6 +15,13 @@ class SourceLocation { self.hashValueCache = [file.hashValue, line, column].hashValue } + func relativeTo(_ path: FilePath) -> SourceLocation { + let newPath = file.path.relativeTo(path) + let newFile = SourceFile(path: newPath, modules: file.modules) + newFile.importStatements = file.importStatements + return SourceLocation(file: newFile, line: line, column: column) + } + // MARK: - Private private func buildDescription(path: String) -> String { diff --git a/Sources/PeripheryKit/Indexer/SwiftIndexer.swift b/Sources/PeripheryKit/Indexer/SwiftIndexer.swift index 91f7e218e..710d747c6 100644 --- a/Sources/PeripheryKit/Indexer/SwiftIndexer.swift +++ b/Sources/PeripheryKit/Indexer/SwiftIndexer.swift @@ -501,8 +501,7 @@ public final class SwiftIndexer: Indexer { graph.withLock { for param in params { - let paramDecl = param.declaration - paramDecl.parent = functionDecl + let paramDecl = param.makeDeclaration(withParent: functionDecl) functionDecl.unusedParameters.insert(paramDecl) graph.addUnsafe(paramDecl) diff --git a/Sources/PeripheryKit/Results/Baseline.swift b/Sources/PeripheryKit/Results/Baseline.swift index c04f5e821..ca764091d 100644 --- a/Sources/PeripheryKit/Results/Baseline.swift +++ b/Sources/PeripheryKit/Results/Baseline.swift @@ -2,12 +2,12 @@ import Foundation /// A baseline set of declarations that are excluded from results. public enum Baseline: Codable { - case v1(usrs: Set) + case v1(usrs: [String]) var usrs: Set { switch self { case .v1(let usrs): - return usrs + return Set(usrs) } } } diff --git a/Sources/PeripheryKit/SourceGraph/SourceGraph.swift b/Sources/PeripheryKit/SourceGraph/SourceGraph.swift index 6d1313f4f..f7911d239 100644 --- a/Sources/PeripheryKit/SourceGraph/SourceGraph.swift +++ b/Sources/PeripheryKit/SourceGraph/SourceGraph.swift @@ -260,7 +260,8 @@ public final class SourceGraph { func markUnusedModuleImport(_ statement: ImportStatement) { withLock { - let usr = "\(statement.location.description)-\(statement.module)" + let location = statement.location.relativeTo(.current) + let usr = "import-\(statement.module)-\(location)" let decl = Declaration(kind: .module, usrs: [usr], location: statement.location) decl.name = statement.module unusedModuleImports.insert(decl) diff --git a/Sources/PeripheryKit/Syntax/UnusedParameterParser.swift b/Sources/PeripheryKit/Syntax/UnusedParameterParser.swift index 8973da668..b9aba610a 100644 --- a/Sources/PeripheryKit/Syntax/UnusedParameterParser.swift +++ b/Sources/PeripheryKit/Syntax/UnusedParameterParser.swift @@ -63,11 +63,13 @@ final class Parameter: Item, Hashable { return secondName ?? firstName ?? "" } - var declaration: Declaration { + func makeDeclaration(withParent parent: Declaration) -> Declaration { let functionName = function?.fullName ?? "func()" - let usr = "\(functionName)-\(name)-\(location)" + let parentUsrs = parent.usrs.joined(separator: "-") + let usr = "param-\(name)-\(functionName)-\(parentUsrs)" let decl = Declaration(kind: .varParameter, usrs: [usr], location: location) decl.name = name + decl.parent = parent return decl }