swift-syntax icon indicating copy to clipboard operation
swift-syntax copied to clipboard

Add initializer to `LabeledExprSyntax` that automatically adds a colon if the label exists

Open ahoppen opened this issue 1 year ago • 17 comments

Currently, if you do the following

let node = EnumCaseParameterSyntax(firstName: "label", type: TypeSyntax("MyType"))
print(node.formatted())

you get label MyType, which is surprising because the colon is missing and you would expect the colon to be present if there is a first name of the enum case parameter.

To fix this, we should add a hand-written initializer of EnumCaseParameterSyntax, which takes a non-optional firstName and automatically adds a colon, because it knows that the firstName exists. The above call would pick that overload and thus add a colon.

And the same strategy can also be applied

  • to other nodes that have an optional colon
  • ClosureCaptureSyntax.equal
  • to automatically add parentheses for the following nodes if arguments are present
    • AttributeSyntax
    • MacroExpansionDeclSyntax
    • ClosureCaptureModifierSyntax
    • FunctionCallExprSyntax
    • FreestandingMacroExpansionSyntax

rdar://107794232

ahoppen avatar Aug 02 '23 21:08 ahoppen