shufo / blade-formatter

An opinionated blade template formatter for Laravel that respects readability
https://www.npmjs.com/package/blade-formatter
MIT License
457 stars 26 forks source link

[Formatting Bug]: issues formatting indented @php blocks following #909 and v1.41.0 #915

Closed claytonrcarter closed 8 months ago

claytonrcarter commented 8 months ago

Version

1.41.0

Template before formatting

@php
    if (1) {
        if (1) {
            if (1) {
                if (1) {
                    if (1) {
                        if (1) {
                            $percent =
                                $item['historical'] ?? null
                                    ? round(
                                        (100 *
                                            ($item['today'] -
                                                $item['historical'])) /
                                            $item['historical'],
                                    )
                                    : null;

                            $color = $percent < 0 ? '#8b0000' : '#006400';
                        }
                    }
                }
            }
        }
    }
@endphp

<div>
    <div>
        <div>
            <div>
                <div>
                    <div>
                        @php
                            $percent =
                                $item['historical'] ?? null
                                    ? round(
                                        (100 *
                                            ($item['today'] -
                                                $item['historical'])) /
                                            $item['historical'],
                                    )
                                    : null;

                            $color = $percent < 0 ? '#8b0000' : '#006400';
                        @endphp
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Template after formatting

@php
    if (1) {
        if (1) {
            if (1) {
                if (1) {
                    if (1) {
                        if (1) {
                            $percent =
                                $item['historical'] ?? null
                                    ? round(
                                        (100 *
                                            ($item['today'] -
                                                $item['historical'])) /
                                            $item['historical'],
                                    )
                                    : null;

                            $color = $percent < 0 ? '#8b0000' : '#006400';
                        }
                    }
                }
            }
        }
    }
@endphp

<div>
    <div>
        <div>
            <div>
                <div>
                    <div>
                        @php
                            $percent =
                                $item[
                                    'historical'
                                ] ??
                                null
                                    ? round(
                                        (100 *
                                            ($item[
                                                'today'
                                            ] -
                                                $item[
                                                    'historical'
                                                ])) /
                                            $item[
                                                'historical'
                                            ],
                                    )
                                    : null;

                            $color =
                                $percent <
                                0
                                    ? '#8b0000'
                                    : '#006400';
                        @endphp
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Expected Behaviour

This code was reported by @calebdw at https://github.com/shufo/blade-formatter/pull/909#issuecomment-1961581690, and I've reproduced it here.

The PHP code in these blocks is indented to the same level and I would expect it to be formatted identically. In particular, the second statement (the ternary) fits into 79 chars, including indentation, and it should be left on a single line.

I can reproduce 1-token-per-line wrapping that @calebdw is seeing, but only after smashing that PHP into so many ifs that every line exceeds the wrap limit (see below). This strongly suggests that we're telling prettier to format these to the wrong printWidth, because it's treating everyline line it's already exceeded the print width.

I can look into this, but it will be a few days.

Reproduction snippet with extreme wrapping

@php
    if (1) {
        if (1) {
            if (1) {
                if (1) {
                    if (1) {
                        if (1) {
                            if (1) {
                                if (1) {
                                    if (1) {
                                        if (1) {
                                            if (1) {
                                                if (1) {
                                                    if (1) {
                                                        if (1) {
                                                            if (1) {
                                                                if (1) {
                                                                    if (1) {
                                                                        $percent =
                                                                            $item[
                                                                                'historical'
                                                                            ] ??
                                                                            null
                                                                                ? round(
                                                                                    (100 *
                                                                                        ($item[
                                                                                            'today'
                                                                                        ] -
                                                                                            $item[
                                                                                                'historical'
                                                                                            ])) /
                                                                                        $item[
                                                                                            'historical'
                                                                                        ],
                                                                                )
                                                                                : null;

                                                                        $color =
                                                                            $percent <
                                                                            0
                                                                                ? '#8b0000'
                                                                                : '#006400';
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
@endphp

Relevant log output

No response