Skip to content

Commit

Permalink
fix: 🐛 long props collapse into single line (#876)
Browse files Browse the repository at this point in the history
  • Loading branch information
shufo authored Oct 15, 2023
1 parent 2624fd1 commit 3936ece
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 11 deletions.
52 changes: 52 additions & 0 deletions __tests__/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5286,4 +5286,56 @@ describe('formatter', () => {

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

test('long props', async () => {
const content = [
`@props(['name', 'title' => 'Please Confirm', 'message' => 'Are you sure?', 'level' => 'info', 'icon' => 'heroicon-o-question-mark-circle', 'cancelButtonText' => 'No', 'cancelButtonType' => 'muted', 'affirmButtonText' => 'Yes', 'affirmButtonType' => 'success', 'affirmButtonDisabled' => false])`,
].join('\n');

const expected = [
`@props([`,
` 'name',`,
` 'title' => 'Please Confirm',`,
` 'message' => 'Are you sure?',`,
` 'level' => 'info',`,
` 'icon' => 'heroicon-o-question-mark-circle',`,
` 'cancelButtonText' => 'No',`,
` 'cancelButtonType' => 'muted',`,
` 'affirmButtonText' => 'Yes',`,
` 'affirmButtonType' => 'success',`,
` 'affirmButtonDisabled' => false,`,
`])`,
``,
].join('\n');

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

test('nested long props', async () => {
const content = [
`<div>`,
`@props(['name', 'title' => 'Please Confirm', 'message' => 'Are you sure?', 'level' => 'info', 'icon' => 'heroicon-o-question-mark-circle', 'cancelButtonText' => 'No', 'cancelButtonType' => 'muted', 'affirmButtonText' => 'Yes', 'affirmButtonType' => 'success', 'affirmButtonDisabled' => false])`,
`</div>`,
].join('\n');

const expected = [
`<div>`,
` @props([`,
` 'name',`,
` 'title' => 'Please Confirm',`,
` 'message' => 'Are you sure?',`,
` 'level' => 'info',`,
` 'icon' => 'heroicon-o-question-mark-circle',`,
` 'cancelButtonText' => 'No',`,
` 'cancelButtonType' => 'muted',`,
` 'affirmButtonText' => 'Yes',`,
` 'affirmButtonType' => 'success',`,
` 'affirmButtonDisabled' => false,`,
` ])`,
`</div>`,
``,
].join('\n');

await util.doubleFormatCheck(content, expected);
});
});
25 changes: 14 additions & 11 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export default class Formatter {
.then((target) => this.preserveBladeBrace(target))
.then((target) => this.preserveRawBladeBrace(target))
.then((target) => this.preserveConditions(target))
.then((target) => this.preservePropsBlock(target))
.then((target) => this.preserveInlineDirective(target))
.then((target) => this.preserveInlinePhpDirective(target))
.then((target) => this.preserveBladeDirectivesInScripts(target))
Expand Down Expand Up @@ -1314,17 +1315,19 @@ export default class Formatter {

async restoreRawPropsBlock(content: any) {
const regex = this.getRawPropsPlaceholder('(\\d+)');
return _.replace(
content,
new RegExp(regex, 'gms'),
(_match: any, p1: any) =>
`@props(${util
.formatRawStringAsPhp(this.rawPropsBlocks[p1], {
...this.options,
printWidth: util.printWidthForInline,
})
.trimRight()})`,
);
return _.replace(content, new RegExp(regex, 'gms'), (_match: any, p1: any) => {
const placeholder = this.getRawPropsPlaceholder(p1.toString());
const matchedLine = content.match(new RegExp(`^(.*?)${placeholder}`, 'gmi')) ?? [''];
const indent = detectIndent(matchedLine[0]);

const formatted = `@props(${util
.formatRawStringAsPhp(this.rawPropsBlocks[p1], {
...this.options,
})
.trim()})`;

return this.indentRawPhpBlock(indent, formatted);
});
}

isInline(content: any) {
Expand Down

0 comments on commit 3936ece

Please sign in to comment.