|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | + |
| 5 | +describe ERBLint::Linters::StrictLocals do |
| 6 | + let(:linter_config) { described_class.config_schema.new } |
| 7 | + let(:file_loader) { ERBLint::FileLoader.new(".") } |
| 8 | + let(:linter) { described_class.new(file_loader, linter_config) } |
| 9 | + let(:processed_source) { ERBLint::ProcessedSource.new("file.html.erb", file) } |
| 10 | + |
| 11 | + subject { linter.offenses } |
| 12 | + before { linter.run(processed_source) } |
| 13 | + |
| 14 | + context "when the ERB contains a strict locals declaration at the top of the file" do |
| 15 | + let(:file) { <<~FILE } |
| 16 | + <%# locals: (foo: "bar") %> |
| 17 | + <div> |
| 18 | + <%= foo %> |
| 19 | + </div> |
| 20 | + FILE |
| 21 | + |
| 22 | + it "does not report any offenses" do |
| 23 | + expect(subject.size).to(eq(0)) |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + context "when the ERB contains a strict locals declaration anywhere else in the file" do |
| 28 | + let(:file) { <<~FILE } |
| 29 | + <div> |
| 30 | + <%= foo %> |
| 31 | + </div> |
| 32 | + <%# locals: (foo: "bar") %> |
| 33 | + FILE |
| 34 | + |
| 35 | + it "does not report any offenses" do |
| 36 | + expect(subject.size).to(eq(0)) |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + context "when the ERB contains an empty strict locals declaration" do |
| 41 | + let(:file) { <<~FILE } |
| 42 | + <%# locals: () %> |
| 43 | + <div> |
| 44 | + <%= foo %> |
| 45 | + </div> |
| 46 | + FILE |
| 47 | + |
| 48 | + it "does not report any offenses" do |
| 49 | + expect(subject.size).to(eq(0)) |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + context "when the ERB does not contain a strict locals declaration" do |
| 54 | + let(:file) { <<~FILE } |
| 55 | + <div> |
| 56 | + <%= foo %> |
| 57 | + </div> |
| 58 | + FILE |
| 59 | + let(:corrector) { ERBLint::Corrector.new(processed_source, subject) } |
| 60 | + let(:corrected_content) { corrector.corrected_content } |
| 61 | + |
| 62 | + it "reports an offense" do |
| 63 | + expect(subject.size).to(eq(1)) |
| 64 | + end |
| 65 | + |
| 66 | + it "reports the suggested fix" do |
| 67 | + expect(subject.first.message).to(include( |
| 68 | + "Missing strict locals declaration.\n", |
| 69 | + "Add <%# locals: () %> at the top of the file to enforce strict locals.", |
| 70 | + )) |
| 71 | + end |
| 72 | + |
| 73 | + it "corrects the file" do |
| 74 | + expect(corrected_content).to(eq("<%# locals: () %>\n<div>\n <%= foo %>\n</div>\n")) |
| 75 | + end |
| 76 | + end |
| 77 | +end |
0 commit comments