Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check variant files when looking for instance variables #353

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading