Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Adds blade support #256

Draft
wants to merge 66 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
89c97cb
Adds blade support
nunomaduro Mar 15, 2024
a542c13
Improves error handling
nunomaduro Mar 15, 2024
fd39d90
Rebuilds
nunomaduro Mar 15, 2024
3b45cb5
Deletes version file
nunomaduro Mar 15, 2024
4b2c343
Ignores version file
nunomaduro Mar 15, 2024
741f377
Update app/Fixers/LaravelBladeFixer.php
nunomaduro Mar 15, 2024
4ee92d9
Update resources/presets/laravel.php
nunomaduro Mar 15, 2024
9458bc7
Rebuilds
nunomaduro Mar 15, 2024
25160ce
Validates with node and npm are installed
nunomaduro Mar 15, 2024
c22d555
Rebuilds
nunomaduro Mar 15, 2024
e9fce59
Changes underlying prettier plugins
nunomaduro Mar 16, 2024
ece1d68
Changes version
nunomaduro Mar 16, 2024
8b547fc
Rebuilds
nunomaduro Mar 16, 2024
dce1b6b
Merge pull request #258 from laravel/feat/prettier-plugin
nunomaduro Mar 16, 2024
fb4061e
Improves error handling
nunomaduro Mar 16, 2024
ac1dbb7
Rebuilds
nunomaduro Mar 16, 2024
dbde266
Merge pull request #259 from laravel/feat/prettier-plugin
nunomaduro Mar 16, 2024
8a6aba7
chore: bumps dependencies
nunomaduro Mar 16, 2024
4e19273
Updates prettier plugin blade version
nunomaduro Mar 17, 2024
d191f53
Rebuilds
nunomaduro Mar 17, 2024
b94e796
Fix tests for the laravel skeleton
Jubeki Mar 17, 2024
e68acf4
Fix static analysis
Jubeki Mar 17, 2024
eaff017
Fix static analysis
Jubeki Mar 17, 2024
6c66735
Fix failing tests
Jubeki Mar 17, 2024
7ec420d
Merge pull request #260 from Jubeki/fix-tests
nunomaduro Mar 17, 2024
8615608
Uses Pint to format code within blade tags
nunomaduro Mar 18, 2024
647ed31
wip
nunomaduro Mar 18, 2024
d3bd5e4
wip
nunomaduro Mar 18, 2024
32f42e5
Uses prettier as worker mode
nunomaduro Mar 18, 2024
59ace97
Uses `"printWidth": 120`
nunomaduro Mar 18, 2024
b74f3e8
Resolves `LaravelBladeFixer` from IOC
nunomaduro Mar 18, 2024
ea25d2e
Rebuilds
nunomaduro Mar 18, 2024
85ad368
Properly ensures the process is terminated
nunomaduro Mar 18, 2024
1ad5058
Rebuilds
nunomaduro Mar 18, 2024
d23f749
Bumps `prettier-plugin-blade`
nunomaduro Mar 18, 2024
6173045
Fixes worker getting confused
nunomaduro Mar 18, 2024
04a2add
Rebuilds
nunomaduro Mar 18, 2024
7558542
Refactors ignorables
nunomaduro Mar 19, 2024
3b64ce8
Rebuilds
nunomaduro Mar 19, 2024
2618dac
Fixes single line attributes
nunomaduro Mar 19, 2024
54bc0ff
Rebuilds
nunomaduro Mar 19, 2024
1511a37
Adds `--blade` option
nunomaduro Mar 20, 2024
42ee061
Rebuilds
nunomaduro Mar 20, 2024
8c4ce67
Keeps improving linting of blade files
nunomaduro Mar 23, 2024
83e50c5
Rebuilds
nunomaduro Mar 23, 2024
be09e72
Fixes configuration
nunomaduro Mar 23, 2024
cc01378
Rebuilds
nunomaduro Mar 23, 2024
61cb222
Adjust wrapping
nunomaduro Mar 23, 2024
2f093ca
Rebuilds
nunomaduro Mar 23, 2024
ba86429
Adjusts style
nunomaduro Mar 24, 2024
5a889e3
Rebuilds
nunomaduro Mar 24, 2024
7e97525
Ignores cache for now
nunomaduro Mar 26, 2024
b325827
150 printWidth
nunomaduro Mar 26, 2024
33bbf45
Rebuilds
nunomaduro Mar 26, 2024
4d3e126
Puts SVGs in the same line
nunomaduro Mar 26, 2024
3148bcc
Coding style
nunomaduro Mar 26, 2024
f3371d2
Ignores minified code
nunomaduro Mar 26, 2024
58e661c
Refactors code
nunomaduro Mar 27, 2024
4a086d3
Rebuilds
nunomaduro Mar 27, 2024
d06e89d
Style
nunomaduro Apr 15, 2024
d11cbbd
inline paragrahps
nunomaduro Apr 15, 2024
e775568
Merge branch 'main' into feat/blade
nunomaduro Apr 15, 2024
f80b15e
Rebuils
nunomaduro Apr 15, 2024
50229c4
Rebuilds
nunomaduro Apr 15, 2024
587cb48
bumps dependencies
nunomaduro Apr 22, 2024
3da5103
Rebuilds
nunomaduro Apr 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
/vendor
composer.lock
.phpunit.result.cache
/node_sandbox/package-lock.json
/node_sandbox/node_modules
/node_sandbox/version
7 changes: 1 addition & 6 deletions app/Actions/FixCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Actions;

use App\Factories\ConfigurationResolverFactory;
use LaravelZero\Framework\Exceptions\ConsoleException;
use PhpCsFixer\Runner\Runner;

class FixCode
Expand Down Expand Up @@ -35,11 +34,7 @@ public function __construct(
*/
public function execute()
{
try {
[$resolver, $totalFiles] = ConfigurationResolverFactory::fromIO($this->input, $this->output);
} catch (ConsoleException $exception) {
return [$exception->getCode(), []];
}
[$resolver, $totalFiles] = ConfigurationResolverFactory::fromIO($this->input, $this->output);

if (is_null($this->input->getOption('format'))) {
$this->progress->subscribe();
Expand Down
49 changes: 49 additions & 0 deletions app/BladeFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App;

use App\Contracts\PostProcessor;
use App\Contracts\PreProcessor;

class BladeFormatter
{
/**
* Create a new blade formatter instance.
*
* @param \App\Prettier $prettier
* @param array<int, PreProcessor|PostProcessor> $processors
* @return void
*/
public function __construct(
protected $prettier,
protected $processors,
) {
//
}

/**
* Format the given content.
*
* @param string $path
* @param string $content
* @return string
*/
public function format($path, $content)
{
foreach ($this->processors as $processor) {
if ($processor instanceof PreProcessor) {
$content = $processor->preProcess($content);
}
}

$content = $this->prettier->format($path, $content);

foreach ($this->processors as $processor) {
if ($processor instanceof PostProcessor) {
$content = $processor->postProcess($content);
}
}

return $content;
}
}
1 change: 1 addition & 0 deletions app/Commands/DefaultCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protected function configure()
->setDefinition(
[
new InputArgument('path', InputArgument::IS_ARRAY, 'The path to fix', [(string) getcwd()]),
new InputOption('blade', '', InputOption::VALUE_NONE, 'Fixes blade files (experimental)'),
new InputOption('config', '', InputOption::VALUE_REQUIRED, 'The configuration that should be used'),
new InputOption('no-config', '', InputOption::VALUE_NONE, 'Disable loading any configuration file'),
new InputOption('preset', '', InputOption::VALUE_REQUIRED, 'The preset that should be used'),
Expand Down
14 changes: 14 additions & 0 deletions app/Contracts/PostProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Contracts;

interface PostProcessor
{
/**
* Process the content after the fixer has run.
*
* @param string $content
* @return string
*/
public function postProcess($content);
}
14 changes: 14 additions & 0 deletions app/Contracts/PreProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Contracts;

interface PreProcessor
{
/**
* Process the content before the fixer runs.
*
* @param string $content
* @return string
*/
public function preProcess($content);
}
10 changes: 10 additions & 0 deletions app/Exceptions/PrettierException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Exceptions;

use Exception;

class PrettierException extends Exception
{
//
}
13 changes: 11 additions & 2 deletions app/Factories/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace App\Factories;

use App\BladeFormatter;
use App\Fixers\LaravelBlade\Fixer;
use App\Repositories\ConfigurationJsonRepository;
use PhpCsFixer\Config;
use PhpCsFixer\Finder;
use Symfony\Component\Console\Input\InputInterface;

class ConfigurationFactory
{
Expand All @@ -18,7 +21,6 @@ class ConfigurationFactory
'_ide_helper_models.php',
'_ide_helper.php',
'.phpstorm.meta.php',
'*.blade.php',
];

/**
Expand All @@ -45,7 +47,10 @@ public static function preset($rules)
->setFinder(self::finder())
->setRules(array_merge($rules, resolve(ConfigurationJsonRepository::class)->rules()))
->setRiskyAllowed(true)
->setUsingCache(true);
->setUsingCache(true)
->registerCustomFixers([
new Fixer(resolve(BladeFormatter::class)),
]);
}

/**
Expand All @@ -57,6 +62,10 @@ public static function finder()
{
$localConfiguration = resolve(ConfigurationJsonRepository::class);

if (resolve(InputInterface::class)->getOption('blade') === false) {
$notName = array_merge(static::$notName, ['*.blade.php']);
}

$finder = Finder::create()
->notName(static::$notName)
->exclude(static::$exclude)
Expand Down
7 changes: 6 additions & 1 deletion app/Factories/ConfigurationResolverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Factories;

use App\NodeSandbox;
use App\Project;
use App\Repositories\ConfigurationJsonRepository;
use ArrayIterator;
Expand Down Expand Up @@ -43,6 +44,10 @@ public static function fromIO($input, $output)
abort(1, 'Preset not found.');
}

if ($input->getOption('blade') && $preset !== 'laravel') {
abort(1, 'The blade option is only available for the laravel preset.');
}

$resolver = new ConfigurationResolver(
new Config('default'),
[
Expand All @@ -61,7 +66,7 @@ public static function fromIO($input, $output)
realpath(sys_get_temp_dir()),
md5(
app()->isProduction()
? implode('|', $path)
? (implode('|', $path) . '||' . NodeSandbox::VERSION)
: (string) microtime()
),
]),
Expand Down
90 changes: 90 additions & 0 deletions app/Fixers/LaravelBlade/Fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace App\Fixers\LaravelBlade;

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Tokens;
use SplFileInfo;

class Fixer extends AbstractFixer
{
/**
* The list of ignorables.
*
* @var array<int, string>
*/
protected static $ignorables = [
Ignorables\Envoy::class,
Ignorables\MarkdownMail::class,
];

/**
* {@inheritdoc}
*/
public function __construct(protected $formatter)
{
}

/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'Laravel/blade';
}

/**
* {@inheritdoc}
*/
public function isCandidate(Tokens $tokens): bool
{
return true;
}

/**
* {@inheritdoc}
*/
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition('Fixes Laravel Blade files.', []);
}

/**
* {@inheritdoc}
*/
public function getPriority(): int
{
return -2;
}

/**
* {@inheritdoc}
*
* @throws \App\Exceptions\PrettierException
*/
protected function applyFix(SplFileInfo $file, Tokens $tokens): void
{
$path = $file->getRealPath();

if (! str_ends_with($path, '.blade.php')) {
return;
}

$content = $tokens->generateCode();

foreach (static::$ignorables as $ignorable) {
if (app()->call($ignorable, [
'path' => $path,
'content' => $content,
])) {
return;
}
}

$content = $this->formatter->format($path, $content);

$tokens->setCode($content);
}
}
20 changes: 20 additions & 0 deletions app/Fixers/LaravelBlade/Ignorables/Envoy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Fixers\LaravelBlade\Ignorables;

class Envoy
{
/**
* Whether the given blade file should be ignored.
*
* @param string $path
* @return bool
*/
public function __invoke($path)
{
return in_array(basename($path), [
'envoy.blade.php',
'Envoy.blade.php',
]);
}
}
17 changes: 17 additions & 0 deletions app/Fixers/LaravelBlade/Ignorables/MarkdownMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Fixers\LaravelBlade\Ignorables;

class MarkdownMail
{
/**
* Whether the given blade file should be ignored.
*
* @param string $content
* @return bool
*/
public function __invoke($content)
{
return str_contains($content, '<x-mail::') || str_contains($content, '@component(\'mail::');
}
}
Loading