Blade Lint provides a set of tools for linting Blade templates. It comes with a set of default rules, as well as APIs for writing your own custom rules. Under the hood, it is powered by the excellent blade-parser
package.
This package is not designed to format Blade templates. If you're looking for a Blade formatter, use Pint instead.
You can install the package via Composer:
composer require ryangjchandler/blade-lint
Setup Blade Lint inside of your project:
php artisan install:blade-lint
This package provides a single blade:lint
command.
php artisan blade:lint
This will validate the syntax and run all registered Rule
classes against the files found in your project.
To configure which rules run, modify the rules
array inside of the config/blade-lint.php
file.
For a full list of available rules, check the Rules section.
use RyanChandler\BladeLint\Rules;
return [
'rules' => [
Rules\VerifyDirectivePairs::class,
Rules\DisallowRawEcho::class,
Rules\VerifyForelseHasEmpty::class,
// Register your custom rules here...
],
];
This package provides a set of useful rules out of the box. Use the table below to find a rule and an example of what it does.
Coming soon.
Writing your own custom rules can be a really powerful way of improving your code quality.
Start by creating a class that implements RyanChandler\BladeLint\Rules\Rule
.
namespace App\Linting\Rules;
use RyanChandler\BladeLint\Rules\Rule;
use RyanChandler\BladeLint\ErrorCollector;
use Stillat\BladeParser\Nodes\AbstractNode;
class MyCustomRule implements Rule
{
public function check(AbstractNode $node, ErrorCollector $errorCollector): void
{
//
}
public function getRuleId(): string
{
return 'internal.my-custom-rule';
}
}
The check()
method is used to validate a single node. For more information on the structure of a Node
, consult the Blade parser documentation.
The getRuleId()
method is used to specify the "readable" name for a rule. This allows you to ignore or silence the rule in certain places throughout your Blade templates.
{!! $html !!} {{-- @lint:ignore internal.my-custom-rule --}}
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.