diff --git a/__tests__/formatter.test.ts b/__tests__/formatter.test.ts deleted file mode 100644 index 30ec1af3..00000000 --- a/__tests__/formatter.test.ts +++ /dev/null @@ -1,5876 +0,0 @@ -import assert from "node:assert"; -import fs from "node:fs"; -import path from "node:path"; -import { describe, expect, test } from "vitest"; -import { BladeFormatter, Formatter } from "../src/main.js"; -import * as cmd from "./support/cmd"; -import * as util from "./support/util"; - -const formatter = () => { - return new Formatter({ indentSize: 4 }); -}; - -describe("formatter", () => { - test("can format plain text", () => { - const content = "aaa\n"; - const expected = "aaa\n"; - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("outputs end with new line", () => { - const content = "aaa"; - const expected = "aaa\n"; - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("can format simple html tag", () => { - const content = ""; - const expected = ["", "", "", "", "", ""].join( - "\n", - ); - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("basic blade directive indent", () => { - const content = [ - "
", - "
", - "@if($user)", - "{{ $user->name }}", - "@endif", - "
", - "
", - "", - ].join("\n"); - - const expected = [ - "
", - "
", - " @if ($user)", - " {{ $user->name }}", - " @endif", - "
", - "
", - "", - ].join("\n"); - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("nested directive indent", () => { - const content = [ - "
", - "@foreach($users as $user)", - "@if($user)", - "{{ $user->name }}", - "@endif", - "@endforeach", - "
", - "", - ].join("\n"); - - const expected = [ - "
", - " @foreach ($users as $user)", - " @if ($user)", - " {{ $user->name }}", - " @endif", - " @endforeach", - "
", - "", - ].join("\n"); - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("hasSection", () => { - const content = [ - "
", - `@hasSection('navigation')`, - "@if ($user)", - "{{ $user->name }}", - "@endif", - "@endif", - "
", - "", - ].join("\n"); - - const expected = [ - "
", - ` @hasSection('navigation')`, - " @if ($user)", - " {{ $user->name }}", - " @endif", - " @endif", - "
", - "", - ].join("\n"); - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - - const builtInDirectives = [ - "auth", - "component", - "empty", - "can", - "canany", - "cannot", - "forelse", - "guest", - "isset", - "push", - "section", - "slot", - "verbatim", - "prepend", - "error", - ]; - - for (const directive of builtInDirectives) { - test(`builtin directive test: ${directive}`, () => { - const content = [ - "
", - `@${directive}($foo)`, - "@if ($user)", - "{{ $user->name }}", - "@endif", - `@end${directive}`, - "
", - "", - ].join("\n"); - - const expected = [ - "
", - ` @${directive}($foo)`, - " @if ($user)", - " {{ $user->name }}", - " @endif", - ` @end${directive}`, - "
", - "", - ].join("\n"); - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - } - - const phpDirectives = ["if", "while"]; - - for (const directive of phpDirectives) { - test("php builtin directive test", () => { - const content = [ - "
", - `@${directive}($foo)`, - "@if ($user)", - "{{ $user->name }}", - "@endif", - `@end${directive}`, - "
", - "", - ].join("\n"); - - const expected = [ - "
", - ` @${directive} ($foo)`, - " @if ($user)", - " {{ $user->name }}", - " @endif", - ` @end${directive}`, - "
", - "", - ].join("\n"); - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - } - - const tokenWithoutParams = ["auth", "guest", "production", "once"]; - - for (const directive of tokenWithoutParams) { - test("token without param directive test", () => { - const content = [ - "
", - `@${directive}`, - "@if ($user)", - "{{ $user->name }}", - "@endif", - `@end${directive}`, - "
", - "", - ].join("\n"); - - const expected = [ - "
", - ` @${directive}`, - " @if ($user)", - " {{ $user->name }}", - " @endif", - ` @end${directive}`, - "
", - "", - ].join("\n"); - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - } - - test("section directive test", () => { - const content = [ - "
", - `@section('foo')`, - `@section('bar')`, - "@if($user)", - "{{ $user->name }}", - "@endif", - "@endsection", - "
", - "", - ].join("\n"); - - const expected = [ - "
", - ` @section('foo')`, - ` @section('bar')`, - " @if ($user)", - " {{ $user->name }}", - " @endif", - " @endsection", - "
", - "", - ].join("\n"); - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("directive token should case insensitive", () => { - const content = [ - "
", - `@Section('foo')`, - `@section('bar')`, - "@if($user)", - "{{ $user->name }}", - "@foreach($users as $user)", - "{{ $user->id }}", - "@endForeach", - "@endIf", - "@endSection", - "
", - "", - ].join("\n"); - - const expected = [ - "
", - ` @Section('foo')`, - ` @section('bar')`, - " @if ($user)", - " {{ $user->name }}", - " @foreach ($users as $user)", - " {{ $user->id }}", - " @endForeach", - " @endIf", - " @endSection", - "
", - "", - ].join("\n"); - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("multiple section directive test", () => { - const content = [ - "
", - `@section('foo')`, - `@section('bar')`, - `@section('baz')`, - "@if($user)", - "{{ $user->name }}", - "@endif", - "@endsection", - "
", - "", - ].join("\n"); - - const expected = [ - "
", - ` @section('foo')`, - ` @section('bar')`, - ` @section('baz')`, - " @if ($user)", - " {{ $user->name }}", - " @endif", - " @endsection", - "
", - "", - ].join("\n"); - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should not clear inline level directive", () => { - const content = ["
", "@section foo @endsection", "
", ""].join( - "\n", - ); - - const expected = [ - "
", - " @section foo @endsection", - "
", - "", - ].join("\n"); - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should not clear php code inside inline @php directive #3", () => { - const content = [ - "
", - `@php $bg = rand(1, 13); $bgchange = $bg.".jpg"; @endphp`, - "
", - "", - ].join("\n"); - - const expected = [ - "
", - " @php", - " $bg = rand(1, 13);", - ` $bgchange = $bg . '.jpg';`, - " @endphp", - "
", - "", - ].join("\n"); - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - - // https://github.com/shufo/blade-formatter/issues/3#issuecomment-552139588 - // eslint-disable-next-line max-len - test("should indent correctly even if inline directive and div tags are mixed", () => { - const content = [ - `@section('content')`, - `
`, - `
@php echo 'FOO'; @endphp
`, - "
", - `
@php echo 'FOO'; @endphp
`, - "@endsection", - "", - ].join("\n"); - - const expected = [ - `@section('content')`, - `
`, - `
@php echo 'FOO'; @endphp
`, - "
", - `
@php echo 'FOO'; @endphp
`, - "@endsection", - "", - ].join("\n"); - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - - // refs: https://github.com/shufo/blade-formatter/issues/48 - test("it should not pad original blankline", async () => { - const content = [ - `@section('content')`, - `
`, - `
@php echo 'FOO'; @endphp
`, - "", // blankline - "
", - "@endsection", - "", - ].join("\n"); - - const expected = [ - `@section('content')`, - `
`, - `
@php echo 'FOO'; @endphp
`, - "", // should be keep original line - "
", - "@endsection", - "", - ].join("\n"); - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("preserve multiline php tag #57", async () => { - const content = [ - "", - `@extends('layouts.mainLayout')`, - "", - `@section('someBlock')`, - "", - "@endsection", - "", - ].join("\n"); - - const expected = [ - "", - `@extends('layouts.mainLayout')`, - "", - `@section('someBlock')`, - "@endsection", - "", - ].join("\n"); - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("preserve inline php tag #57", async () => { - const content = [ - ` { - assert.equal(result, expected); - }); - }); - - test("preserve inline php tag in script", async () => { - const content = [ - "", - "", - ].join("\n"); - - const expected = [ - "", - "", - ].join("\n"); - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("it should be ignore script tag #4", async () => { - const content = [ - `", - "", - ].join("\n"); - - const expected = [ - `", - "", - ].join("\n"); - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should be ignore short tag #56", async () => { - const content = [ - "", - "", - "
", - "", - ].join("\n"); - - const expected = [ - "", - " ", - "
", - "", - ].join("\n"); - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("format API", async () => { - const content = [ - "", - "", - "
", - "", - ].join("\n"); - - const expected = [ - "", - " ", - "
", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("format API with option", async () => { - const content = [ - "", - "", - "
", - "", - ].join("\n"); - - const expected = [ - "", - " ", - "
", - "", - ].join("\n"); - - return new BladeFormatter({ indentSize: 2 }) - .format(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should remove semicolon in end of line", async () => { - const content = [ - "{!! strip_tags($header) !!}", - "", - "{!! strip_tags($slot) !!}", - "@isset($subcopy)", - "{!! strip_tags($subcopy) !!}", - "@endisset", - "", - "{!! strip_tags($footer); !!}", - "", - ].join("\n"); - - const expected = [ - "{!! strip_tags($header) !!}", - "", - "{!! strip_tags($slot) !!}", - "@isset($subcopy)", - " {!! strip_tags($subcopy) !!}", - "@endisset", - "", - "{!! strip_tags($footer) !!}", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("@for directive should work", async () => { - const content = [ - "@for ($i=0;$i<=5;$i++)", - `
`, - "
", - "@endfor", - "", - ].join("\n"); - - const expected = [ - "@for ($i = 0; $i <= 5; $i++)", - `
`, - "
", - "@endfor", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("@foreach directive should work", async () => { - const content = [ - "@foreach($users as $user)", - `
`, - "
", - "@endforeach", - "", - ].join("\n"); - - const expected = [ - "@foreach ($users as $user)", - `
`, - "
", - "@endforeach", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("@foreach directive should work with variable key", async () => { - const content = [ - `@foreach($users["foo"] as $user)`, - `
`, - "
", - "@endforeach", - "", - ].join("\n"); - - const expected = [ - `@foreach ($users['foo'] as $user)`, - `
`, - "
", - "@endforeach", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("@foreach directive should work with children methods", async () => { - const content = [ - "@foreach($user->blogs() as $blog)", - `
`, - "
", - "@endforeach", - "", - ].join("\n"); - - const expected = [ - "@foreach ($user->blogs() as $blog)", - `
`, - "
", - "@endforeach", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("@switch directive should work", async () => { - const content = [ - "@switch($i)", - "@case(1)", - " First case...", - " @break", - "", - "@case(2)", - " Second case...", - " @break", - "", - "@case(3)", - "@case(4)", - " Third case...", - " @break", - "", - "@default", - " Default case...", - "@endswitch", - "", - ].join("\n"); - - const expected = [ - "@switch($i)", - " @case(1)", - " First case...", - " @break", - "", - " @case(2)", - " Second case...", - " @break", - "", - " @case(3)", - " @case(4)", - " Third case...", - " @break", - "", - " @default", - " Default case...", - "@endswitch", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("method call should not indented in blade brackets #2", async () => { - const content = ["{{ auth()->user()->getSeeding() }}", ""].join("\n"); - - const expected = [ - "{{ auth()->user()->getSeeding() }}", - "", - /* */ - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("indented call should be inline in blade brackets #2", async () => { - const content = [ - "{{ auth()", - " ->user()", - " ->getSeeding() }}", - "", - ].join("\n"); - - const expected = [ - "{{ auth()->user()->getSeeding() }}", - "", - /* */ - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("method call in directive should not be multiline #2", async () => { - const content = [ - `@if(auth()->user()->name === 'foo')`, - "

bar

", - "@endif", - "", - ].join("\n"); - - const expected = [ - `@if (auth()->user()->name === 'foo')`, - "

bar

", - "@endif", - "", - /* */ - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should remain tags even if php tag exists vscode-blade-formattere#2", async () => { - const content = [ - "", - `", - "", - ].join("\n"); - - const expected = [ - "", - `", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should not format nbsp", async () => { - const content = [ - `{{ trans('email.website_title') }} `, - `{{ trans('email.website_url') }}`, - "", - ].join("\n"); - - const expected = [ - `{{ trans('email.website_title') }} `, - `{{ trans('email.website_url') }}`, - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should not occurs error with if directive", async () => { - const content = ["@if($user)", " foo", "@endif", ""].join("\n"); - - const expected = ["@if ($user)", " foo", "@endif", ""].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should not occurs error on directive inside html tag ", async () => { - const content = [ - `", - "", - ].join("\n"); - - const expected = [ - ``, - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should not occurs error even if 3 level nested in directive", async () => { - const content = [ - `@if(config('app.foo', env('APP_FOO_BAR')))`, - " foo", - "@endif>", - "", - ].join("\n"); - - const expected = [ - `@if (config('app.foo', env('APP_FOO_BAR')))`, - " foo", - "@endif>", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("forelse directive should work", async () => { - const content = [ - "@forelse($students as $student)", - "
foo
", - "@empty", - "empty", - "@endforelse", - "", - ].join("\n"); - - const expected = [ - "@forelse($students as $student)", - "
foo
", - "@empty", - " empty", - "@endforelse", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should preserve spaces between directive and parentheses", async () => { - const content = [`@if($user === 'foo')`, "foo", "@endif", ""].join("\n"); - - const expected = [`@if ($user === 'foo')`, " foo", "@endif", ""].join( - "\n", - ); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should preserve spaces between directive and parentheses (space exists)", async () => { - const content = ["@foreach ($users as $user)", "foo", "@endif", ""].join( - "\n", - ); - - const expected = [ - "@foreach ($users as $user)", - " foo", - "@endif", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should keep format even if @include directive exists", async () => { - const content = [ - `{{ Form::open(['route' => 'withdraw.withdraw', 'method' => 'post', 'id'=>'form01']) }}`, - "{{ Form::close() }}", - "", - `@include('common.footer_js')`, - "", - ].join("\n"); - - const expected = [ - `{{ Form::open(['route' => 'withdraw.withdraw', 'method' => 'post', 'id' => 'form01']) }}`, - "{{ Form::close() }}", - "", - `@include('common.footer_js')`, - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should keep format with while and section", async () => { - const content = [ - `@extends('layouts.app')`, - "", - `@section('content')`, - `@include('partials.page-header')`, - "", - "@if(!have_posts())", - ``, - `{!! __('Sorry, no results were found.', 'sage') !!}`, - "", - "", - "{!! get_search_form(false) !!}", - "@endif", - "", - "@while(have_posts()) @php(the_post())", - `@includeFirst(['partials.content-' . get_post_type(), 'partials.content'])`, - "@endwhile", - "", - "{!! get_the_posts_navigation() !!}", - "@endsection", - "", - `@section('sidebar')`, - `@include('partials.sidebar')`, - "@endsection", - "", - ].join("\n"); - - const expected = [ - `@extends('layouts.app')`, - "", - `@section('content')`, - ` @include('partials.page-header')`, - "", - " @if (!have_posts())", - ` `, - ` {!! __('Sorry, no results were found.', 'sage') !!}`, - " ", - "", - " {!! get_search_form(false) !!}", - " @endif", - "", - " @while (have_posts())", - " @php(the_post())", - ` @includeFirst(['partials.content-' . get_post_type(), 'partials.content'])`, - " @endwhile", - "", - " {!! get_the_posts_navigation() !!}", - "@endsection", - "", - `@section('sidebar')`, - ` @include('partials.sidebar')`, - "@endsection", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("mixed html tag and directive #5", async () => { - const content = [ - `@extends('dashboard')`, - "", - `@section('content')`, - "@if( $member->isAdmin() )", - `
`, - "@endif", - "Test!", - "@if( $member->isAdmin() )", - "
", - "@endif", - "@endsection", - "", - ].join("\n"); - - const expected = [ - `@extends('dashboard')`, - "", - `@section('content')`, - " @if ($member->isAdmin())", - `
`, - " @endif", - " Test!", - " @if ($member->isAdmin())", - "
", - " @endif", - "@endsection", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("escaped brace without line return issue shufo/vscode-blade-formatter#12", async () => { - const content = [ - `{!! Form::open(['method' => 'DELETE', 'route' => ['roles.destroy', $role->id], 'style' => 'display:inline']) !!}`, - `{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}`, - "{!! Form::close() !!}", - ].join("\n"); - - const expected = [ - `{!! Form::open(['method' => 'DELETE', 'route' => ['roles.destroy', $role->id], 'style' => 'display:inline']) !!}`, - `{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}`, - "{!! Form::close() !!}", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("braces without content should not occurs error", async () => { - const content = [ - ``, - `
`, - " {{ }}", - "
", - "
", - ].join("\n"); - - const expected = [ - ``, - `
`, - " {{ }}", - "
", - "
", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("directive in html attribute should not occurs error", async () => { - const content = [ - "@if (count($topics))", - ` ", - "@endif", - ].join("\n"); - - const expected = [ - "@if (count($topics))", - ` ", - "@endif", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should consider directive in html tag", async () => { - const cmdResult = await cmd.execute( - path.resolve("bin", "blade-formatter.js"), - [path.resolve("__tests__", "fixtures", "inline_php_tag.blade.php")], - ); - - const formatted = fs.readFileSync( - path.resolve( - "__tests__", - "fixtures", - "formatted_inline_php_tag.blade.php", - ), - ); - - expect(cmdResult).toEqual(formatted.toString("utf-8")); - }); - - test("should not occurs error on inline if to end directive on long line", async () => { - const content = [ - "
", - `@if (count($users) && $users->has('friends')) {{ $user->name }} @endif`, - "
", - ].join("\n"); - - const expected = [ - "
", - ` @if (count($users) && $users->has('friends'))`, - " {{ $user->name }}", - " @endif", - "
", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should format within @php directive", async () => { - const content = [ - " @php", - " if ($user) {", - ` $user->name = 'foo';`, - " }", - " @endphp", - ].join("\n"); - - const expected = [ - " @php", - " if ($user) {", - ` $user->name = 'foo';`, - " }", - " @endphp", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - const predefinnedConstants = [ - "PHP_VERSION", - "PHP_RELEASE_VERSION", - "PHP_VERSION_ID", - "PHP_OS_FAMILY", - "PHP_FLOAT_DIG", - ]; - - for (const constant of predefinnedConstants) { - test("should format php predefined constants", async () => { - const content = [`{{ ${constant} }}`].join("\n"); - const expected = [`{{ ${constant} }}`, ""].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - } - - test("should format null safe operator", async () => { - const content = ["{{ $entity->executors->first()?->name() }}"].join("\n"); - - const expected = ["{{ $entity->executors->first()?->name() }}", ""].join( - "\n", - ); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should format named arguments", async () => { - const content = ["{{ foo(double_encode: true) }}"].join("\n"); - - const expected = ["{{ foo(double_encode: true) }}", ""].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should format blade directive in scripts", async () => { - const content = [ - " ", - ].join("\n"); - - const expected = [ - " ", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should format multiple blade directive in script tag", async () => { - const content = [ - "", - ].join("\n"); - - const expected = [ - "", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should format inline directive in scripts #231", async () => { - const content = [ - ``, - ].join("\n"); - - const expected = [ - "", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - const elseEnabledDirectives = ["can", "canany", "cannot"]; - - for (const directive of elseEnabledDirectives) { - test(`else directives test - ${directive}`, async () => { - const content = [ - "
", - `@${directive}(["update",'read'],$user)`, - "@if ($user)", - "{{ $user->name }}", - "@endif", - `@else${directive}(['delete'], $user)`, - "foo", - "@else", - "bar", - `@end${directive}`, - "
", - "", - ].join("\n"); - - const expected = [ - "
", - ` @${directive}(['update', 'read'], $user)`, - " @if ($user)", - " {{ $user->name }}", - " @endif", - ` @else${directive}(['delete'], $user)`, - " foo", - " @else", - " bar", - ` @end${directive}`, - "
", - "", - ].join("\n"); - - return formatter() - .formatContent(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - } - - test("should break chained method in directive", async () => { - const content = [ - "@if (auth()", - "->user()", - "->subscribed('default'))", - "aaa", - "@endif", - ].join("\n"); - - const expected = [ - "@if (auth()->user()->subscribed('default'))", - " aaa", - "@endif", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should break chained method in directive 2", async () => { - const content = [ - "@foreach (request()->users() as $user)", - "aaa", - "@endif", - ].join("\n"); - - const expected = [ - "@foreach (request()->users() as $user)", - " aaa", - "@endif", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("should format inline function directives in scripts", async () => { - const content = [ - `", - ].join("\n"); - - const expected = [ - `", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("forelse inside if directive should work #254", async () => { - const content = [ - "@if (true)", - "", - "@forelse($elems as $elem)", - "", - "@empty", - "", - "@endforelse", - "
", - "@endif", - ].join("\n"); - - const expected = [ - "@if (true)", - " ", - " @forelse($elems as $elem)", - " ", - " @empty", - " ", - " @endforelse", - "
", - "@endif", - "", - ].join("\n"); - - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("directives with optional endtags", async () => { - const content = [ - `@extends('layouts.test')`, - `@section('title', 'This is title')`, - `@section('content')`, - `
`, - " This is content.", - "
", - "@endsection", - "", - "@if (true)", - ` @push('some-stack', $some->getContent())`, - " @section($aSection, $some->content)", - ` @push('some-stack')`, - " more", - " @endpush", - ` @prepend($stack->name, 'here we go')`, - "@endif", - "", - ].join("\n"); - return new BladeFormatter().format(content).then((result: any) => { - assert.equal(result, content); - }); - }); - - test("force expand multilines", async () => { - const content = [ - '
', - "@if (Auth::check())", - "@php($user = Auth::user())", - "{{ $user->name }}", - "@endif", - "
", - ].join("\n"); - - const expected = [ - "", - " @if (Auth::check())", - " @php($user = Auth::user())", - " {{ $user->name }}", - " @endif", - "", - "", - ].join("\n"); - - return new BladeFormatter({ wrapAttributes: "force-expand-multiline" }) - .format(content) - .then((result: any) => { - assert.equal(result, expected); - }); - }); - - test("component attribute name #346", async () => { - let content = [``].join("\n"); - let expected = [``, ""].join("\n"); - - util.doubleFormatCheck(content, expected); - - content = [""].join("\n"); - expected = [``, ""].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("empty class atrbitue", async () => { - let content = [`
`].join("\n"); - let expected = [`
`, ""].join("\n"); - - await util.doubleFormatCheck(content, expected); - - content = [ - ` -`, - ].join("\n"); - expected = [ - ``, - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("support laravel-permission directives", async () => { - const directives = [ - { - start: "@role", - end: "@endrole", - }, - { - start: "@hasrole", - end: "@endhasrole", - }, - { - start: "@hasanyrole", - end: "@endhasanyrole", - }, - { - start: "@hasallroles", - end: "@endhasallroles", - }, - { - start: "@unlessrole", - end: "@endunlessrole", - }, - { - start: "@hasexactroles", - end: "@endhasexactroles", - }, - ]; - - for (const target of directives) { - const content = [ - `
`, - `${target.start}('foo')`, - "
bar
", - `${target.end}`, - "
", - ].join("\n"); - const expected = [ - `
`, - ` ${target.start}('foo')`, - "
bar
", - ` ${target.end}`, - "
", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - } - }); - - test("@class directive", async () => { - let content = [ - `$isActive])>`, - ].join("\n"); - let expected = [ - ` $isActive])>`, - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - - content = [ - `$isActive,`, - " ])>", - ].join("\n"); - - expected = [ - ` $isActive])>`, - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - - content = [ - `$isActive,`, - ` 'text-gray-500' => !$isActive,`, - ` 'bg-red' => $hasError,`, - "])>", - ].join("\n"); - - expected = [ - " $isActive,`, - ` 'text-gray-500' => !$isActive,`, - ` 'bg-red' => $hasError,`, - "])>", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - - content = [ - "
", - `$isActive,`, - ` 'text-gray-500' => !$isActive,`, - ` 'bg-red' => $hasError,`, - "])>", - "
", - ].join("\n"); - - expected = [ - "
", - " $isActive,`, - ` 'text-gray-500' => !$isActive,`, - ` 'bg-red' => $hasError,`, - " ])>", - "
", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("@button directive", async () => { - let content = [`@button(['class'=>'btn btn-primary p-btn-wide',])`].join( - "\n", - ); - let expected = [ - `@button(['class' => 'btn btn-primary p-btn-wide'])`, - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - - content = [ - "@button([", - `'class'=>'btn btn-primary p-btn-wide',`, - "])", - ].join("\n"); - - expected = [ - "@button([", - ` 'class' => 'btn btn-primary p-btn-wide',`, - "])", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - - content = [ - "
", - `@button(['class' => 'btn btn-primary p-btn-wide',])`, - "
", - ].join("\n"); - - expected = [ - "
", - ` @button(['class' => 'btn btn-primary p-btn-wide'])`, - "
", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - - content = [ - "
", - "@button([", - `'class' => 'btn btn-primary p-btn-wide',`, - `'text' => 'Save',`, - "])", - "
", - ].join("\n"); - - expected = [ - "
", - " @button([", - ` 'class' => 'btn btn-primary p-btn-wide',`, - ` 'text' => 'Save',`, - " ])", - "
", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - - content = [ - "
", - "
", - "@button([", - `'class' => 'btn btn-primary p-btn-wide',`, - `'text' => 'Save',`, - "])", - "
", - "
", - ].join("\n"); - - expected = [ - "
", - "
", - " @button([", - ` 'class' => 'btn btn-primary p-btn-wide',`, - ` 'text' => 'Save',`, - " ])", - "
", - "
", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("@forelse-@empty-@endforelse directive in scripts", async () => { - const content = [ - "", - ].join("\n"); - - const expected = [ - "", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("quoted expression should not adds space", async () => { - const content = [ - "@foreach ($items as $item)", - " @switch($item->status)", - ` @case("status")`, - " // Do something", - " @break", - " @endswitch", - "@endforeach", - ].join("\n"); - - const expected = [ - "@foreach ($items as $item)", - " @switch($item->status)", - ` @case('status')`, - " // Do something", - " @break", - " @endswitch", - "@endforeach", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("conditional expression", async () => { - const content = [ - "@if ($condition < 1)", - " {{-- Do something --}}", - "@elseif ($condition <2)", - " {{-- Do something --}}", - "@elseif ($condition< 3)", - " {{-- Do something --}}", - "@else", - " {{-- Do something --}}", - "@endif", - ].join("\n"); - - const expected = [ - "@if ($condition < 1)", - " {{-- Do something --}}", - "@elseif ($condition < 2)", - " {{-- Do something --}}", - "@elseif ($condition < 3)", - " {{-- Do something --}}", - "@else", - " {{-- Do something --}}", - "@endif", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("conditional expression (while)", async () => { - const content = [ - "@while ($condition< 1)", - "{{-- Do something --}}", - "@endwhile", - ].join("\n"); - - const expected = [ - "@while ($condition < 1)", - " {{-- Do something --}}", - "@endwhile", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("ignore formatting between blade-formatter-disable and blade-formatter-enable", async () => { - const content = [ - "@if ($condition < 1)", - " {{ $user }}", - " {{-- blade-formatter-disable --}}", - " {{ $foo}}", - " {{-- blade-formatter-enable --}}", - "@elseif (!condition())", - " {{ $user }}", - "@elseif ($condition < 3)", - " {{ $user }}", - " {{-- blade-formatter-disable --}}", - " {{ $bar}}", - " {{-- blade-formatter-enable --}}", - "@endif", - ].join("\n"); - - const expected = [ - "@if ($condition < 1)", - " {{ $user }}", - " {{-- blade-formatter-disable --}}", - " {{ $foo}}", - " {{-- blade-formatter-enable --}}", - "@elseif (!condition())", - " {{ $user }}", - "@elseif ($condition < 3)", - " {{ $user }}", - " {{-- blade-formatter-disable --}}", - " {{ $bar}}", - " {{-- blade-formatter-enable --}}", - "@endif", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("ignore formatting after blade-formatter-disable-next-line", async () => { - const content = [ - "
", - "@if ($condition < 1)", - " {{-- blade-formatter-disable-next-line --}}", - " {{ $user }}", - "@elseif ($condition < 3)", - " {{ $user }}", - "@endif", - "
", - ].join("\n"); - - const expected = [ - "
", - " @if ($condition < 1)", - " {{-- blade-formatter-disable-next-line --}}", - " {{ $user }}", - " @elseif ($condition < 3)", - " {{ $user }}", - " @endif", - "
", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("ignore formatting entire file if blade-formatter-disable on a first line", async () => { - const content = [ - "{{-- blade-formatter-disable --}}", - "
", - "{{-- blade-formatter-disable --}}", - " {{ $foo}}", - "{{-- blade-formatter-enable --}}", - "@if ($condition < 1)", - " {{-- blade-formatter-disable-next-line --}}", - " {{ $user }}", - "@elseif ($condition < 3)", - " {{ $user }}", - "@endif", - "
", - "", - ].join("\n"); - - const expected = [ - "{{-- blade-formatter-disable --}}", - "
", - "{{-- blade-formatter-disable --}}", - " {{ $foo}}", - "{{-- blade-formatter-enable --}}", - "@if ($condition < 1)", - " {{-- blade-formatter-disable-next-line --}}", - " {{ $user }}", - "@elseif ($condition < 3)", - " {{ $user }}", - "@endif", - "
", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("double curly brace expression for js framework", async () => { - const content = [ - ``, - ` `, - ` @{{ ok? 'YES': 'NO' }}`, - " ", - ` @{{ message.split('').reverse().join('') }}`, - " ", - ` @{{item.roles.map(role=>role.name).join(', ')}}`, - " ", - "", - ].join("\n"); - - const expected = [ - ``, - ` `, - ` @{{ ok ? 'YES' : 'NO' }}`, - " ", - ` @{{ message.split('').reverse().join('') }}`, - " ", - ` @{{ item.roles.map(role => role.name).join(', ') }}`, - " ", - "", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("slow without endslot directive https://github.com/shufo/vscode-blade-formatter/issues/304", async () => { - const content = [ - `@component('components.article.intro')`, - ` @slot('date', $article->formatDate)`, - ` @slot('read_mins', $article->readTime)`, - " @if ($author)", - ` @slot('authors', [['link' => $author_link, 'name' => $author]])`, - " @endif", - ` @slot('intro_text')`, - " {!! $article->introduction !!}", - " @endslot", - " @endcomponent", - ].join("\n"); - - const expected = [ - `@component('components.article.intro')`, - ` @slot('date', $article->formatDate)`, - ` @slot('read_mins', $article->readTime)`, - " @if ($author)", - ` @slot('authors', [['link' => $author_link, 'name' => $author]])`, - " @endif", - ` @slot('intro_text')`, - " {!! $article->introduction !!}", - " @endslot", - "@endcomponent", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("@permission directive", async () => { - const content = [ - `@permission('post.edit')`, - ``, - "@endpermission", - ].join("\n"); - - const expected = [ - `@permission('post.edit')`, - ` `, - "@endpermission", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("formatter throws exception on syntax error", async () => { - const content = [ - `@permission('post.edit')`, - ``, - "@endpermission", - ].join("\n"); - - await expect(new BladeFormatter().format(content)).rejects.toThrow( - "SyntaxError", - ); - }); - - test("inline nested parenthesis #350", async () => { - const content = [ - "@if ($user)", - "
", - ` {{ asset(auth()->user()->getUserMedia('first', 'second')) }}`, - ` {{ asset4(asset1(asset2(asset3(auth()->user($aaaa['bbb'])->aaa("aaa"))))) }}`, - ` {{ asset(auth()->user($aaaa["bbb"])->aaa('aaa')) }}`, - " {{ $user }}", - ` {{ auth()->user( ["bar","ccc"])->foo("aaa") }}`, - ` {{ asset(auth()->user(['bar', 'ccc'])->tooooooooooooooooooooooooooooooooooolongmethod('aaa')->chained()->tooooooooooooooooooooooooooo()->long()) }}`, - "
", - "@endif", - ].join("\n"); - - const expected = [ - "@if ($user)", - "
", - ` {{ asset(auth()->user()->getUserMedia('first', 'second')) }}`, - ` {{ asset4(asset1(asset2(asset3(auth()->user($aaaa['bbb'])->aaa('aaa'))))) }}`, - ` {{ asset(auth()->user($aaaa['bbb'])->aaa('aaa')) }}`, - " {{ $user }}", - ` {{ auth()->user(['bar', 'ccc'])->foo('aaa') }}`, - ` {{ asset(auth()->user(['bar', 'ccc'])->tooooooooooooooooooooooooooooooooooolongmethod('aaa')->chained()->tooooooooooooooooooooooooooo()->long()) }}`, - "
", - "@endif", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("3 more level nested parenthesis #340", async () => { - const content = [ - "
", - " @if (count($foo->bar(Auth::user(), Request::path())) >= 1)", - " foo", - " @endif", - " @if (count($foo->bar(Auth::user($baz->method()), Request::path())) >= 1)", - " foo", - " @endif", - " @foreach (Auth::users($my->users($as->foo)) as $user)", - " foo", - " @endif", - " @isset($user->foo($user->bar($user->baz())))", - " foo", - " @endisset", - "
", - ].join("\n"); - - const expected = [ - "
", - " @if (count($foo->bar(Auth::user(), Request::path())) >= 1)", - " foo", - " @endif", - " @if (count($foo->bar(Auth::user($baz->method()), Request::path())) >= 1)", - " foo", - " @endif", - " @foreach (Auth::users($my->users($as->foo)) as $user)", - " foo", - " @endif", - " @isset($user->foo($user->bar($user->baz())))", - " foo", - " @endisset", - "
", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("it should line break before and after directives", async () => { - const content = [ - "
", - " @if (count($foo->bar(Auth::user(), Request::path())) >= 1) foo", - " @endif", - "
", - " @if (count($foo->bar(Auth::user(), Request::path())) >= 1)", - " foo", - " @endif", - "
", - " @if (count($foo->bar(Auth::user(), Request::path())) >= 1) foo", - " @endif", - "
", - "
", - " @foreach ($collection as $item)", - " {{ $item }} @endforeach", - " @if (count($foo->bar(Auth::user(), Request::path())) >= 1)", - " foo", - "", - " @endif", - " @if (count($foo->bar(Auth::user(), Request::path())) >= 1) foo", - "", - " @endif", - " @if (count($foo->bar(Auth::user(), Request::path())) >= 1)", - " @endif", - "
", - ].join("\n"); - - const expected = [ - "
", - " @if (count($foo->bar(Auth::user(), Request::path())) >= 1)", - " foo", - " @endif", - "
", - " @if (count($foo->bar(Auth::user(), Request::path())) >= 1)", - " foo", - " @endif", - "
", - " @if (count($foo->bar(Auth::user(), Request::path())) >= 1)", - " foo", - " @endif", - "
", - "
", - " @foreach ($collection as $item)", - " {{ $item }}", - " @endforeach", - " @if (count($foo->bar(Auth::user(), Request::path())) >= 1)", - " foo", - " @endif", - " @if (count($foo->bar(Auth::user(), Request::path())) >= 1)", - " foo", - " @endif", - " @if (count($foo->bar(Auth::user(), Request::path())) >= 1)", - " @endif", - "
", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("complex line break", async () => { - const content = [ - "
", - "@if ($user) @if ($condition) aaa @endif", - "@endif", - ` @can('edit') bbb`, - " @endcan", - `@auth('user') ccc`, - "@endauth", - "
", - "
", - `@section('title') aaa @endsection`, - "
", - `
@foreach($users as $user) @foreach($shops as $shop) {{ $user["id"] . $shop["id"] }} @endforeach @endforeach
`, - `
@if($users) @foreach($shops as $shop) {{ $user["id"] . $shop["id"] }} @endforeach @endif
`, - ].join("\n"); - - const expected = [ - "
", - " @if ($user)", - " @if ($condition)", - " aaa", - " @endif", - " @endif", - ` @can('edit')`, - " bbb", - " @endcan", - ` @auth('user')`, - " ccc", - " @endauth", - "
", - "
", - ` @section('title')`, - " aaa", - " @endsection", - "
", - "
", - " @foreach ($users as $user)", - " @foreach ($shops as $shop)", - ` {{ $user['id'] . $shop['id'] }}`, - " @endforeach", - " @endforeach", - "
", - "
", - " @if ($users)", - " @foreach ($shops as $shop)", - ` {{ $user['id'] . $shop['id'] }}`, - " @endforeach", - " @endif", - "
", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("inline @json directive", async () => { - const content = [ - "`, - "foo", - "", - `
`, - "
", - ].join("\n"); - - const expected = [ - ``, - " foo", - "", - `
`, - "
", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("inline @error directive should keep its format", async () => { - const content = [ - ``, - " Choose restaurant", - "", - ].join("\n"); - - const expected = [ - ``, - " Choose restaurant", - "", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("inline @if-@endif directive should keep its format", async () => { - const content = [ - ``, - "", - ``, - "", - // multiline directive inside html tag should be formatted into inline - ``, - // multiple directives in html tag - ``, - ].join("\n"); - - const expected = [ - ``, - "", - ``, - "", - `", - `", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("order aware nested directive", async () => { - const content = [ - `@component('components.elements.button') @slot('href') /plant/details/{{ $plant->system_name }} @endslot @endcomponent`, - `@section('components.elements.button') @error('href') /plant/details/{{ $plant->system_name }} @enderror @endsection`, - `@foreach($users as $user) @error('href') {{ $user }} @enderror @endforeach`, - ].join("\n"); - - const expected = [ - `@component('components.elements.button')`, - ` @slot('href')`, - " /plant/details/{{ $plant->system_name }}", - " @endslot", - "@endcomponent", - `@section('components.elements.button')`, - ` @error('href')`, - " /plant/details/{{ $plant->system_name }}", - " @enderror", - "@endsection", - "@foreach ($users as $user)", - ` @error('href')`, - " {{ $user }}", - " @enderror", - "@endforeach", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("line break around @case, @break and @default", async () => { - const content = [ - "@switch($type) @case(1) $a = 3; @break @case(2) @case(3) $a = 4; @break @default $a = null; @endswitch", - "
", - "@switch($type) @case(1) $a = 3; @break @case(2) @case(3) $a = 4; @break @default $a = null; @endswitch", - "
", - "@switch($type) @case(1) $a = 3; @break @case(2) @case(3) $a = 4; @break @default $a = null; @endswitch", - `@section('aaa')`, - "@switch($type)", - "@case(1)", - "$a = 3;", - "@break", - "", - "@case(2)", - "@case(3)", - "$a = 4;", - "@break", - "", - "@default", - "$a = null;", - "@endswitch", - "@endsection", - "
", - "@switch($i)", - " @case(1)", - " @switch($j)", - " @case(1)", - " First case...", - " @break", - " @case(2)", - " Second case...", - " @break", - " @default", - " Default case...", - " @endswitch", - " @break", - " @case(2)", - " hogehoge...", - " @break", - "@endswitch", - "
", - "@switch($type)", - " @case(1)", - " $a = 3;", - " @break", - "", - " @case(2)", - " @case(3)", - " $a = 4;", - " @break", - "", - "@case(3)", - " $a = 4;", - "@break", - "", - "@default", - " $a = null;", - "@endswitch", - ].join("\n"); - - const expected = [ - "@switch($type)", - " @case(1)", - " $a = 3;", - " @break", - "", - " @case(2)", - " @case(3)", - " $a = 4;", - " @break", - "", - " @default", - " $a = null;", - "@endswitch", - "
", - " @switch($type)", - " @case(1)", - " $a = 3;", - " @break", - "", - " @case(2)", - " @case(3)", - " $a = 4;", - " @break", - "", - " @default", - " $a = null;", - " @endswitch", - "
", - "@switch($type)", - " @case(1)", - " $a = 3;", - " @break", - "", - " @case(2)", - " @case(3)", - " $a = 4;", - " @break", - "", - " @default", - " $a = null;", - "@endswitch", - `@section('aaa')`, - " @switch($type)", - " @case(1)", - " $a = 3;", - " @break", - "", - " @case(2)", - " @case(3)", - " $a = 4;", - " @break", - "", - " @default", - " $a = null;", - " @endswitch", - "@endsection", - "
", - " @switch($i)", - " @case(1)", - " @switch($j)", - " @case(1)", - " First case...", - " @break", - "", - " @case(2)", - " Second case...", - " @break", - "", - " @default", - " Default case...", - " @endswitch", - " @break", - "", - " @case(2)", - " hogehoge...", - " @break", - "", - " @endswitch", - "
", - "@switch($type)", - " @case(1)", - " $a = 3;", - " @break", - "", - " @case(2)", - " @case(3)", - " $a = 4;", - " @break", - "", - " @case(3)", - " $a = 4;", - " @break", - "", - " @default", - " $a = null;", - "@endswitch", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("else token auto line breaking", async () => { - const content = [ - "@if (count($users) === 1)", - " Foo", - "@elseif (count($users) > 1)Bar", - "@elseif (count($users) > 2)Bar2", - "@else Baz@endif", - `@can('update') foo @elsecan('read') bar @endcan`, - ].join("\n"); - - const expected = [ - "@if (count($users) === 1)", - " Foo", - "@elseif (count($users) > 1)", - " Bar", - "@elseif (count($users) > 2)", - " Bar2", - "@else", - " Baz", - "@endif", - `@can('update')`, - " foo", - `@elsecan('read')`, - " bar", - "@endcan", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("line breaking with html tag", async () => { - const content = [ - "
", - `
@can('auth')`, - `foo @elsecan('aaa') bar @endcan
`, - "
@foreach($users as $user)", - "{{$user}} bar @endforeach
", - `

@if($user)`, - "{!!$user!!} @elseif ($authorized) foo @else bar @endif

", - ``, - "

@for ($i = 0; $i < 5; $i++)", - "aaa", - "@endfor

", - "

@if($user)", - "{!!$user!!} @elseif ($authorized) foo @else bar @endif", - "", - "

", - ].join("\n"); - - const expected = [ - "
", - "
", - ` @can('auth')`, - " foo", - ` @elsecan('aaa')`, - " bar", - " @endcan", - "
", - "
", - " @foreach ($users as $user)", - " {{ $user }} bar", - " @endforeach", - "
", - "
", - `

`, - " @if ($user)", - " {!! $user !!}", - " @elseif ($authorized)", - " foo", - " @else", - " bar", - " @endif", - "

", - ``, - "

", - " @for ($i = 0; $i < 5; $i++)", - " aaa", - " @endfor", - "

", - "

", - " @if ($user)", - " {!! $user !!}", - " @elseif ($authorized)", - " foo", - " @else", - " bar", - " @endif", - "

", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("raw php inlined comment #493", async () => { - const content = [ - "", - "", - `@foreach ($preview['new'] as $game)`, - ` `, - "@endforeach", - ].join("\n"); - - const expected = [ - "", - "", - `@foreach ($preview['new'] as $game)`, - ` `, - "@endforeach", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("sort tailwindcss classs option can work", () => { - const content = [ - `
`, - "
", - ].join("\n"); - const expected = [ - `
`, - "
", - "", - ].join("\n"); - - return new Formatter({ sortTailwindcssClasses: true }) - .formatContent(content) - .then((result: string) => { - assert.equal(result, expected); - }); - }); - - test("sort tailwindcss classs with various character", () => { - const content = [ - `
`, - ].join("\n"); - const expected = [ - `
`, - "", - ].join("\n"); - - return new Formatter({ sortTailwindcssClasses: true }) - .formatContent(content) - .then((result: string) => { - assert.equal(result, expected); - }); - }); - - test("long tailwindcss classs", async () => { - const content = [ - `
`, - "
", - ].join("\n"); - - const expected = [ - `
`, - "
", - "", - ].join("\n"); - - const result = await new Formatter({ - sortTailwindcssClasses: true, - }).formatContent(content); - assert.equal(result, expected); - const result2 = await new Formatter({ - sortTailwindcssClasses: true, - }).formatContent(result); - assert.equal(result2, result); - }); - - test("tailwindcss classs with new line", async () => { - const content = [ - `
`, - "
", - ].join("\n"); - - const expected = [ - `
`, - "
", - "", - ].join("\n"); - - const result = await new Formatter({ - sortTailwindcssClasses: true, - }).formatContent(content); - assert.equal(result, expected); - const result2 = await new Formatter({ - sortTailwindcssClasses: true, - }).formatContent(result); - assert.equal(result2, result); - }); - - test("@include directive should format its parameter", async () => { - const content = [ - `@include('parts.partials.buttons.btn-group', ["buttons" => [`, - "[", - `"style" => "link",`, - `"link" => [`, - `"title" => "Call to Action",`, - `"url" => "#",`, - `"target" => "_self",`, - "],", - "],", - "]])", - "
", - `@include('parts.partials.buttons.btn-group', ["buttons" => [`, - "[", - `"style" => "link",`, - `"link" => [`, - `"title" => "Call to Action",`, - `"url" => "#",`, - `"target" => "_self",`, - "],", - "],", - "]])", - "
", - ].join("\n"); - - const expected = [ - `@include('parts.partials.buttons.btn-group', [`, - ` 'buttons' => [`, - " [", - ` 'style' => 'link',`, - ` 'link' => [`, - ` 'title' => 'Call to Action',`, - ` 'url' => '#',`, - ` 'target' => '_self',`, - " ],", - " ],", - " ],", - "])", - "
", - ` @include('parts.partials.buttons.btn-group', [`, - ` 'buttons' => [`, - " [", - ` 'style' => 'link',`, - ` 'link' => [`, - ` 'title' => 'Call to Action',`, - ` 'url' => '#',`, - ` 'target' => '_self',`, - " ],", - " ],", - " ],", - " ])", - "
", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("@include directive should not add an extra comma on long view name #504", async () => { - const content = [ - `@include('livewire.cx', ['account' => $account])`, - `@include('livewire.cx.equipment-list-internal.account', ['account' => $account])`, - "
", - "
", - "
", - `@include('livewire.cx.equipment-list-internal.account', ['account' => $account])`, - "
", - "
", - "
", - "
", - "
", - "
", - "
", - "
", - "
", - `@include('livewire.cx.equipment-list-internal.account', ['account' => $account])`, - "
", - "
", - "
", - "
", - "
", - "
", - ].join("\n"); - - const expected = [ - `@include('livewire.cx', ['account' => $account])`, - `@include('livewire.cx.equipment-list-internal.account', ['account' => $account])`, - "
", - "
", - "
", - ` @include('livewire.cx.equipment-list-internal.account', ['account' => $account])`, - "
", - "
", - "
", - "
", - "
", - "
", - "
", - "
", - "
", - ` @include('livewire.cx.equipment-list-internal.account', ['account' => $account])`, - "
", - "
", - "
", - "
", - "
", - "
", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("inline script tag should keep its element #508", async () => { - const content = [ - `

foo

bar

blah

`, - ].join("\n"); - - const expected = [ - "

foo

", - "

bar

", - "", - "

blah

", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("inline @php directive in script tag", async () => { - const content = [ - "", - ].join("\n"); - - const expected = [ - "", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("@checked directive", async () => { - const content = [ - `active)) />`, - ].join("\n"); - - const expected = [ - `active)) />`, - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("@selected directive", async () => { - const content = [ - `", - ].join("\n"); - - const expected = [ - `", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("@disabled directive", async () => { - const content = [ - ``, - ].join("\n"); - - const expected = [ - ``, - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("@includeIf, @includeWhen, @includeUnless and @includeFirst directive", async () => { - const content = [ - "
", - `@includeIf('livewire.cx.equipment-list-internal.account',['status'=>'complete',`, - `'foo'=>$user,'bar'=>$bbb,'baz'=>$myVariable])`, - "
", - "
", - `@includeWhen($boolean,'livewire.cx.equipment-list-internal.account',['status'=>'complete',`, - `'foo'=>$user,'bar'=>$bbb,'baz'=>$myVariable])`, - "
", - "
", - `@includeUnless($boolean,'livewire.cx.equipment-list-internal.account',['status'=>'complete',`, - `'foo'=>$user,'bar'=>$bbb,'baz'=>$myVariable])`, - "
", - "
", - `@includeFirst(['custom.admin','admin'],['status'=>'complete',`, - `'foo'=>$user,'bar'=>$bbb,'baz'=>$myVariable])`, - "
", - ].join("\n"); - - const expected = [ - "
", - ` @includeIf('livewire.cx.equipment-list-internal.account', [`, - ` 'status' => 'complete',`, - ` 'foo' => $user,`, - ` 'bar' => $bbb,`, - ` 'baz' => $myVariable,`, - " ])", - "
", - "
", - ` @includeWhen($boolean, 'livewire.cx.equipment-list-internal.account', [`, - ` 'status' => 'complete',`, - ` 'foo' => $user,`, - ` 'bar' => $bbb,`, - ` 'baz' => $myVariable,`, - " ])", - "
", - "
", - ` @includeUnless($boolean, 'livewire.cx.equipment-list-internal.account', [`, - ` 'status' => 'complete',`, - ` 'foo' => $user,`, - ` 'bar' => $bbb,`, - ` 'baz' => $myVariable,`, - " ])", - "
", - "
", - " @includeFirst(", - ` ['custom.admin', 'admin'],`, - ` ['status' => 'complete', 'foo' => $user, 'bar' => $bbb, 'baz' => $myVariable]`, - " )", - "
", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("long expression blade brace", async () => { - const content = [ - ``, - " {{ formatName($relatedAuthority->name) }}", - ` `, - "
", - ].join("\n"); - - const expected = [ - `isCorporateBody()", - ` ? 'knowsAbout'`, - ` : 'member')`, - " : ($relatedAuthority->isCorporateBody()", - ` ? 'memberOf'`, - ` : 'knows') }}">`, - " {{ formatName($relatedAuthority->name) }}", - ` `, - "
", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("whitespace sensitive tag should keep its content unformatted", async () => { - const content = [ - "
", - `@foreach (config('translatable.locales') as $i => $locale)`, - `
`, - `
`, - ` `, - "
", - "
", - "@endforeach", - "
", - "
", - "
",
-			"aaaa 
", - "
", - ].join("\n"); - - const expected = [ - "
", - ` @foreach (config('translatable.locales') as $i => $locale)`, - `
`, - `
`, - ` `, - "
", - "
", - " @endforeach", - "
", - "
", - "
",
-			"aaaa 
", - "
", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("sort blade brace mixes classes", async () => { - const content = [ - "`, - "", - ].join("\n"); - - const expected = [ - "`, - "", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected, { - sortTailwindcssClasses: true, - }); - }); - - test("string literal with line break in raw php directive", async () => { - const content = [ - "
", - "
", - " @php", - ` $myvar = "lorem`, - ` ipsum";`, - ` $foo = "lorem`, - "", - "multiline", - ` ipsum";`, - " @endphp", - "
", - "
", - ].join("\n"); - - const expected = [ - "
", - "
", - " @php", - ` $myvar = "lorem`, - ` ipsum";`, - ` $foo = "lorem`, - "", - "multiline", - ` ipsum";`, - " @endphp", - "
", - "
", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("it should not throw exception even if inline component attribute has syntax error", async () => { - const content = [``].join("\n"); - const expected = [``, ""].join("\n"); - - await util.doubleFormatCheck(content, expected); - await expect(new BladeFormatter().format(content)).resolves.not.toThrow( - "SyntaxError", - ); - }); - - test("syntax error on multiline component attribute throws a syntax error", async () => { - const content = [ - ``, - ].join("\n"); - - await expect(new BladeFormatter().format(content)).rejects.toThrow( - "SyntaxError", - ); - }); - - test("directive inside component attribute", async () => { - const content = [ - `@section('body')`, - ` `, - "@endsection", - ``, - " Submit", - "", - ].join("\n"); - - const expected = [ - `@section('body')`, - ` `, - "@endsection", - ``, - " Submit", - "", - "", - ].join("\n"); - - await util.doubleFormatCheck(content, expected); - }); - - test("colon prefixed attribute #552", async () => { - const content = [ - "", - "@if ($user)", - "Is HR", - "@endif", - "", - ``, - `