Skip to content

Issues encountered building a formatter #4484

Description

@janezpodhostnik

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions