Skip to content

Commit

Permalink
Add severity setting to ERBLint rules (#1634)
Browse files Browse the repository at this point in the history
Co-authored-by: Cameron Dutro <[email protected]>
  • Loading branch information
mxriverlynn and camertron authored Nov 23, 2022
1 parent f99c61b commit cdc13a1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-items-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

Adding a custom erblint schema to allow `severity` in linter configuration
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require_relative "helpers/deprecated_components_helpers"
require_relative "severity_schema"

require "erblint-github/linters/custom_helpers"

module ERBLint
Expand All @@ -11,6 +13,8 @@ class DeprecatedComponentsCounter < Linter
include ERBLint::LinterRegistry
include Helpers::DeprecatedComponentsHelpers

self.config_schema = SeveritySchema

def run(processed_source)
processed_source.ast.descendants(:erb).each do |erb_node|
_, _, code_node = *erb_node
Expand All @@ -21,10 +25,7 @@ def run(processed_source)
deprecated_components.each do |component|
next unless code.include?(component)

add_offense(
erb_node.loc,
message(component)
)
add_offense(erb_node.loc, message(component))
end
end

Expand All @@ -44,6 +45,16 @@ def autocorrect(processed_source, offense)
end
end
end

# this override is necessary because of the github/erblint-github `CustomHelpers`
# module. `counter_correct?` is provided by this module, and calls `add_offense`
# directly. there is no simple way to modify this without updating the gem and
# creating what would likely be an API that is non-standard and/or difficult to use
#
# https://github.com/github/erblint-github/blob/main/lib/erblint-github/linters/custom_helpers.rb
def add_offense(source_range, message, context = nil, severity = nil)
super(source_range, message, context, severity || @config.severity)
end
end
end
end
14 changes: 14 additions & 0 deletions lib/primer/view_components/linters/severity_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require "erb_lint/utils/severity_levels"

module ERBLint
module Linters
class SeveritySchema < LinterConfig
# SEVERITY_NAMES :info, :refactor, :convention, :warning, :error, :fatal
# see https://github.com/Shopify/erb-lint/blob/main/lib/erb_lint/utils/severity_levels.rb

property :severity, accepts: ERBLint::Utils::SeverityLevels::SEVERITY_NAMES, default: nil, reader: :severity
end
end
end
21 changes: 21 additions & 0 deletions test/lib/erblint/deprecated_components_counter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
require "lib/erblint_test_case"

class DeprecatedComponentsCounterTest < ErblintTestCase
def test_default_severity_level
@file = <<~ERB
<%= render Primer::BlankslateComponent.new %>
ERB
@linter.run(processed_source)

assert_nil @linter.offenses[0].severity
assert_nil @linter.offenses[1].severity
end

def test_setting_severity_level
@file = <<~ERB
<%= render Primer::BlankslateComponent.new %>
ERB
linter = linter_with_severity(:info)
linter.run(processed_source)

assert_equal :info, linter.offenses[0].severity
assert_equal :info, linter.offenses[1].severity
end

def test_warns_about_deprecated_primer_component
@file = <<~ERB
<%= render Primer::BlankslateComponent.new %>
Expand Down
7 changes: 7 additions & 0 deletions test/lib/erblint_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ def corrected_content
end
end

def linter_with_severity(severity)
return unless linter_class

config_with_severity = linter_class.config_schema.new({ severity: severity })
linter_class.new(file_loader, config_with_severity)
end

def linter_with_override
linter_class&.new(file_loader, linter_class.config_schema.new(override_ignores_if_correctable: true))
end
Expand Down

0 comments on commit cdc13a1

Please sign in to comment.