Skip to content

Commit

Permalink
fix: 🐛 json directive is not indented when it is multi-line
Browse files Browse the repository at this point in the history
  • Loading branch information
shufo committed Sep 24, 2022
1 parent 30840c3 commit 3933eb4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
27 changes: 27 additions & 0 deletions __tests__/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4592,4 +4592,31 @@ describe('formatter', () => {

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

test('inline @json directive', async () => {
const content = [
`@section('footer')`,
` <script>`,
` Object.assign(lang, @json([`,
` 'name' => __('name'),`,
` 'current' => __('current'),`,
` ]));`,
` </script>`,
`@endsection`,
].join('\n');

const expected = [
`@section('footer')`,
` <script>`,
` Object.assign(lang, @json([`,
` 'name' => __('name'),`,
` 'current' => __('current'),`,
` ]));`,
` </script>`,
`@endsection`,
``,
].join('\n');

await util.doubleFormatCheck(content, expected);
});
});
8 changes: 6 additions & 2 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
indentStartTokensWithoutPrefix,
unbalancedStartTokens,
cssAtRuleTokens,
inlinePhpDirectives,
} from './indent';
import { nestedParenthesisRegex } from './regex';
import { SortHtmlAttributes } from './runtimeConfig';
Expand Down Expand Up @@ -1600,10 +1601,13 @@ export default class Formatter {
.trimRight('\n')}`;
}

if (/(@button|@class|@include|@disabled|@checked)/gi.test(matched)) {
if (new RegExp(inlinePhpDirectives.join('|'), 'gi').test(matched)) {
const formatted = _.replace(
matched,
/(?<=@(button|class|include|disabled|checked).*?\()(.*)(?=\))/gis,
new RegExp(
`(?<=@(${_.map(inlinePhpDirectives, (token) => token.substring(1)).join('|')}).*?\\()(.*)(?=\\))`,
'gis',
),
(match2: any, p3: any, p4: any) => {
let wrapLength = this.wrapLineLength;

Expand Down
2 changes: 2 additions & 0 deletions src/indent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ export const phpKeywordStartTokens = ['@forelse', '@if', '@for', '@foreach', '@w

export const phpKeywordEndTokens = ['@endforelse', '@endif', '@endforeach', '@endfor', '@endwhile', '@break'];

export const inlinePhpDirectives = ['@button', '@class', '@include', '@disabled', '@checked', '@json'];

export const inlineFunctionTokens = [
'@set',
'@json',
Expand Down

0 comments on commit 3933eb4

Please sign in to comment.