Skip to content
Merged
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
5 changes: 4 additions & 1 deletion lib/super_slim/linter/rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,10 @@ def first_variable_by_line(code)
return variable_by_line unless code.include?("@")

Prism.lex(code).value.each do |token,|
variable_by_line[token.location.start_line] ||= token.value if token.type == :INSTANCE_VARIABLE
# An `@` that no name follows is lexed as one too: `@<%= name %>` in a generator's template.
next unless token.type == :INSTANCE_VARIABLE && token.value != "@"

variable_by_line[token.location.start_line] ||= token.value
end
variable_by_line
end
Expand Down
8 changes: 8 additions & 0 deletions test/linter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ def test_instance_variables_wherever_a_partial_holds_ruby
"12: @event", "14: @title"], messages
end

def test_an_at_sign_without_a_name_is_no_instance_variable
source = "= form_for(@<%= singular_table_name %>) do |form|\n = form.input @field\n"
messages = SuperSlim::Linter.new.call("_form.html.slim", source).select { _1.rule == "InstanceVariables" }
.map { |offense| "#{offense.lineno}: #{offense.message}" }

assert_equal ["2: Pass `@field` to the partial as a local"], messages
end

def test_unused_locals
assert_offenses ["UnusedLocals:1"], "/# locals: (post:, form:)\n\nh2 = post.title\n", path: "_card.html.slim"
read_everywhere = <<~SLIM
Expand Down
Loading