fix: normalize leading whitespaces in multiline go code#1305
Merged
Conversation
Contributor
|
I'd say this is not the right way to fix the bug, instead I'd change templ/parser/v2/expressionparser.go Line 32 in 7be7dd6 to var spaceOrTabOrNewLine = parse.Any(spaceOrTab, parse.String("\r\n"), parse.Rune('\n'))This space is already handled by parser templ/parser/v2/gocodeparser.go Line 21 in 7be7dd6 and it works on Linux because Linux uses \n as newline character. To fix the bug we simply have to add \r\n as potential newline character.
|
Contributor
Author
|
That's a good point by MilkeeyCat, my bad for not looking through the code base thoroughly enough. I have tested the change and it works fine. Commit has been updated. |
Owner
|
Nice change. Thanks folks. Is this a Windows only problem, hence it not showing up in the tests etc.? |
Contributor
Yes, only Windows uses |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
I had an issue where templ fmt would add an extra newline and leading tabs in the beginning of every multiline go code expression in templates. My case looked like this:
which got formatted to:
Every time the formatter ran a new newline would appear above ' rawUrl := "" '
I noticed that the Go Code Writer always adds a number of tabs depending on the indentation and then a newline ahead of the go code so I fixed the issue by trimming any leading \r \n and \t characters before writing the node, thus normalizing the string so it always starts with exactly one newline and the correct number of tabs.