Skip to content

Commit

Permalink
Merge pull request #335 from tighten/adc/fix-space-after
Browse files Browse the repository at this point in the history
Fix issue with `SpaceAfterBladeDirectives`
  • Loading branch information
driftingly authored Mar 17, 2023
2 parents d097fc2 + 101e1d1 commit bd3dc5d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bin/tlint
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env php
<?php

const TLINT_VERSION = 'v8.0.2';
const TLINT_VERSION = 'v8.0.3';

foreach (
[
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"phpunit/phpunit": "^9.0",
"spatie/ray": "^1.36",
"symfony/var-dumper": "^5.0",
"tightenco/duster": "^0.5.5"
"tightenco/duster": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 3 additions & 1 deletion src/Formatters/SpaceAfterBladeDirectives.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public function format(Parser $parser, Lexer $lexer): string
);

foreach ($matches as $match) {
if (in_array($match[1] ?? null, Linter::SPACE_AFTER) && ($match[2] ?? null) === '') {
$match = array_pad($match, 5, null);

if (in_array($match[1], Linter::SPACE_AFTER) && $match[2] === '') {
$codeLine = str_replace($match[0], "@{$match[1]} {$match[3]}", $codeLine);

$this->code = $this->replaceCodeLine($index + 1, $codeLine);
Expand Down
34 changes: 34 additions & 0 deletions tests/Formatting/Formatters/SpaceAfterBladeDirectivesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ public function it_ignores_correctly_spaced_directives()
@foreach ($users as $user)
<li>{{ $user->name }}</li>
@endforeach
@auth
<p>Authenticated</p>
@endauth
file;

$formatted = (new TFormat)->format(
Expand Down Expand Up @@ -237,6 +241,36 @@ public function it_adds_space_to_kitchen_sink()
@while (true)
<p>I'm looping forever.</p>
@endwhile
file;

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

/** @test */
public function it_fixes_directives_spanning_multiple_lines()
{
$file = <<<'file'
@foreach([
Laravel\Memberships\Membership::MEMBER_ROLE,
Laravel\Memberships\Membership::SUPERVISION_ROLE,
Laravel\Memberships\Membership::ADMIN_ROLE,
] as $role)
<option value="{{ $role }}">{{ $role }}</option>
@endforeach
file;

$formatted = (new TFormat)->format(
new SpaceAfterBladeDirectives($file)
);

$correctlyFormatted = <<<'file'
@foreach ([
Laravel\Memberships\Membership::MEMBER_ROLE,
Laravel\Memberships\Membership::SUPERVISION_ROLE,
Laravel\Memberships\Membership::ADMIN_ROLE,
] as $role)
<option value="{{ $role }}">{{ $role }}</option>
@endforeach
file;

$this->assertEquals($correctlyFormatted, $formatted);
Expand Down
2 changes: 1 addition & 1 deletion tests/Linting/EmptyDiffDoesNotTriggerWarningTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public function gracefully_handles_empty_diff()
{
$files = ParsesGitOutput::parseFilesFromGitDiffOutput('');

$this->assertCount(0, $files);
$this->assertCount(0, iterator_to_array($files));
}
}
20 changes: 20 additions & 0 deletions tests/Linting/Linters/SpaceAfterBladeDirectivesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,24 @@ public function it_catches_missing_space_kitchen_sink()
$this->assertEquals(23, $lints[8]->getNode()->getLine());
$this->assertEquals(29, $lints[9]->getNode()->getLine());
}

/** @test */
public function it_catches_directives_spanning_multiple_lines()
{
$file = <<<'file'
@foreach([
Laravel\Memberships\Membership::MEMBER_ROLE,
Laravel\Memberships\Membership::SUPERVISION_ROLE,
Laravel\Memberships\Membership::ADMIN_ROLE,
] as $role)
<option value="{{ $role }}">{{ $role }}</option>
@endforeach
file;

$lints = (new TLint)->lint(
new SpaceAfterBladeDirectives($file)
);

$this->assertEquals(1, $lints[0]->getNode()->getLine());
}
}

0 comments on commit bd3dc5d

Please sign in to comment.