Skip to content

Incorrect code formatting result (flipped conditional logic) #794

@willhoney7

Description

@willhoney7

Okay, I know this is strange code, but the standard formatter completely flipped the conditional logic. I did a full codebase-wide formatting update and we started getting errors from this.

Version: 1.54.0

Input

class FormatTest
  def payload
    {}
  end

  def check_format
    unless payload.is_a?(Array) &&
           !payload.blank? &&
           payload.first.try(:[], :effective_date);

           fail UnprocessableError
    end
  end
end

The standard report

standard: Use Ruby Standard Style (https://github.com/standardrb/standard)
  format_test.rb:1:3: Layout/InitialIndentation: Indentation of first line in file detected.
  format_test.rb:1:13: Layout/IndentationWidth: Use 2 (not 4) spaces for indentation.
  format_test.rb:3:1: Layout/DefEndAlignment: `end` at 3, 2 is not aligned with `def` at 1, 0.
  format_test.rb:5:1: Layout/IndentationConsistency: Inconsistent indentation detected.
  format_test.rb:6:5: Style/IfWithSemicolon: Do not use `if payload.is_a?(Array) &&            !payload.blank? &&            payload.first.try(:[], :effective_date);` - use a ternary operator instead.
  format_test.rb:6:3: Style/TernaryParentheses: Use parentheses for ternary expressions with complex conditions.
  format_test.rb:7:3: Layout/MultilineOperationIndentation: Use 2 (not 7) spaces for indenting an expression spanning multiple lines.
  format_test.rb:7:12: Layout/MultilineOperationIndentation: Use 4 (not 7) spaces for indenting a condition in an `unless` statement spanning multiple lines.
  format_test.rb:8:3: Layout/MultilineOperationIndentation: Use 2 (not 7) spaces for indenting an expression spanning multiple lines.
  format_test.rb:8:12: Layout/MultilineOperationIndentation: Use 4 (not 7) spaces for indenting a condition in an `unless` statement spanning multiple lines.
  format_test.rb:8:51: Style/Semicolon: Do not use semicolons to terminate expressions.
  format_test.rb:10:5: Layout/IndentationWidth: Use 2 (not 7) spaces for indentation.
  format_test.rb:12:6: Layout/TrailingEmptyLines: Final newline missing.
standard: Run `standardrb --fix` to fix up to 13 problems.

Fix Output

class FormatTest
  def payload
    {}
  end

  def check_format
    (payload.is_a?(Array) &&
      !payload.blank? &&
      payload.first.try(:[], :effective_date)) ? fail(UnprocessableError) : nil
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions