Skip to content

[Formatting issue] Chaining null-aware member access with if-null formats weirdly #1727

Open
@C0Florent

Description

@C0Florent

What input code did you provide to the formatter?

String melosVersion = argResults
    ?.option("melos-version-specification")
    ?.nonEmpty()
    ?? "7.0.0-dev.9";

What output did the formatter produce?

String melosVersion =
    argResults?.option("melos-version-specification")?.nonEmpty() ??
        "7.0.0-dev.9";

What output did you expect or want the formatter to produce?

I think a better format would be my original input, we can clearly read the chaining member access operators and the ending if-null on the start of a line rather than on the end of a line.

String melosVersion = argResults
    ?.option("melos-version-specification")
    ?.nonEmpty()
    ?? "7.0.0-dev.9";

To me this reads much better because operators on line endings are very easy to miss, thus it is easy to miss (not see) the ?? at the end of the line and wonder why there is an indented string on the following line.

Anything else we should know?

Interestingly, if the member access chain is longer, it is somewhat better formatted already :

String melosVersion = argResults
        ?.option("melos-version-specification")
        ?.nonEmpty()
        .toString() ??
    "7.0.0-dev.9";

Although I still dislike the ?? on the end of line, and have no idea why the default string would be deindented compared to the member acccess chain

Perhaps this could be implemented by treating a ?? at the end of a ?. chain the same as . member access for formatting. Examples of what I mean:

Treating the following

      String? melosVersion = argResults
          ?.option("melos-version-specification")
          ?.nonEmpty()
          .toString();

The same as this:

      String melosVersion = argResults
          ?.option("melos-version-specification")
          ?.nonEmpty()
          ?? "7.0.0-dev.9";

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions