Skip to content

Commit

Permalink
Check variants when looking for instance variables
Browse files Browse the repository at this point in the history
There was a bug in the regex used to determine if a template is a partial, which means it was excluding variants.
  • Loading branch information
iainbeeston committed Oct 22, 2024
1 parent 67e9f5c commit 84f273b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/erb_lint/linters/partial_instance_variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class PartialInstanceVariable < Linter

def run(processed_source)
instance_variable_regex = /\s@\w+/
return unless processed_source.filename.match?(%r{(\A|.*/)_[^/\s]*\.html\.erb\z}) &&
return unless processed_source.filename.match?(%r{(\A|.*/)_[^/\s]*\.html(\+[^/\s]+)?\.erb\z}) &&
processed_source.file_content.match?(instance_variable_regex)

add_offense(
Expand Down
5 changes: 4 additions & 1 deletion spec/erb_lint/linters/partial_instance_variable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
let(:linter) { described_class.new(file_loader, linter_config) }
let(:processed_source_one) { ERBLint::ProcessedSource.new("_file.html.erb", file) }
let(:processed_source_two) { ERBLint::ProcessedSource.new("app/views/_a_model/a_view.html.erb", file) }
let(:processed_source_three) { ERBLint::ProcessedSource.new("_variant.html+mobile.erb", file) }
let(:offenses) { linter.offenses }
before do
linter.run(processed_source_one)
linter.run(processed_source_two)
linter.run(processed_source_three)
end

describe "offenses" do
subject { offenses }

context "when instance varaible is not present" do
context "when instance variable is not present" do
let(:file) { "<%= user.first_name %>" }
it { expect(subject).to(eq([])) }
end
Expand All @@ -27,6 +29,7 @@
it do
expect(subject).to(eq([
build_offense(processed_source_one, 7..32, "Instance variable detected in partial."),
build_offense(processed_source_three, 7..32, "Instance variable detected in partial."),
]))
end
end
Expand Down

0 comments on commit 84f273b

Please sign in to comment.