Skip to content

Commit

Permalink
add severity level support for all linters
Browse files Browse the repository at this point in the history
  • Loading branch information
domingo2000 committed May 10, 2024
1 parent f31f100 commit a2fa4cb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
6 changes: 6 additions & 0 deletions lib/erb_lint/linter_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "active_support"
require "smart_properties"
require "erb_lint/utils/severity_levels"

module ERBLint
class LinterConfig
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/erb_lint/offense.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 35 additions & 1 deletion spec/erb_lint/linter_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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/**/*"] } }
Expand Down
2 changes: 1 addition & 1 deletion spec/erb_lint/runner_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit a2fa4cb

Please sign in to comment.