enhance(template): Preserve line numbers #468
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.
This PR tweaks the
template
module to build intermediate template code where instructions have the same line numbers as in the original template.For example, with the old behaviour, the template
would have resulted in the following intermediate code:
If
bar()
were to produce an error, Lua would report it as occurring on line 8, which is completely unhelpful. However, after this PR, the above template will produce the following code:While less readable, any errors will now be reported as occurring on the same line as in the original template, which greatly eases debugging, since it's often easy to pinpoint the source of error just by looking at the right line in the template string.
Essentially, this PR only swaps out some newlines for spaces and semicolons; the actual rendering behaviour of templates should remain unchanged. It also works when the
newline
option is truthy; stripped newlines aren't removed completely, just moved out of the appender statements and into the intermediate Lua code.