Skip to content

Conversation

bendavies
Copy link

@bendavies bendavies commented Jul 15, 2025

primarily this resolves the "WARNING: unclosed parentheses or section" error that occurred when parsing SQL statements containing ELSEIF keywords in IF/ELSEIF/ELSE/END IF blocks.

second, I noticed and fixed that AND/OR weren't handled correctly inside IF blocks, where new lines were added incorrectly.

… warning

This resolves the "WARNING: unclosed parentheses or section" error that occurred when parsing SQL statements containing ELSEIF keywords in IF/ELSEIF/ELSE/END IF blocks.
- Add context tracking to detect when inside IF conditions
- Skip newlines for AND/OR tokens within IF conditions while preserving
  newline behavior for AND/OR in WHERE clauses and other contexts
- This improves readability of formatted SQL with complex IF statements
@derrabus derrabus added the bug Something isn't working label Jul 15, 2025
}
} elseif ($token->isOfType(Token::TOKEN_TYPE_RESERVED_NEWLINE)) {
// Newline reserved words start a new line
// Newline reserved words start a new line, except for AND/OR within IF conditions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are more contexts where boolean expressions can be used the same way, at least CASE WHEN. Not sure if this optimization is welcomed in this PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

The behavior is now:

  • IF/ELSEIF/ELSIF conditions: AND/OR stay on the same line
  • CASE WHEN conditions: AND/OR stay on the same line
  • WHERE/HAVING clauses: AND/OR still get newlines (unchanged - this is probably desired for readability)
  • JOIN ON conditions: AND/OR still get newlines (unchanged)

- Apply same-line AND/OR formatting to CASE WHEN conditions
- Maintain newline behavior for WHERE/HAVING/JOIN clauses
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants