Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add severity level support for all linters #349

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ linters:
- '**/local-lib/**/*'
```

## Severity

You can specify the severity of each linter. The severity can be `info`, `refactor`, `convention`, `warning`, `error` or `fatal`. By default the severity of all linters is `error`.

Linters with severity less than `error` will not be included in the total count of errors and will not cause the command to exit with a non-zero status.

```yaml
---
linters:
ErbSafety:
enabled: true
severity: info
```

You can set a custom severity for the command running `erblint` with the `--fail-level` option, this will set the minimum severity level to consider when exiting with a non-zero status.

```sh
# will exit with a non-zero status if there are any linters with
# severity warning or greater
erblint foo.erb --fail-level W
```

## Linters

| Available Linters | Default | Description |
Expand Down
11 changes: 10 additions & 1 deletion lib/erb_lint/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,16 @@ def option_parser
@options[:enabled_linters] = linters
end

opts.on("--fail-level SEVERITY", "Minimum severity for exit with error code") do |level|
opts.on(
"--fail-level SEVERITY",
"Minimum severity for exit with error code.",
" [I] info",
" [R] refactor",
" [C] convention",
" [W] warning",
" [E] error",
" [F] fatal",
) do |level|
parsed_severity = SEVERITY_CODE_TABLE[level.upcase.to_sym] || (SEVERITY_NAMES & [level.downcase]).first

if parsed_severity.nil?
Expand Down
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
Loading