Skip to content

Commit 9050542

Browse files
committed
SwiftFixIt: Require that locations are resolved in the right source file
1 parent d9e6ad6 commit 9050542

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

Sources/SwiftFixIt/SwiftFixit.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,12 @@ private struct SourceFile {
219219
)
220220
}
221221

222-
func position(of location: borrowing some AnySourceLocation) -> AbsolutePosition {
222+
func position(of location: borrowing some AnySourceLocation) throws -> AbsolutePosition {
223+
guard try AbsolutePath(validating: location.filename) == self.path else {
224+
// Wrong source file.
225+
throw Error.failedToResolveSourceLocation
226+
}
227+
223228
guard location.offset == 0 else {
224229
return AbsolutePosition(utf8Offset: Int(location.offset))
225230
}
@@ -231,7 +236,7 @@ private struct SourceFile {
231236
}
232237

233238
func node(at location: some AnySourceLocation) throws -> Syntax {
234-
let position = position(of: location)
239+
let position = try position(of: location)
235240

236241
if let token = syntax.token(at: position) {
237242
return SwiftSyntax.Syntax(token)
@@ -305,10 +310,10 @@ extension DiagnosticConverter {
305310
private static func fixIt(
306311
from diagnostic: borrowing some AnyDiagnostic,
307312
in sourceFile: borrowing SourceFile
308-
) -> SwiftDiagnostics.FixIt {
309-
let changes = diagnostic.fixIts.map { fixIt in
310-
let startPosition = sourceFile.position(of: fixIt.start)
311-
let endPosition = sourceFile.position(of: fixIt.end)
313+
) throws -> SwiftDiagnostics.FixIt {
314+
let changes = try diagnostic.fixIts.map { fixIt in
315+
let startPosition = try sourceFile.position(of: fixIt.start)
316+
let endPosition = try sourceFile.position(of: fixIt.end)
312317

313318
return SwiftDiagnostics.FixIt.Change.replaceText(
314319
range: startPosition ..< endPosition,
@@ -325,8 +330,8 @@ extension DiagnosticConverter {
325330
in sourceFile: borrowing SourceFile
326331
) throws -> [Syntax] {
327332
try diagnostic.ranges.map { startLocation, endLocation in
328-
let startPosition = sourceFile.position(of: startLocation)
329-
let endPosition = sourceFile.position(of: endLocation)
333+
let startPosition = try sourceFile.position(of: startLocation)
334+
let endPosition = try sourceFile.position(of: endLocation)
330335

331336
var highlightedNode = try sourceFile.node(at: startLocation)
332337

Tests/SwiftFixItTests/SwiftFixItTests.swift

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -575,31 +575,35 @@ extension SwiftFixItTests {
575575
}
576576
}
577577

578-
func testFixItInDifferentFile() throws {
579-
XCTExpectFailure()
580-
581-
// Apply a fix-it in a different file.
582-
try self.testAPI2Files { (filename1: String, filename2: String) in
583-
.init(
584-
edits: (
585-
.init(input: "var x = 1", result: "let x = 1"),
586-
.init(input: "", result: "")
587-
),
588-
diagnostics: [
589-
TestDiagnostic(
590-
text: "error",
591-
level: .error,
592-
location: .init(filename: filename2, line: 1, column: 1, offset: 0),
593-
fixIts: [
594-
.init(
595-
start: .init(filename: filename1, line: 1, column: 1, offset: 0),
596-
end: .init(filename: filename1, line: 1, column: 4, offset: 0),
597-
text: "let"
598-
),
599-
]
578+
func testFixItInDifferentFile() {
579+
do {
580+
try self.testAPI2Files { (filename1: String, filename2: String) in
581+
.init(
582+
edits: (
583+
.init(input: "var x = 1", result: "let x = 1"),
584+
.init(input: "", result: "")
600585
),
601-
]
602-
)
586+
diagnostics: [
587+
TestDiagnostic(
588+
text: "error",
589+
level: .error,
590+
location: .init(filename: filename2, line: 1, column: 1, offset: 0),
591+
fixIts: [
592+
.init(
593+
start: .init(filename: filename1, line: 1, column: 1, offset: 0),
594+
end: .init(filename: filename1, line: 1, column: 4, offset: 0),
595+
text: "let"
596+
),
597+
]
598+
),
599+
]
600+
)
601+
}
602+
} catch {
603+
// Expected to throw an error.
604+
return
603605
}
606+
607+
XCTFail()
604608
}
605609
}

0 commit comments

Comments
 (0)