-
Notifications
You must be signed in to change notification settings - Fork 442
WIP: Make convenience initializers with CodeGeneration #2142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
WIP: Make convenience initializers with CodeGeneration #2142
Conversation
CodeGeneration/Sources/generate-swift-syntax/LayoutNode+Extensions.swift
Outdated
Show resolved
Hide resolved
CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxNodesFile.swift
Outdated
Show resolved
Hide resolved
I only glanced over it and the direction looks good. Before going into full-steam review on this, I would like to see a list of all the types that we want to apply rules to. Just to make sure that the rules are general enough to fit the needs and that there are sufficiently many that it’s easier to generate the code than to hand-write it. |
@ahoppen, thank you! Yeah, I got excited, and first trying to make the end to end prototype and not care about how clean the code is. Then, I'll post the list — I remember you're interested in looking at that first. I doubt I know all of the instances of where we'll want convenience initializers, so it's a possibility that the rules won't be flexible enough. I think once I have the list and the prototype, I'll groom this for the first review. I expect about 10 initializers, but perhaps I will think of more on my own, too. If at any point we decide to go with a different approach, no hard feelings. I'm here to learn 🙃 |
I think you’re doing a great job here and I’m all in favor of generating code instead of handwriting it. |
@ahoppen, the proof of concept works! The code in this PR successfully generates one initializer for I've updated the initial comments with the list of next steps to emphasize getting the list of convenience init methods to generate first, then I'll clean up the code a little bit and split it into two commits — the infrastructure of the rules, and the list of actual nodes to process, so it's easier to review. I'll take a day away from this to let this sit, I'll re-reading the code after will give me a fresh lens, I'll spot more mistakes. |
2d01e9c
to
03c20f8
Compare
03c20f8
to
09e7d57
Compare
29d171a
to
6eacdba
Compare
@ahoppen, I've got some interesting findings!
Next steps:
|
I think it really depends how many cases like this we have. If |
Summary
Work in progress, not ready for a full-on review, ideas and feedback welcome!
This pull request closes #1984. Instead of doing one-off initializers by hand, like in #2127 and #2112, we add a code generation clause that makes the initializers based on
NodeInitRule
s.Approach overview
Node
gets a new property:rules: [NodeInitRule]
.NodeInitRule
hasnonOptionalChildName
andchildDefaultValues: [String: Token]
.nonOptionalChildName
and set defaults tochildDefaultValues.keys
.AttributeSyntax
:Next steps
Proof of concept
init
declarationinit
documentationinit
body: callself.init
with the parameters list.Getting ready for the 1st review
Polish
rules
validation:nonOptionalChildName
exists innode.children
and that it is optional (otherwise the rule does not make any sense)childDefaultValues
exists innode.children