Skip to content

SwiftSyntax: add an API to check whether a syntax node has underlying source. #15389

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 1 commit into from
Mar 21, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
43 changes: 27 additions & 16 deletions test/SwiftSyntax/AbsolutePosition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,34 +101,45 @@ PositionTests.test("Recursion") {
})
}

PositionTests.test("Trivias") {
expectDoesNotThrow({
let leading = Trivia(pieces: [
func createSourceFile(_ count: Int) -> SourceFileSyntax {
let leading = Trivia(pieces: [
.newlines(1),
.backticks(1),
.docLineComment("/// some comment")
])
let trailing = Trivia.docLineComment("/// This is comment\n")
let idx = 5
let items : [CodeBlockItemSyntax] =
[CodeBlockItemSyntax](repeating: CodeBlockItemSyntax {
$0.useItem(ReturnStmtSyntax {
$0.useReturnKeyword(
SyntaxFactory.makeReturnKeyword(
leadingTrivia: leading,
trailingTrivia: trailing))
})}, count: idx + 1)
let root = SyntaxFactory.makeSourceFile(
statements: SyntaxFactory.makeCodeBlockItemList(items),
eofToken: SyntaxFactory.makeToken(.eof, presence: .present))
let trailing = Trivia.docLineComment("/// This is comment\n")
let items : [CodeBlockItemSyntax] =
[CodeBlockItemSyntax](repeating: CodeBlockItemSyntax {
$0.useItem(ReturnStmtSyntax {
$0.useReturnKeyword(
SyntaxFactory.makeReturnKeyword(
leadingTrivia: leading,
trailingTrivia: trailing))
})}, count: count)
return SyntaxFactory.makeSourceFile(
statements: SyntaxFactory.makeCodeBlockItemList(items),
eofToken: SyntaxFactory.makeToken(.eof, presence: .present))
}

PositionTests.test("Trivias") {
expectDoesNotThrow({
let idx = 5
let root = createSourceFile(idx + 1)
expectEqual(root.leadingTrivia!.count, 3)
expectEqual(root.trailingTrivia!.count, 0)
let state = root.statements[idx]
expectEqual(state.leadingTrivia!.count, 3)
expectEqual(state.trailingTrivia!.count, 1)
expectEqual(state.leadingTrivia!.byteSize + state.trailingTrivia!.byteSize
+ state.byteSizeAfterTrimmingTrivia, state.byteSize)
expectFalse(root.statements.isImplicit)
})
}

PositionTests.test("Implicit") {
expectDoesNotThrow({
let root = createSourceFile(0)
expectTrue(root.statements.isImplicit)
})
}

Expand Down
6 changes: 6 additions & 0 deletions tools/SwiftSyntax/Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ extension Syntax {
return raw.trailingTrivia
}

/// When isImplicit is true, the syntax node doesn't include any
/// underlying tokens, e.g. an empty CodeBlockItemList.
public var isImplicit: Bool {
return leadingTrivia == nil
}

/// The textual byte length of this node exluding leading and trailing trivia.
public var byteSizeAfterTrimmingTrivia: Int {
return data.byteSize - (leadingTrivia?.byteSize ?? 0) -
Expand Down