Issues encountered building a formatter
Issues in the Cadence AST Doc() and Walk() methods that affect external formatting tools.
1. EntitlementDeclaration.Doc() uses HardLine for access modifiers
EntitlementDeclaration.Doc() and EntitlementMappingDeclaration.Doc() use prettier.HardLine{} after the access modifier, forcing a line break:
// entitlement_declaration.go:111
doc = append(doc, docOrEmpty(d.Access), prettier.HardLine{})
This produces:
access(all)
entitlement NodeOperator
All other declaration types (FunctionDeclaration, CompositeDeclaration, FieldDeclaration, etc.) use prettier.Line{}, which keeps the access modifier on the same line when it fits:
access(all) fun foo()
access(all) struct Bar {}
2. Move operator <- spacing inconsistency
The move operator <- is formatted inconsistently across Doc() methods:
return <-expr (no space after <-)
self.x <- expr (space around <-)
This appears to stem from different handling of TransferOperation rendering in return statements vs assignment statements.
3. InvocationExpression.Walk() doesn't yield Argument wrappers
InvocationExpression.Walk() yields arg.Expression for each argument, but not the Argument struct itself:
func (e *InvocationExpression) Walk(walkChild func(Element)) {
walkChild(e.InvokedExpression)
for _, typeArgument := range e.TypeArguments {
walkChild(typeArgument)
}
for _, argument := range e.Arguments {
walkChild(argument.Expression) // only the Expression, not the Argument
}
}
Since Argument has positional information (LabelStartPos, LabelEndPos, TrailingSeparatorPos), tools that map source positions to AST nodes (e.g., for comment attachment) cannot correctly associate comments that fall between argument labels and their expressions.
For comparison, ParameterList.Walk() yields individual Parameter elements.
Issues encountered building a formatter
Issues in the Cadence AST
Doc()andWalk()methods that affect external formatting tools.1.
EntitlementDeclaration.Doc()usesHardLinefor access modifiersEntitlementDeclaration.Doc()andEntitlementMappingDeclaration.Doc()useprettier.HardLine{}after the access modifier, forcing a line break:This produces:
All other declaration types (
FunctionDeclaration,CompositeDeclaration,FieldDeclaration, etc.) useprettier.Line{}, which keeps the access modifier on the same line when it fits:2. Move operator
<-spacing inconsistencyThe move operator
<-is formatted inconsistently acrossDoc()methods:return <-expr(no space after<-)self.x <- expr(space around<-)This appears to stem from different handling of
TransferOperationrendering in return statements vs assignment statements.3.
InvocationExpression.Walk()doesn't yieldArgumentwrappersInvocationExpression.Walk()yieldsarg.Expressionfor each argument, but not theArgumentstruct itself:Since
Argumenthas positional information (LabelStartPos,LabelEndPos,TrailingSeparatorPos), tools that map source positions to AST nodes (e.g., for comment attachment) cannot correctly associate comments that fall between argument labels and their expressions.For comparison,
ParameterList.Walk()yields individualParameterelements.