From 7c37134f0ec72f2c628ddc8c94e8a5b730a9ee6c Mon Sep 17 00:00:00 2001 From: domingo2000 Date: Fri, 10 May 2024 14:55:05 -0400 Subject: [PATCH] add severity level support for all linters --- lib/erb_lint/linter_config.rb | 6 +++++ lib/erb_lint/offense.rb | 2 +- spec/erb_lint/linter_config_spec.rb | 36 ++++++++++++++++++++++++++++- spec/erb_lint/runner_config_spec.rb | 2 +- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/lib/erb_lint/linter_config.rb b/lib/erb_lint/linter_config.rb index 1e5c3d8e..0c91ee17 100644 --- a/lib/erb_lint/linter_config.rb +++ b/lib/erb_lint/linter_config.rb @@ -2,6 +2,7 @@ require "active_support" require "smart_properties" +require "erb_lint/utils/severity_levels" module ERBLint class LinterConfig @@ -21,6 +22,11 @@ def to_array_of(klass) property :enabled, accepts: [true, false], default: false, reader: :enabled? property :exclude, accepts: array_of?(String), default: -> { [] } + property :severity, + accepts: ERBLint::Utils::SeverityLevels::SEVERITY_NAMES, + default: :error, + reader: :severity, + converts: lambda { |value| value.to_sym } def initialize(config = {}) config = config.dup.deep_stringify_keys diff --git a/lib/erb_lint/offense.rb b/lib/erb_lint/offense.rb index 07e73bfd..07f7983d 100644 --- a/lib/erb_lint/offense.rb +++ b/lib/erb_lint/offense.rb @@ -14,7 +14,7 @@ def initialize(linter, source_range, message, context = nil, severity = nil) @source_range = source_range @message = message @context = context - @severity = severity + @severity = severity || linter.config.severity @disabled = false end diff --git a/spec/erb_lint/linter_config_spec.rb b/spec/erb_lint/linter_config_spec.rb index 52f4c673..c0b06178 100644 --- a/spec/erb_lint/linter_config_spec.rb +++ b/spec/erb_lint/linter_config_spec.rb @@ -13,7 +13,7 @@ let(:config_hash) { {} } it "returns default config" do - expect(subject).to(eq("enabled" => false, "exclude" => [])) + expect(subject).to(eq("enabled" => false, "exclude" => [], "severity" => :error)) end end @@ -97,6 +97,40 @@ class CustomConfig < described_class end end + describe "#severity" do + subject { linter_config.severity } + + context "when no severity is set" do + let(:config_hash) { {} } + it { expect(subject).to(eq(:error)) } + end + + describe "when severity is a valid value" do + valid_severities = ERBLint::Utils::SeverityLevels::SEVERITY_NAMES + + valid_severities.each do |severity| + context "when severity is #{severity}" do + let(:config_hash) { { severity: severity } } + it { expect(subject).to(eq(severity)) } + end + end + end + + context "when severity is an invalid value" do + let(:config_hash) { { severity: "bogus" } } + + it do + expect { subject }.to( + raise_error( + described_class::Error, + "ERBLint::LinterConfig does not accept :bogus as value for the property severity. Only accepts: \ +[:info, :refactor, :convention, :warning, :error, :fatal]", + ), + ) + end + end + end + describe "#excludes_file?" do context "when glob matches" do let(:config_hash) { { exclude: ["vendor/**/*"] } } diff --git a/spec/erb_lint/runner_config_spec.rb b/spec/erb_lint/runner_config_spec.rb index 91bba348..1efc085f 100644 --- a/spec/erb_lint/runner_config_spec.rb +++ b/spec/erb_lint/runner_config_spec.rb @@ -126,7 +126,7 @@ class MySchema < ERBLint::LinterConfig it { expect(subject.class).to(eq(ERBLint::Linters::FinalNewline::ConfigSchema)) } it "fills linter config with defaults from schema" do - expect(subject.to_hash).to(eq("enabled" => false, "exclude" => [], "present" => true)) + expect(subject.to_hash).to(eq("enabled" => false, "exclude" => [], "present" => true, "severity" => :error)) end it "is disabled by default" do expect(subject.enabled?).to(eq(false))