Skip to content

Commit

Permalink
fix: 🐛 nested condition inserts unexpected comma
Browse files Browse the repository at this point in the history
  • Loading branch information
shufo committed Jul 12, 2022
1 parent b402a47 commit 92d5023
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
47 changes: 47 additions & 0 deletions __tests__/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4046,4 +4046,51 @@ describe('formatter', () => {

await util.doubleFormatCheck(content, expected);
});

test('nested condition', async () => {
const content = [
`@if (count( auth(" ( ) ")->user() ->currentXY->shopsXY()) > 1)`,
` <span class="ml-24">Test</span>`,
`@else`,
` <span class="ml-16">Test</span>`,
`@endif`,
`@if (`,
``,
``,
`foo(count( auth(" ( ) ")->user() ->currentXY->shopsXY()) > 1`,
``,
``,
`))`,
` <span class="ml-24">Test</span>`,
`@else`,
` <span class="ml-16">Test</span>`,
`@endif`,
`@if (count(auth()->user()->currentXY->shopsXY()) > 1)`,
` <span class="ml-24">Test</span>`,
`@else`,
` <span class="ml-16">Test</span>`,
`@endif`,
].join('\n');

const expected = [
`@if (count(auth(' ( ) ')->user()->currentXY->shopsXY()) > 1)`,
` <span class="ml-24">Test</span>`,
`@else`,
` <span class="ml-16">Test</span>`,
`@endif`,
`@if (foo(count(auth(' ( ) ')->user()->currentXY->shopsXY()) > 1))`,
` <span class="ml-24">Test</span>`,
`@else`,
` <span class="ml-16">Test</span>`,
`@endif`,
`@if (count(auth()->user()->currentXY->shopsXY()) > 1)`,
` <span class="ml-24">Test</span>`,
`@else`,
` <span class="ml-16">Test</span>`,
`@endif`,
``,
].join('\n');

await util.doubleFormatCheck(content, expected);
});
});
4 changes: 3 additions & 1 deletion src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1484,8 +1484,10 @@ export default class Formatter {
return this.indentComponentAttribute(
indent.indent,
util
.formatRawStringAsPhp(matched)
.formatRawStringAsPhp(matched, this.wrapLineLength, true)
.replace(/([\n\s]*)->([\n\s]*)/gs, '->')
.replace(/(?<!(['"]).*)(?<=\()[\n\s]+?(?=\w)/gms, '')
.replace(/,[\n\s]*\)/gs, ')')
.trimEnd(),
);
}),
Expand Down

0 comments on commit 92d5023

Please sign in to comment.