Use SwiftSyntax itself to generate SwiftSyntax’s source code

I wouldn’t worry about formatting too much at this point. Formatting is a non-trivial problem to solve and I would suggest running a formatter like swift-format on the code generated by SwiftSyntaxBuilder but that would be a follow-up step after generating the actual code.

Yes, that’s a known limitation at the moment. @iMostfa has some ideas how to fix this in [GSoC 2022] Use SwiftSyntax itself to generate SwiftSyntax’s source code instead of GYB - #9 by iMostfa

The code is an accessor and not an initializer because every time you access the enum variable, the code in the braces gets executed to generate TokenSyntax representing the enum keyword. You can see this behavior if you e.g. put a print statement in the accessor. An initializer would be static var `enum`: TokenSyntax = SyntaxFactory.makeEnumKeyword().withTrailingTrivia(.spaces(1))

I agree that the syntax to generate variables is a little verbose at the moment. IMHO adding convenience initializers like the one you added in Tokens.gen.swift is the way forward here.

1 Like