Skip to content

Commit

Permalink
Merge pull request swiftlang#16350 from nkcsgexi/swift-syntax-absolut…
Browse files Browse the repository at this point in the history
…e-pos-file

SwiftSyntax: Allow absolute position access for dangling nodes.
  • Loading branch information
nkcsgexi authored May 3, 2018
2 parents 0107e93 + cbf840f commit e7b3db3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
12 changes: 12 additions & 0 deletions test/SwiftSyntax/AbsolutePosition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,16 @@ PositionTests.test("Implicit") {
})
}

PositionTests.test("WithoutSourceFileRoot") {
expectDoesNotThrow({
let item = SyntaxFactory.makeCodeBlockItem(
item: SyntaxFactory.makeReturnStmt(
returnKeyword: SyntaxFactory.makeToken(.returnKeyword, presence: .present)
.withLeadingTrivia(.newlines(1)).withTrailingTrivia(.newlines(1)),
expression: nil), semicolon: nil)
expectEqual(0, item.position.utf8Offset)
expectEqual(1, item.positionAfterSkippingLeadingTrivia.utf8Offset)
})
}

runAllTests()
9 changes: 0 additions & 9 deletions tools/SwiftSyntax/RawSyntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,4 @@ extension RawSyntax {
piece.accumulateAbsolutePosition(pos)
}
}

var isSourceFile: Bool {
switch self {
case .node(let kind, _, _):
return kind == SyntaxKind.sourceFile
default:
return false
}
}
}
2 changes: 0 additions & 2 deletions tools/SwiftSyntax/SyntaxData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ final class SyntaxData: Equatable {
fileprivate func calculatePosition(_ initPos: AbsolutePosition) ->
AbsolutePosition {
guard let parent = parent else {
assert(raw.isSourceFile, "cannot find SourceFileSyntax as root")
// If this node is SourceFileSyntax, its location is the start of the file.
return initPos
}
Expand Down Expand Up @@ -78,7 +77,6 @@ final class SyntaxData: Equatable {
// If this node is root, the position of the next sibling is the end of
// this node.
guard let parent = parent else {
assert(raw.isSourceFile, "cannot find SourceFileSyntax as root")
let result = self.position.copy()
raw.accumulateAbsolutePosition(result)
return result
Expand Down

0 comments on commit e7b3db3

Please sign in to comment.