Skip to content

Commit cc4baca

Browse files
committed
Fix formatter spacing for JSON path operators (->, ->>)
- Treat `->` and `->>` as SQL operators in the lexer. - Prevent formatter from inserting spaces around JSON path operators. - Add formatter test for JSON path access. For example: ```sql SELECT details->'$."first_name"' FROM users; ``` Signed-off-by: Sean Wei <me@sean.taipei>
1 parent 30ff7cd commit cc4baca

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/Context.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ final class Context
124124
')' => 16,
125125
'.' => 16,
126126
',' => 16,
127+
'->' => 16,
128+
'->>' => 16,
127129
';' => 16,
128130
];
129131

src/Utils/Formatter.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -518,12 +518,15 @@ public function formatList(TokensList $list): string
518518
} elseif (
519519
$prev->keyword === 'DELIMITER'
520520
|| ! (
521-
($prev->type === TokenType::Operator && ($prev->value === '.' || $prev->value === '('))
522-
// No space after . (
521+
($prev->type === TokenType::Operator
522+
&& ($prev->value === '.' || $prev->value === '('
523+
|| $prev->value === '->' || $prev->value === '->>'))
524+
// No space after punctuation and JSON operators.
523525
|| ($curr->type === TokenType::Operator
524526
&& ($curr->value === '.' || $curr->value === ','
525-
|| $curr->value === '(' || $curr->value === ')'))
526-
// No space before . , ( )
527+
|| $curr->value === '(' || $curr->value === ')'
528+
|| $curr->value === '->' || $curr->value === '->>'))
529+
// No space before punctuation and JSON operators.
527530
|| $curr->type === TokenType::Delimiter && mb_strlen((string) $curr->value, 'UTF-8') < 2
528531
)
529532
) {

tests/Utils/FormatterTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,21 @@ public static function formatQueriesProviders(): array
317317
'<span class="sql-reserved">WHERE</span><br/>' .
318318
'&nbsp;&nbsp;&nbsp;&nbsp;<span class="sql-number">1</span>',
319319
],
320+
'json operator' => [
321+
'query' => 'SELECT details->\'$."first_name"\' FROM users',
322+
'text' => 'SELECT' . "\n" .
323+
' details->\'$."first_name"\'' . "\n" .
324+
'FROM' . "\n" .
325+
' users',
326+
'cli' => "\x1b[35mSELECT\n" .
327+
" \x1b[39mdetails->\x1b[91m'$.\"first_name\"'\n" .
328+
"\x1b[35mFROM\n" .
329+
" \x1b[39musers\x1b[0m",
330+
'html' => '<span class="sql-reserved">SELECT</span><br/>' .
331+
'&nbsp;&nbsp;&nbsp;&nbsp;details-&gt;<span class="sql-string">\'$."first_name"\'</span><br/>' .
332+
'<span class="sql-reserved">FROM</span><br/>' .
333+
'&nbsp;&nbsp;&nbsp;&nbsp;users',
334+
],
320335
'typical' => [
321336
'query' => 'SELECT id, if(id=1,"Si","No") from `tbl` where id = 0 or ' .
322337
'id = 1 group by id order by id desc limit 1 offset 0',

0 commit comments

Comments
 (0)