Skip to content

Air breaks code with a variable called else_* #499

@lorenzwalthert

Description

@lorenzwalthert

Under certain circumstances, air breaks my code. E.g. running air format test.R

# contents of test.R
function() {

  if (TRUE) {
    1
  }

  else_idx <- 1
  if (FALSE) {
    1
  }
}

I get

function() {
  if (TRUE) {
    1
  } else { # <- this line is modified
    _idx <- 1 # <- this line is modified
  } # <- this line is modified
  if (FALSE) {
    1
  }
}

Which should not happen. Seems like else_idx assignment is somehow mistaken as an else statement. {styler}'s rules are mostly based on the tokenized code and not on the text of a token which would provide a first layer of defense against such problems. I guess you do something similar in air? Always, the token is not else but else_*, so I am surprised this bug still occurs. I admit it's an unusual code snippet, but changing users' code (and in this case, the meaning of the code) is in any case an urgent matter. FWIW, when removing if(TRUE) {1} the problem also seems to vanish. I wonder if there are other similar problems with partial token matching.

In {styler}, we assert that the output is parsable. I don't know if you have such a runtime check, but in the above case, it wouldn't help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    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