Skip to content

Commit cbf840f

Browse files
committed
SwiftSyntax: Allow absolute position access for dangling nodes.
Since absolute position is defined by accumulating proceeding nodes, we should allow its access for nodes without SourceFileSyntax as root.
1 parent ebf12c3 commit cbf840f

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

test/SwiftSyntax/AbsolutePosition.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,16 @@ PositionTests.test("Implicit") {
144144
})
145145
}
146146

147+
PositionTests.test("WithoutSourceFileRoot") {
148+
expectDoesNotThrow({
149+
let item = SyntaxFactory.makeCodeBlockItem(
150+
item: SyntaxFactory.makeReturnStmt(
151+
returnKeyword: SyntaxFactory.makeToken(.returnKeyword, presence: .present)
152+
.withLeadingTrivia(.newlines(1)).withTrailingTrivia(.newlines(1)),
153+
expression: nil), semicolon: nil)
154+
expectEqual(0, item.position.utf8Offset)
155+
expectEqual(1, item.positionAfterSkippingLeadingTrivia.utf8Offset)
156+
})
157+
}
158+
147159
runAllTests()

tools/SwiftSyntax/RawSyntax.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,4 @@ extension RawSyntax {
257257
piece.accumulateAbsolutePosition(pos)
258258
}
259259
}
260-
261-
var isSourceFile: Bool {
262-
switch self {
263-
case .node(let kind, _, _):
264-
return kind == SyntaxKind.sourceFile
265-
default:
266-
return false
267-
}
268-
}
269260
}

tools/SwiftSyntax/SyntaxData.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ final class SyntaxData: Equatable {
4343
fileprivate func calculatePosition(_ initPos: AbsolutePosition) ->
4444
AbsolutePosition {
4545
guard let parent = parent else {
46-
assert(raw.isSourceFile, "cannot find SourceFileSyntax as root")
4746
// If this node is SourceFileSyntax, its location is the start of the file.
4847
return initPos
4948
}
@@ -78,7 +77,6 @@ final class SyntaxData: Equatable {
7877
// If this node is root, the position of the next sibling is the end of
7978
// this node.
8079
guard let parent = parent else {
81-
assert(raw.isSourceFile, "cannot find SourceFileSyntax as root")
8280
let result = self.position.copy()
8381
raw.accumulateAbsolutePosition(result)
8482
return result

0 commit comments

Comments
 (0)