Fix whitespace normalization to support multiline ERB blocks #22
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes issue #15 (preserving whitespace in parameter values) while also supporting multiline ERB templates, which was broken by PR #17.
Problem
Original Issue (#15): The old implementation applied
gsub(/(\n|\s)+/, ' ')AFTER ERB processing, which collapsed whitespace inside parameter values:WHERE email = ' e@mail.dev 'WHERE email = 'e@mail.dev'❌ (spaces removed)PR #17 Attempted Fix: Applied
gsubBEFORE ERB processing, which preserved parameter values BUT broke multiline ERB blocks:Became:
<% field1 = @field1 || 'default1' field2 = @field2 || 'default2' %>(Ruby syntax error) ❌Solution
This PR takes a different approach:
WhitespaceNormalizerhandles whitespace normalizationWhy State Machine vs Regex?
We considered the regex approach from branch
fix/multiline-erb-template-handling:We chose the state machine approach because:
✅ Maintainability
putsstatements to see state changes✅ Predictability
✅ Extensibility
in_commentstate✅ Correctness
''and"")Both approaches are O(n) for typical queries:
For typical SQL queries (< 1000 chars), performance difference is negligible (microseconds). Maintainability is more valuable than micro-optimization.
Implementation Details
Architecture
Before:
After:
State Machine Algorithm
The
WhitespaceNormalizertracks:in_single_quote: Inside'...'in_double_quote: Inside"..."prev_was_space: Just added a space (for collapsing)Key logic:
'inside a single-quoted string followed by another', it's an escape ('it''s') - consume both without toggling state"")RuboCop Compliance
The code follows Ruby best practices:
Minor complexity metrics are exceeded only where the state machine logic requires it, with explicit
rubocop:disablecomments explaining why.Testing
All tests pass:
Test Cases
' value 'preserved'it''s'handled correctlyMigration Path
This is a drop-in replacement with no breaking changes:
Alternatives Considered
Regex approach (branch
fix/multiline-erb-template-handling)Hybrid pre/post processing
Current approach (state machine)
Recommendation
Approve and merge - This solution:
The slight increase in code size is justified by the significant improvement in readability and maintainability.
Checklist