-
Notifications
You must be signed in to change notification settings - Fork 28
fix: ELSEIF handling #155
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
base: 1.5.x
Are you sure you want to change the base?
fix: ELSEIF handling #155
Conversation
… 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
src/SqlFormatter.php
Outdated
} | ||
} 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
4111b1c
to
a9ec3ec
Compare
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.