8000 Sync swift-5.0-branch with master. by nkcsgexi · Pull Request #42 · swiftlang/swift-syntax · GitHub
[go: up one dir, main page]

Skip to content

Sync swift-5.0-branch with master. #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Sources/SwiftSyntax/Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ extension Syntax {
in file: URL,
afterLeadingTrivia: Bool = true
) -> SourceLocation {
let pos = afterLeadingTrivia ?
data.position :
data.positionAfterSkippingLeadingTrivia
let pos = afterLeadingTrivia ?
data.positionAfterSkippingLeadingTrivia :
data.position
return SourceLocation(file: file.path, position: pos)
}

Expand Down
26 changes: 26 additions & 0 deletions Tests/SwiftSyntaxTest/AbsolutePosition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class AbsolutePositionTestCase: XCTestCase {
("testTrivias", testTrivias),
("testImplicit", testImplicit),
("testWithoutSourceFileRoot", testWithoutSourceFileRoot),
("testSourceLocation", testSourceLocation),
]

public func testVisitor() {
Expand Down Expand Up @@ -139,4 +140,29 @@ public class AbsolutePositionTestCase: XCTestCase {
XCTAssertEqual(0, item.position.utf8Offset)
XCTAssertEqual(1, item.positionAfterSkippingLeadingTrivia.utf8Offset)
}

public func testSourceLocation() {
let url = URL(fileURLWithPath: "/tmp/test.swift")
let root = self.createSourceFile(2)
guard let secondReturnStmt = root.child(at: 0)?.child(at: 1) else {
fatalError("out of sync with createSourceFile")
}
let startLoc = secondReturnStmt.startLocation(in: url)
XCTAssertEqual(startLoc.line, 4)
XCTAssertEqual(startLoc.column, 18)

let startLocBeforeTrivia =
secondReturnStmt.startLocation(in: url, afterLeadingTrivia: false)
XCTAssertEqual(startLocBeforeTrivia.line, 3)
XCTAssertEqual(startLocBeforeTrivia.column, 1)

let endLoc = secondReturnStmt.endLocation(in: url)
XCTAssertEqual(endLoc.line, 4)
XCTAssertEqual(endLoc.column, 24)

let endLocAfterTrivia =
secondReturnStmt.endLocation(in: url, afterTrailingTrivia: true)
XCTAssertEqual(endLocAfterTrivia.line, 5)
XCTAssertEqual(endLocAfterTrivia.column, 1)
}
}
0