-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Strict locals missing linter (#175)
And here I propose another linter that we would love to use in our project. It checks for [Strict locals](https://guides.rubyonrails.org/action_view_overview.html#strict-locals) magic line and reports when the magic line is missing from the template. Strict locals are encouraged by some in Rails partials as they allow to define an explicit "API" for the templates showing clearly which variables should be passed into the template when rendering it vs. which ones are purely local for the template. They also allow to define default values. I wrote a little [post](https://dev.to/nejremeslnici/strict-locals-in-slim-haml-partials-in-rails-2f73) about them recently. Thanks for your considering this! ![image](https://github.com/sds/slim-lint/assets/462701/c34333ec-4dbe-42f4-8e34-fabf879ecc69) ![image](https://github.com/sds/slim-lint/assets/462701/96c491ee-4ccd-44fe-8bcb-4227770af5c5)
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module SlimLint | ||
# Reports on missing strict locals magic line in Slim templates. | ||
class Linter::StrictLocalsMissing < Linter | ||
include LinterRegistry | ||
|
||
on_start do |_sexp| | ||
unless document.source =~ %r{/#\s+locals:\s+\(.*\)} | ||
dummy_node = Struct.new(:line) | ||
report_lint(dummy_node.new(1), 'Strict locals magic line is missing') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe SlimLint::Linter::StrictLocalsMissing do | ||
include_context 'linter' | ||
|
||
context 'when magic line defines strict locals' do | ||
let(:slim) { '/# locals: (foo:, bar:)' } | ||
|
||
it { should_not report_lint } | ||
end | ||
|
||
context 'when magic line defines empty strict locals' do | ||
let(:slim) { '/# locals: ()' } | ||
|
||
it { should_not report_lint } | ||
end | ||
|
||
context 'when magic line is not at the top of the file' do | ||
let(:slim) { <<-SLIM } | ||
h1 Hello | ||
= call(foo, bar) | ||
/# locals: (foo:, bar:) | ||
SLIM | ||
|
||
it { should_not report_lint } | ||
end | ||
|
||
context 'when template has no strict locals definition' do | ||
let(:slim) { 'h1 hello' } | ||
|
||
it { should report_lint } | ||
end | ||
end |