|
| 1 | +import dedent from "dedent" |
| 2 | +import { describe, test } from "vitest" |
| 3 | +import { ERBNoInstanceVariablesInPartialsRule } from "../../src/rules/erb-no-instance-variables-in-partials.js" |
| 4 | +import { createLinterTest } from "../helpers/linter-test-helper.js" |
| 5 | + |
| 6 | +const { expectNoOffenses, expectError, assertOffenses } = createLinterTest(ERBNoInstanceVariablesInPartialsRule) |
| 7 | + |
| 8 | +describe("ERBNoInstanceVariablesInPartialsRule", () => { |
| 9 | + test("allows local variables in partials", () => { |
| 10 | + expectNoOffenses(dedent` |
| 11 | + <div> |
| 12 | + <%= post.title %> |
| 13 | + </div> |
| 14 | + `, { fileName: "_card.html.erb" }) |
| 15 | + }) |
| 16 | + |
| 17 | + test("allows local variables passed to partials", () => { |
| 18 | + expectNoOffenses(dedent` |
| 19 | + <div> |
| 20 | + <%= user.name %> |
| 21 | + <%= title %> |
| 22 | + </div> |
| 23 | + `, { fileName: "_user_card.html.erb" }) |
| 24 | + }) |
| 25 | + |
| 26 | + test("flags instance variables in partials", () => { |
| 27 | + expectError("Avoid using instance variables in partials. Pass `@post` as a local variable instead.", [2, 6]) |
| 28 | + |
| 29 | + assertOffenses(dedent` |
| 30 | + <div> |
| 31 | + <%= @post.title %> |
| 32 | + </div> |
| 33 | + `, { fileName: "_card.html.erb" }) |
| 34 | + }) |
| 35 | + |
| 36 | + test("flags multiple instance variables in partials", () => { |
| 37 | + expectError("Avoid using instance variables in partials. Pass `@post` as a local variable instead.", [2, 6]) |
| 38 | + expectError("Avoid using instance variables in partials. Pass `@user` as a local variable instead.", [3, 6]) |
| 39 | + |
| 40 | + assertOffenses(dedent` |
| 41 | + <div> |
| 42 | + <%= @post.title %> |
| 43 | + <%= @user.name %> |
| 44 | + </div> |
| 45 | + `, { fileName: "_card.html.erb" }) |
| 46 | + }) |
| 47 | + |
| 48 | + test("flags instance variables in silent ERB tags in partials", () => { |
| 49 | + expectError("Avoid using instance variables in partials. Pass `@posts` as a local variable instead.", [1, 3]) |
| 50 | + |
| 51 | + assertOffenses(dedent` |
| 52 | + <% @posts.each do |post| %> |
| 53 | + <div><%= post.title %></div> |
| 54 | + <% end %> |
| 55 | + `, { fileName: "_list.html.erb" }) |
| 56 | + }) |
| 57 | + |
| 58 | + test("does not flag instance variables in non-partial files", () => { |
| 59 | + expectNoOffenses(dedent` |
| 60 | + <div> |
| 61 | + <%= @post.title %> |
| 62 | + </div> |
| 63 | + `, { fileName: "show.html.erb" }) |
| 64 | + }) |
| 65 | + |
| 66 | + test("does not flag instance variables in layout files", () => { |
| 67 | + expectNoOffenses(dedent` |
| 68 | + <!DOCTYPE html> |
| 69 | + <html> |
| 70 | + <body><%= @content %></body> |
| 71 | + </html> |
| 72 | + `, { fileName: "application.html.erb" }) |
| 73 | + }) |
| 74 | + |
| 75 | + test("does not flag when filename is not provided", () => { |
| 76 | + expectNoOffenses(dedent` |
| 77 | + <div> |
| 78 | + <%= @post.title %> |
| 79 | + </div> |
| 80 | + `, { fileName: undefined }) |
| 81 | + }) |
| 82 | + |
| 83 | + test("flags instance variables in deeply nested partials path", () => { |
| 84 | + expectError("Avoid using instance variables in partials. Pass `@user` as a local variable instead.", [1, 4]) |
| 85 | + |
| 86 | + assertOffenses(dedent` |
| 87 | + <%= @user.name %> |
| 88 | + `, { fileName: "app/views/users/_profile.html.erb" }) |
| 89 | + }) |
| 90 | + |
| 91 | + test("does not flag class variables", () => { |
| 92 | + expectNoOffenses(dedent` |
| 93 | + <%= @@counter %> |
| 94 | + `, { fileName: "_card.html.erb" }) |
| 95 | + }) |
| 96 | + |
| 97 | + test("does not flag email addresses in text", () => { |
| 98 | + expectNoOffenses(dedent` |
| 99 | + <p>Contact us</p> |
| 100 | + `, { fileName: "_footer.html.erb" }) |
| 101 | + }) |
| 102 | + |
| 103 | + test("flags instance variable writes in partials", () => { |
| 104 | + expectError("Avoid setting instance variables in partials. Use a local variable instead of `@record`.", [1, 3]) |
| 105 | + |
| 106 | + assertOffenses(dedent` |
| 107 | + <% @record = "hello" %> |
| 108 | + `, { fileName: "_form.html.erb" }) |
| 109 | + }) |
| 110 | + |
| 111 | + test("flags instance variables in if conditions", () => { |
| 112 | + expectError("Avoid using instance variables in partials. Pass `@show` as a local variable instead.", [1, 6]) |
| 113 | + |
| 114 | + assertOffenses(dedent` |
| 115 | + <% if @show %> |
| 116 | + <p>Visible</p> |
| 117 | + <% end %> |
| 118 | + `, { fileName: "_toggle.html.erb" }) |
| 119 | + }) |
| 120 | +}) |
0 commit comments