|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | +require "spec_utils" |
| 5 | + |
| 6 | +describe ERBLint::Linters::NoUnusedDisable do |
| 7 | + let(:linter_config) { described_class.config_schema.new } |
| 8 | + |
| 9 | + let(:file_loader) { ERBLint::FileLoader.new(".") } |
| 10 | + let(:linter) { described_class.new(file_loader, linter_config) } |
| 11 | + let(:processed_source) { ERBLint::ProcessedSource.new("file.rb", file) } |
| 12 | + let(:offenses) { linter.offenses } |
| 13 | + |
| 14 | + module ERBLint |
| 15 | + module Linters |
| 16 | + class Fake < ERBLint::Linter |
| 17 | + attr_accessor :offenses |
| 18 | + end |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + describe "offenses" do |
| 23 | + subject { offenses } |
| 24 | + context "when file has unused disable comment" do |
| 25 | + let(:file) { "<span></span><%# erblint:disable Fake %>" } |
| 26 | + before { linter.run(processed_source, []) } |
| 27 | + it do |
| 28 | + expect(subject.size).to(eq(1)) |
| 29 | + expect(subject.first.message).to(eq("Unused erblint:disable comment for Fake")) |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + context "when file has a disable comment and a corresponding offense" do |
| 34 | + let(:file) { "<span></span><%# erblint:disable Fake %>" } |
| 35 | + before do |
| 36 | + offense = ERBLint::Offense.new(ERBLint::Linters::Fake.new(file_loader, linter_config), |
| 37 | + SpecUtils.source_range_for_code(processed_source, "<span></span>"), |
| 38 | + "some fake linter message") |
| 39 | + offense.disabled = true |
| 40 | + linter.run(processed_source, [offense]) |
| 41 | + end |
| 42 | + |
| 43 | + it "does not report anything" do |
| 44 | + expect(subject.size).to(eq(0)) |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + context "when file has a disable comment in wrong place and a corresponding offense" do |
| 49 | + let(:file) { <<~FILE } |
| 50 | + <%# erblint:disable Fake %> |
| 51 | + <span>bad content</span> |
| 52 | + FILE |
| 53 | + before do |
| 54 | + offense = ERBLint::Offense.new( |
| 55 | + ERBLint::Linters::Fake.new(file_loader, linter_config), |
| 56 | + SpecUtils.source_range_for_code(processed_source, "<span>bad content</span>"), |
| 57 | + "some fake linter message" |
| 58 | + ) |
| 59 | + offense.disabled = true |
| 60 | + linter.run(processed_source, [offense]) |
| 61 | + end |
| 62 | + |
| 63 | + it "reports the unused inline comment" do |
| 64 | + expect(subject.size).to(eq(1)) |
| 65 | + expect(subject.first.message).to(eq("Unused erblint:disable comment for Fake")) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + context "when file has disable comment for multiple rules" do |
| 70 | + let(:file) { "<span></span><%# erblint:disable Fake, Fake2 %>" } |
| 71 | + before do |
| 72 | + offense = ERBLint::Offense.new( |
| 73 | + ERBLint::Linters::Fake.new(file_loader, linter_config), |
| 74 | + SpecUtils.source_range_for_code(processed_source, "<span></span>"), |
| 75 | + "some fake linter message" |
| 76 | + ) |
| 77 | + offense.disabled = true |
| 78 | + linter.run(processed_source, [offense]) |
| 79 | + end |
| 80 | + |
| 81 | + it "reports the unused inline comment" do |
| 82 | + expect(subject.size).to(eq(1)) |
| 83 | + expect(subject.first.message).to(eq("Unused erblint:disable comment for Fake2")) |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + context "when file has multiple disable comments for one offense" do |
| 88 | + let(:file) { <<~ERB } |
| 89 | + <%# erblint:disable Fake %> |
| 90 | + <span></span><%# erblint:disable Fake %> |
| 91 | + ERB |
| 92 | + before do |
| 93 | + offense = ERBLint::Offense.new( |
| 94 | + ERBLint::Linters::Fake.new(file_loader, linter_config), |
| 95 | + SpecUtils.source_range_for_code(processed_source, "<span></span>"), |
| 96 | + "some fake linter message" |
| 97 | + ) |
| 98 | + offense.disabled = true |
| 99 | + linter.run(processed_source, [offense]) |
| 100 | + end |
| 101 | + |
| 102 | + it "reports the unused inline comment" do |
| 103 | + expect(subject.size).to(eq(1)) |
| 104 | + expect(subject.first.message).to(eq("Unused erblint:disable comment for Fake")) |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | +end |
0 commit comments