templ icon indicating copy to clipboard operation
templ copied to clipboard

fix: consistent whitespace around go keywords

Open JonnyLoughlin opened this issue 1 year ago • 0 comments

This PR is an attempted fix for #354. The issue seemed to be caused by the recursive nature of writeNodes. When calling writeNodes() from writeIfExpression(), writeForExpression(), and writeSwitchExpression(), the last iteration of the loop in writeNodes() wouldn't correctly know the next node in the node list.

For example:

templ page(doIf bool) {
	<button>Left</button>
	if doIf {
		<button>Middle</button>
	}
	<button>Right</button>
}

would produce a node list of [element]->[if expression]->[element] with [if expression] having a node list of [element]. On the final iteration of the loop in writeNodes() when writing the node list of [if expression], the next element would be nil instead of the next [element] node. This was messing with the isInlineOrText() function as passing it a nil node will return false so they expected whitespace isn't added.

I have fixed the issue by having writeNodes() also take in a next parser.Node so when the function is called recursively from writeIfExpression(), writeForExpression(), and writeSwitchExpression(), the next node in the node list can be passed in and used when relevant. If the function isn't being called recursively, we can still pass in nil as the next node and then writeNodes() will work as it did previously. If we pass in a non nil next to writeNodes(), it will be used as the next value when nextNode is nil ie. on the last iteration of the loop.

I've also added a few test for whitespace in if, switch and for expressions.

If there is anything that looks incorrect in this or if there is anything you'd like me to change, please let me know.

JonnyLoughlin avatar Dec 28 '23 17:12 JonnyLoughlin