CST: Serialize and print EOF trivia properly #247
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The CST works by attaching trivia between tokens as trailing trivia of the previous token or leading trivia of the currently processed token. However, right now the CST does not serialize any trailing trivia of the file properly (e.g. whitespace such as newline at EOF, or files composed solely of comments). This is because we do not process a final token to attach leading trivia to.
This PR introduces a final "EOF" token that is composed solely of leading trivia at the end of the file. This is not a concept in the Luau AST itself, so we need to extend our parse result to carry the "eof" token separately, independent of the AstStatBlock.
An alternative approach could be to attach all trivia at the end of the file as trailing trivia of the last processed token. However, this ends up special casing this particular token and breaks the guarantee that the trailing trivia will only consist of trivia up to and including a single newline character. As such, downstream tooling would potentially need to handle final statement / expressions in files separately to all other statements / expressions if they cared about trivia processing (e.g., formatters). Also, this would not work for files consisting solely of trivia, as there is no token present.