Skip to content

Commit

Permalink
Fix usrs containing absolute paths, sort usrs.
Browse files Browse the repository at this point in the history
  • Loading branch information
ileitch committed Jun 2, 2024
1 parent 0a22202 commit 94c5be2
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Sources/Frontend/Commands/ScanBehavior.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 8 additions & 0 deletions Sources/PeripheryKit/Indexer/SourceLocation.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import SystemPackage

class SourceLocation {
let file: SourceFile
Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions Sources/PeripheryKit/Indexer/SwiftIndexer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions Sources/PeripheryKit/Results/Baseline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import Foundation

/// A baseline set of declarations that are excluded from results.
public enum Baseline: Codable {
case v1(usrs: Set<String>)
case v1(usrs: [String])

var usrs: Set<String> {
switch self {
case .v1(let usrs):
return usrs
return Set(usrs)
}
}
}
3 changes: 2 additions & 1 deletion Sources/PeripheryKit/SourceGraph/SourceGraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions Sources/PeripheryKit/Syntax/UnusedParameterParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 94c5be2

Please sign in to comment.