Skip to content
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

Indent rule doesn't handle conditional assignment with line break after = operator #1575

Closed
calda opened this issue Nov 9, 2023 · 5 comments
Labels
fixed in develop bug/feature resolved in the develop branch

Comments

@calda
Copy link
Collaborator

calda commented Nov 9, 2023

The indent rule doesn't properly handle conditional assignment expressions when formatted with a line break after the = operator.

Take this example if expression from SE-0380:

let bullet =
    if isRoot && (count == 0 || !willExpand) { "" }
    else if count == 0 { "- " }
    else if maxDepth <= 0 { "" }
    else { "" }

print(bullet)

on develop, this is unexpectedly re-indented to:

let bullet =
    if isRoot && (count == 0 || !willExpand) { "" }
else if count == 0 { "- " }
else if maxDepth <= 0 { "" }
else { "" }

print(bullet)

Since this is the formatting used in SE-0380, it seems like this will be a common preferred way to format these sorts of conditional assignment expressions.

@calda
Copy link
Collaborator Author

calda commented Nov 9, 2023

I tried to fix this as a part of #1574, and made some progress.

My change indents this example correctly:

let foo =
  if let foo {
    bar
  } else {
    baaz
  }

but clearly leaves the indentation stack in the wrong state, because if you add a print statement on the following line it unexpectedly indents that to:

let foo =
  if let foo {
    bar
  } else {
    baaz
  }

  print("Unexpectedly indented, whoops")

and it also has multiple issues with the above example, outputting:

let bullet =
    if isRoot && (count == 0 || !willExpand) { "" }
        else if count == 0 { "- " }
        else if maxDepth <= 0 { "" }
        else { "" }

        print(bullet)

@calda
Copy link
Collaborator Author

calda commented Nov 9, 2023

@nicklockwood, I spent a few hours trying to fix this as a part of #1574 and wasn't able to make much progress. I was wondering if you could take a pass on it and see if you're able to implement a better fix than me. If so it would be much appreciated!

@nicklockwood
Copy link
Owner

@calda thanks for looking into this. Yeah the indent logic is not the easiest thing to decipher. I'll have a stab at it.

@calda
Copy link
Collaborator Author

calda commented Nov 16, 2023

@nicklockwood, I revisited this with a different approach in e6b98ed and was able to get all of the tests passing :)

@nicklockwood nicklockwood added the fixed in develop bug/feature resolved in the develop branch label Nov 26, 2023
@nicklockwood
Copy link
Owner

Landed in 0.52.11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in develop bug/feature resolved in the develop branch
Projects
None yet
Development

No branches or pull requests

2 participants