Skip to content

Commit

Permalink
Help prevent arrays with complex elements from being trashed #93
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnathonKoster committed Feb 15, 2024
1 parent 3f95bdc commit d0e4391
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 30 deletions.
4 changes: 4 additions & 0 deletions formatter/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.1.2

- Improves directive array formatting to prevent trashing complex input (#93)

## 2.1.1

- Corrects an issue preventing `@hasSection` from formatting correctly (#92)
Expand Down
2 changes: 1 addition & 1 deletion formatter/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prettier-plugin-blade",
"version": "2.1.1",
"version": "2.1.2",
"description": "A Prettier plugin to format Laravel Blade templates",
"repository": {
"type": "git",
Expand Down
56 changes: 28 additions & 28 deletions formatter/plugin.js

Large diffs are not rendered by default.

20 changes: 19 additions & 1 deletion src/document/printers/directivePrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ export class DirectivePrinter {
'if', 'elseif', 'unless', 'while', 'for', 'foreach', 'forelse'
];

private static canUseSimpleParser(value: string): boolean {
// Quick work around for now.
// Likely remove the prettier workaround logic in the next major version to save headache.
if (
value.includes('//') || value.includes('/**') ||
value.includes('?') || value.includes(':') ||
(value.includes('(') && value.includes(')')) ||
(value.includes('{') && value.includes('}'))
) {
return false;
}

return true;
}

private static getReasonableDirectivePhpOptions(directive: DirectiveNode, phpOptions: ParserOptions): ParserOptions {
// We will override the traillingCommaPHP setting within condition-like
// directives; if we don't we end up with rather ugly @if(...) blocks
Expand Down Expand Up @@ -96,7 +111,10 @@ export class DirectivePrinter {
trimShift = true;

const phpOptions = getPhpOptions();
if (params.startsWith('[') && params.endsWith(']') && directive.startPosition?.line != directive.endPosition?.line) {
if (
params.startsWith('[') && params.endsWith(']') && directive.startPosition?.line != directive.endPosition?.line &&
DirectivePrinter.canUseSimpleParser(params)
) {
let phpInput = '<?php ' + params;
const lineWrapWorkaround = preparePrettierWorkaround(phpInput);

Expand Down
52 changes: 52 additions & 0 deletions src/test/formatter_directives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,4 +586,56 @@ asdf
assert.strictEqual(out, expected);
assert.strictEqual(out2, expected);
});

test('it does not trash comments inside directive args', async () => {
const template = `<img
@class([
// This is valid a comment.
$coverUrl ? "object-center" :"object-left-top",
])
/>`;
const expected = `<img
@class([
// This is valid a comment.
$coverUrl ? "object-center" : "object-left-top",
])
/>
`;
let result = await formatBladeString(template);

assert.strictEqual(result, expected);

for (let i = 0; i < 5; i++) {
result = await formatBladeString(result);

assert.strictEqual(result, expected);
}
});

test('it does not remove whitespace complicated directive args', async () => {
const template = `<img
@class([
$coverUrl ? "object-center" :"object-left-top",
])
/>`;
// Unfortunately this will be the result.
const expected = `<img @class([$coverUrl ? "object-center" : "object-left-top"]) />
`;
const result = await formatBladeString(template);
assert.strictEqual(result, expected);
});

test('the manual prettier array workaround works', async () => {
const template = `<img
@class([
$coverUrl ? "object-center" :"object-left-top", //
])
/>`;
const expected = `<img @class([
$coverUrl ? "object-center" : "object-left-top", //
]) />
`;
const result = await formatBladeString(template);
assert.strictEqual(result, expected);
});
});

0 comments on commit d0e4391

Please sign in to comment.