Skip to content

Commit bd3dc5d

Browse files
authored
Merge pull request #335 from tighten/adc/fix-space-after
Fix issue with `SpaceAfterBladeDirectives`
2 parents d097fc2 + 101e1d1 commit bd3dc5d

File tree

6 files changed

+60
-4
lines changed

6 files changed

+60
-4
lines changed

bin/tlint

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env php
22
<?php
33

4-
const TLINT_VERSION = 'v8.0.2';
4+
const TLINT_VERSION = 'v8.0.3';
55

66
foreach (
77
[

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"phpunit/phpunit": "^9.0",
2222
"spatie/ray": "^1.36",
2323
"symfony/var-dumper": "^5.0",
24-
"tightenco/duster": "^0.5.5"
24+
"tightenco/duster": "^1.0"
2525
},
2626
"autoload": {
2727
"psr-4": {

src/Formatters/SpaceAfterBladeDirectives.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public function format(Parser $parser, Lexer $lexer): string
2929
);
3030

3131
foreach ($matches as $match) {
32-
if (in_array($match[1] ?? null, Linter::SPACE_AFTER) && ($match[2] ?? null) === '') {
32+
$match = array_pad($match, 5, null);
33+
34+
if (in_array($match[1], Linter::SPACE_AFTER) && $match[2] === '') {
3335
$codeLine = str_replace($match[0], "@{$match[1]} {$match[3]}", $codeLine);
3436

3537
$this->code = $this->replaceCodeLine($index + 1, $codeLine);

tests/Formatting/Formatters/SpaceAfterBladeDirectivesTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ public function it_ignores_correctly_spaced_directives()
155155
@foreach ($users as $user)
156156
<li>{{ $user->name }}</li>
157157
@endforeach
158+
159+
@auth
160+
<p>Authenticated</p>
161+
@endauth
158162
file;
159163

160164
$formatted = (new TFormat)->format(
@@ -237,6 +241,36 @@ public function it_adds_space_to_kitchen_sink()
237241
@while (true)
238242
<p>I'm looping forever.</p>
239243
@endwhile
244+
file;
245+
246+
$this->assertEquals($correctlyFormatted, $formatted);
247+
}
248+
249+
/** @test */
250+
public function it_fixes_directives_spanning_multiple_lines()
251+
{
252+
$file = <<<'file'
253+
@foreach([
254+
Laravel\Memberships\Membership::MEMBER_ROLE,
255+
Laravel\Memberships\Membership::SUPERVISION_ROLE,
256+
Laravel\Memberships\Membership::ADMIN_ROLE,
257+
] as $role)
258+
<option value="{{ $role }}">{{ $role }}</option>
259+
@endforeach
260+
file;
261+
262+
$formatted = (new TFormat)->format(
263+
new SpaceAfterBladeDirectives($file)
264+
);
265+
266+
$correctlyFormatted = <<<'file'
267+
@foreach ([
268+
Laravel\Memberships\Membership::MEMBER_ROLE,
269+
Laravel\Memberships\Membership::SUPERVISION_ROLE,
270+
Laravel\Memberships\Membership::ADMIN_ROLE,
271+
] as $role)
272+
<option value="{{ $role }}">{{ $role }}</option>
273+
@endforeach
240274
file;
241275

242276
$this->assertEquals($correctlyFormatted, $formatted);

tests/Linting/EmptyDiffDoesNotTriggerWarningTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ public function gracefully_handles_empty_diff()
1212
{
1313
$files = ParsesGitOutput::parseFilesFromGitDiffOutput('');
1414

15-
$this->assertCount(0, $files);
15+
$this->assertCount(0, iterator_to_array($files));
1616
}
1717
}

tests/Linting/Linters/SpaceAfterBladeDirectivesTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,24 @@ public function it_catches_missing_space_kitchen_sink()
177177
$this->assertEquals(23, $lints[8]->getNode()->getLine());
178178
$this->assertEquals(29, $lints[9]->getNode()->getLine());
179179
}
180+
181+
/** @test */
182+
public function it_catches_directives_spanning_multiple_lines()
183+
{
184+
$file = <<<'file'
185+
@foreach([
186+
Laravel\Memberships\Membership::MEMBER_ROLE,
187+
Laravel\Memberships\Membership::SUPERVISION_ROLE,
188+
Laravel\Memberships\Membership::ADMIN_ROLE,
189+
] as $role)
190+
<option value="{{ $role }}">{{ $role }}</option>
191+
@endforeach
192+
file;
193+
194+
$lints = (new TLint)->lint(
195+
new SpaceAfterBladeDirectives($file)
196+
);
197+
198+
$this->assertEquals(1, $lints[0]->getNode()->getLine());
199+
}
180200
}

0 commit comments

Comments
 (0)