Skip to content

ryangjchandler/blade-lint

Repository files navigation

Validate and analyse Blade templates in your Laravel project.

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.

Installation

You can install the package via Composer:

composer require ryangjchandler/blade-lint

Setup Blade Lint inside of your project:

php artisan install:blade-lint

Usage

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...
    ],

];

Rules

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.

Custom Rules

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 --}}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

Validate and analyse Blade templates in your Laravel project.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Languages