roderio / blade-rs
Fast, Node-free linter and formatter for Laravel Blade templates. Installs the native blade binary.
Requires
- php: >=8.1
- composer-runtime-api: ^2.2
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A fast, Node-free linter and formatter for Laravel Blade templates, and the lossless Blade parser behind them, written in Rust.
Try it in your browser: paste a template and see what blade lint finds and how blade fmt formats it.
Status: v0.2.
blade lintandblade fmtare tested against 879 real-world templates from Laravel, Filament, Livewire, Breeze, Jetstream, Pulse, BookStack and laravel.io. The formatter is also checked by rendering templates with Laravel's own Blade engine before and after formatting.
Install
# Composer, for a Laravel project composer require --dev roderio/blade-rs vendor/bin/blade lint resources/views # npm npm install --save-dev blade-rs npx blade-rs lint resources/views # Homebrew brew install roderio/tap/blade-cli # Cargo cargo install blade-cli
Or download a prebuilt binary (Linux including Alpine, macOS, Windows) from the latest release, or use the installer scripts:
# macOS, Linux curl --proto '=https' --tlsv1.2 -LsSf https://github.com/roderio/blade-rs/releases/latest/download/blade-cli-installer.sh | sh # Windows (PowerShell) powershell -ExecutionPolicy Bypass -c "irm https://github.com/roderio/blade-rs/releases/latest/download/blade-cli-installer.ps1 | iex"
The Composer package is a small PHP launcher. On first run it downloads the binary for your platform from the matching GitHub release, checks its SHA-256, and caches it in vendor/.
blade lint
$ blade lint resources/views resources/views/posts/edit.blade.php:4:11: error[form-method]: browsers send method="PUT" as GET; use method="POST" with @method('PUT') | 4 | <form method="PUT" action="{{ route('posts.update', $post) }}"> | ^^^^^^^^^^^^ 1 error in 23 files
It finds problems Blade only reveals when a page breaks: unclosed @if/@foreach/@section, {{ }} with nothing in it, forms that browsers send with the wrong method or without a CSRF token, unescaped {!! !!} output, and more. See all rules.
-
Lints every
*.blade.phpunder the given paths (default:.), skipping.gitignored and hidden directories. -
--format human(default),json, orgithub(annotations on pull requests in GitHub Actions). -
Exits with
0if there are no errors (warnings are fine),1if there are errors, and2if it couldn't run (a badblade.toml, a missing path, an unreadable file). -
Configure rules in
blade.toml:[lint.rules] raw-echo = "off" # "error", "warning" or "off"
-
Silence one line with
{{-- blade-lint-ignore-next-line raw-echo --}}above it.
blade fmt
$ blade fmt resources/views formatted 42 files, 131 already formatted
It indents Blade blocks and HTML elements together, trims trailing whitespace and extra blank lines, writes {{ $x }} and @if ($x) consistently, and puts long tags one attribute per line. It's built not to change what a page renders: <pre>, <textarea>, <script>, <style>, whitespace-pre elements, @verbatim and @php keep every byte; Markdown mail and plain-text views keep their layout; and each result is checked against the original before it's written. Anything it can't format safely is left as it is. What it assumes.
blade fmt --checklists the files that would change and exits with1if any would (for CI).blade fmt --stdinformats standard input, for VS Code, Neovim, Zed and PhpStorm.- Configure it in
blade.tomlunder[fmt](indent-width,use-tabs,line-width,wrap-attributes).
On the 879-template corpus, blade fmt --check takes 0.8 s and blade-formatter (Node) takes about 25 s. They don't make the same changes; details.
GitHub Action
- uses: actions/checkout@v4 - uses: roderio/blade-rs@v0.2.0 with: version: 0.2.0 args: lint --format github resources/views - uses: roderio/blade-rs@v0.2.0 with: version: 0.2.0 args: fmt --check resources/views
Problems show up as annotations on the pull request. Inputs:
version: the blade-rs release to install. It defaults tolatest, so pinning the action (@v0.2.0) alone doesn't pin the linter; setversiontoo if you want the same results until you upgrade.args: the arguments forblade, defaultlint --format github. They're split on spaces, so a path containing spaces needsworking-directoryinstead.working-directory: where to runblade, default the repository root.
blade-syntax
The parser is a separate crate for other tools to build on. It produces a lossless rowan tree (the same tree library as rust-analyzer) that reproduces the input byte for byte, never fails on broken input, and understands Blade's directives as well as the HTML tags, attributes and components around them. See crates/blade-syntax and how it works.
Planned
- Formatting the PHP inside echoes and directives
- More lint rules (ideas welcome: open a Lint rule issue)
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.