-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #395 from Shopify/strict-locals-linter
Add strict locals linter
- Loading branch information
Showing
3 changed files
with
187 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
module ERBLint | ||
module Linters | ||
# Enforces the use of strict locals in Rails view partial templates. | ||
class StrictLocals < Linter | ||
include LinterRegistry | ||
|
||
STRICT_LOCALS_REGEX = /\s+locals:\s+\((.*)\)/ | ||
|
||
def initialize(file_loader, config) | ||
super | ||
end | ||
|
||
def run(processed_source) | ||
return unless processed_source.filename.match?(%r{(\A|.*/)_[^/\s]*\.html\.erb\z}) | ||
|
||
file_content = processed_source.file_content | ||
return if file_content.empty? | ||
|
||
strict_locals_node = processed_source.ast.descendants(:erb).find do |erb_node| | ||
indicator_node, _, code_node, _ = *erb_node | ||
|
||
indicator_node_str = indicator_node&.deconstruct&.last | ||
next unless indicator_node_str == "#" | ||
|
||
code_node_str = code_node&.deconstruct&.last | ||
|
||
code_node_str.match(STRICT_LOCALS_REGEX) | ||
end | ||
|
||
unless strict_locals_node | ||
add_offense( | ||
processed_source.to_source_range(0...processed_source.file_content.size), | ||
<<~EOF.chomp, | ||
Missing strict locals declaration. | ||
Add <%# locals: () %> at the top of the file to enforce strict locals. | ||
EOF | ||
) | ||
end | ||
end | ||
|
||
def autocorrect(_processed_source, offense) | ||
lambda do |corrector| | ||
corrector.insert_before(offense.source_range, "<%# locals: () %>\n") | ||
end | ||
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,113 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
describe ERBLint::Linters::StrictLocals do | ||
let(:linter_config) { described_class.config_schema.new } | ||
let(:file_loader) { ERBLint::FileLoader.new(".") } | ||
let(:linter) { described_class.new(file_loader, linter_config) } | ||
|
||
subject { linter.offenses } | ||
before { linter.run(processed_source) } | ||
|
||
context "when the ERB is not a view partial" do | ||
let(:file) { <<~FILE } | ||
<div> | ||
<%= foo %> | ||
</div> | ||
FILE | ||
|
||
context "when the ERB is a simple file" do | ||
let(:processed_source) { ERBLint::ProcessedSource.new("file.html.erb", file) } | ||
|
||
it "does not report any offenses" do | ||
expect(subject).to(be_empty) | ||
end | ||
end | ||
|
||
context "when the ERB is a nested file" do | ||
let(:processed_source) { ERBLint::ProcessedSource.new("foo/bar/baz/my_file.html.erb", file) } | ||
|
||
it "does not report any offenses" do | ||
expect(subject).to(be_empty) | ||
end | ||
end | ||
end | ||
|
||
context "when the ERB is empty" do | ||
let(:file) { "" } | ||
let(:processed_source) { ERBLint::ProcessedSource.new("_file.html.erb", file) } | ||
|
||
it "does not report any offenses" do | ||
expect(subject).to(be_empty) | ||
end | ||
end | ||
|
||
context "when the ERB is a view partial" do | ||
let(:processed_source) { ERBLint::ProcessedSource.new("_file.html.erb", file) } | ||
|
||
context "when the ERB contains a strict locals declaration at the top of the file" do | ||
let(:file) { <<~FILE } | ||
<%# locals: (foo: "bar") %> | ||
<div> | ||
<%= foo %> | ||
</div> | ||
FILE | ||
|
||
it "does not report any offenses" do | ||
expect(subject).to(be_empty) | ||
end | ||
end | ||
|
||
context "when the ERB contains a strict locals declaration anywhere else in the file" do | ||
let(:file) { <<~FILE } | ||
<div> | ||
<%= foo %> | ||
</div> | ||
<%# locals: (foo: "bar") %> | ||
FILE | ||
|
||
it "does not report any offenses" do | ||
expect(subject).to(be_empty) | ||
end | ||
end | ||
|
||
context "when the ERB contains an empty strict locals declaration" do | ||
let(:file) { <<~FILE } | ||
<%# locals: () %> | ||
<div> | ||
<%= foo %> | ||
</div> | ||
FILE | ||
|
||
it "does not report any offenses" do | ||
expect(subject).to(be_empty) | ||
end | ||
end | ||
|
||
context "when the ERB does not contain a strict locals declaration" do | ||
let(:file) { <<~FILE } | ||
<div> | ||
<%= foo %> | ||
</div> | ||
FILE | ||
let(:corrector) { ERBLint::Corrector.new(processed_source, subject) } | ||
let(:corrected_content) { corrector.corrected_content } | ||
|
||
it "reports an offense" do | ||
expect(subject.size).to(eq(1)) | ||
end | ||
|
||
it "reports the suggested fix" do | ||
expect(subject.first.message).to(include( | ||
"Missing strict locals declaration.\n", | ||
"Add <%# locals: () %> at the top of the file to enforce strict locals.", | ||
)) | ||
end | ||
|
||
it "corrects the file" do | ||
expect(corrected_content).to(eq("<%# locals: () %>\n<div>\n <%= foo %>\n</div>\n")) | ||
end | ||
end | ||
end | ||
end |