Skip to content

Commit 7c1c501

Browse files
authored
Merge pull request #1490 from ahoppen/ahoppen/trivia-invalid-utf8
Document that `Trivia` cannot represent invalid UTF-8
2 parents d3c4f24 + fe6ab43 commit 7c1c501

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

Sources/SwiftSyntax/Syntax.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,12 @@ public extension SyntaxProtocol {
471471
/// The leading trivia of this syntax node. Leading trivia is attached to
472472
/// the first token syntax contained by this node. Without such token, this
473473
/// property will return nil.
474+
///
475+
/// Note: `Trivia` is not able to represent invalid UTF-8 sequences. To get
476+
/// the leading trivia text including all invalid UTF-8 sequences, use
477+
/// ```
478+
/// node.syntaxTextBytes.prefix(self.leadingTriviaLength.utf8Length)
479+
/// ```
474480
var leadingTrivia: Trivia {
475481
get {
476482
return raw.formLeadingTrivia()
@@ -483,6 +489,12 @@ public extension SyntaxProtocol {
483489
/// The trailing trivia of this syntax node. Trailing trivia is attached to
484490
/// the last token syntax contained by this node. Without such token, this
485491
/// property will return nil.
492+
///
493+
/// Note: `Trivia` is not able to represent invalid UTF-8 sequences. To get
494+
/// the leading trivia text including all invalid UTF-8 sequences, use
495+
/// ```
496+
/// node.syntaxTextBytes[(node.byteSize - node.trailingTriviaLength.utf8Length)...]
497+
/// ```
486498
var trailingTrivia: Trivia {
487499
get {
488500
return raw.formTrailingTrivia()

0 commit comments

Comments
 (0)