Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: remove counter-related code. #75

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

Merged
merged 3 commits into from
Jun 9, 2023
Merged
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
28 changes: 0 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,34 +73,6 @@ linters:
- [GitHub::Accessibility::NoTitleAttribute](./docs/rules/accessibility/no-title-attribute.md)
- [GitHub::Accessibility::SvgHasAccessibleText](./docs/rules/accessibility/svg-has-accessible-text.md)

## Disabling a rule (Deprecated)

_This is a soon-to-be deprecated feature. Do not use. See [migration guide](./docs/counter-migration-guide.md)_

`erblint` does not natively support rule disables. At GitHub, we've implemented these rules in a way to allow rules to be disabled at an offense-level via counters or disabled at a file-level because often times, we want to enable a rule but aren't able to address all offenses at once. We achieve this in one of two ways.

Rules that are marked as `Counter` can be disabled by adding a comment with the offense count that matches the number of offenses within the file like:

```.html.erb
<%# erblint:counter GitHub::Accessibility::LinkHasHrefCounter 1 %>
```

In this comment example, when a new `LinkHasHref` offense has been added, the counter will need to be bumped up to 2. More recent rules use a `Counter` format.

If you are enabling a rule for the first time and your codebase has a lot of offenses, you can use the `-a` command to automatically add these counter comments in the appropriate places.

```
bundle exec erblint app/views app/components -a
```

Rules that are not marked as `Counter` like `NoRedundantImageAlt` are considered to be legacy format. We are in the process of migrating these to counters. These rules can still be disabled at the file-level by adding this comment at the top of the file:

```.html.erb
<%# erblint:disable GitHub::Accessibility::NoRedundantImageAlt %>
```

However, unlike a counter, any subsequent offenses introduced to the file will not raise.

## Testing

```
Expand Down
2 changes: 2 additions & 0 deletions docs/counter-migration-guide.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Counter migration guide

This is relevant for `erblint-github` versions <= 0.3.2.

[ERBLint v0.4.0](https://github.com/Shopify/erb-lint/releases/tag/v0.4.0) introduces support for inline lint rule disables.

Since an inline disable feature is now natively available, it is time to move away from the in-house (hacky) counter system we've used internally over the years and in this library. 🎉
Expand Down
33 changes: 0 additions & 33 deletions lib/erblint-github/linters/custom_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,6 @@ module Linters
module CustomHelpers
INTERACTIVE_ELEMENTS = %w[button summary input select textarea a].freeze

def counter_correct?(processed_source)
comment_node = nil
expected_count = 0
rule_name = simple_class_name
offenses_count = @offenses.length

processed_source.parser.ast.descendants(:erb).each do |node|
indicator_node, _, code_node, = *node
indicator = indicator_node&.loc&.source
comment = code_node&.loc&.source&.strip

if indicator == "#" && comment.start_with?("erblint:counter") && comment.match(rule_name)
comment_node = node
expected_count = comment.match(/\s(\d+)\s?$/)[1].to_i
end
end

if offenses_count.zero?
# have to adjust to get `\n` so we delete the whole line
add_offense(processed_source.to_source_range(comment_node.loc.adjust(end_pos: 1)), "Unused erblint:counter comment for #{rule_name}", "") if comment_node
return
end

first_offense = @offenses[0]

if comment_node.nil?
add_offense(processed_source.to_source_range(first_offense.source_range), "#{rule_name}: If you must, add <%# erblint:disable #{rule_name} %> at the end of the offending line to bypass this check. See https://github.com/shopify/erb-lint#disable-rule-at-offense-level", "<%# erblint:disable #{rule_name} %>")
else
clear_offenses
add_offense(processed_source.to_source_range(comment_node.loc), "Incorrect erblint:counter number for #{rule_name}. Expected: #{expected_count}, actual: #{offenses_count}.", "<%# erblint:counter #{rule_name}Counter #{offenses_count} %>") if expected_count != offenses_count
end
end

def generate_offense(klass, processed_source, tag, message = nil, replacement = nil)
message ||= klass::MESSAGE
message += "\nLearn more at https://github.com/github/erblint-github#rules.\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ class AvoidBothDisabledAndAriaDisabled < Linter
ELEMENTS_WITH_NATIVE_DISABLED_ATTRIBUTE_SUPPORT = %w[button fieldset input optgroup option select textarea].freeze
MESSAGE = "[aria-disabled] may be used in place of native HTML [disabled] to allow tab-focus on an otherwise ignored element. Setting both attributes is contradictory."

class ConfigSchema < LinterConfig
property :counter_enabled, accepts: [true, false], default: false, reader: :counter_enabled?
end
self.config_schema = ConfigSchema

def run(processed_source)
tags(processed_source).each do |tag|
next if tag.closing?
Expand All @@ -26,10 +21,6 @@ def run(processed_source)

generate_offense(self.class, processed_source, tag)
end

if @config.counter_enabled?
counter_correct?(processed_source)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ class AvoidGenericLinkText < Linter

MESSAGE = "Avoid using generic link text such as #{BANNED_GENERIC_TEXT.join(', ')} which do not make sense in isolation."

class ConfigSchema < LinterConfig
property :counter_enabled, accepts: [true, false], default: false, reader: :counter_enabled?
end
self.config_schema = ConfigSchema

def run(processed_source)
processed_source.ast.children.each_with_index do |node, index|
next unless node.methods.include?(:type) && node.type == :text
Expand Down Expand Up @@ -98,9 +93,6 @@ def run(processed_source)
banned_text = nil
end
end
if @config.counter_enabled?
counter_correct?(processed_source)
end
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@ class DisabledAttribute < Linter
VALID_DISABLED_TAGS = %w[button input textarea option select fieldset optgroup task-lists].freeze
MESSAGE = "`disabled` is only valid on #{VALID_DISABLED_TAGS.join(', ')}."

class ConfigSchema < LinterConfig
property :counter_enabled, accepts: [true, false], default: false, reader: :counter_enabled?
end
self.config_schema = ConfigSchema

class ConfigSchema < LinterConfig
property :counter_enabled, accepts: [true, false], default: false, reader: :counter_enabled?
end
self.config_schema = ConfigSchema

def run(processed_source)
tags(processed_source).each do |tag|
next if tag.closing?
Expand All @@ -31,10 +21,6 @@ def run(processed_source)

generate_offense(self.class, processed_source, tag)
end

if @config.counter_enabled?
counter_correct?(processed_source)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ class IframeHasTitle < Linter
MESSAGE = "`<iframe>` with meaningful content should have a title attribute that identifies the content."\
" If `<iframe>` has no meaningful content, hide it from assistive technology with `aria-hidden='true'`."\

class ConfigSchema < LinterConfig
property :counter_enabled, accepts: [true, false], default: false, reader: :counter_enabled?
end
self.config_schema = ConfigSchema

def run(processed_source)
tags(processed_source).each do |tag|
next if tag.name != "iframe"
Expand All @@ -27,10 +22,6 @@ def run(processed_source)

generate_offense(self.class, processed_source, tag) if title.empty? && !aria_hidden?(tag)
end

if @config.counter_enabled?
counter_correct?(processed_source)
end
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ class ImageHasAlt < Linter

MESSAGE = "<img> should have an alt prop with meaningful text or an empty string for decorative images"

class ConfigSchema < LinterConfig
property :counter_enabled, accepts: [true, false], default: false, reader: :counter_enabled?
end
self.config_schema = ConfigSchema

def run(processed_source)
tags(processed_source).each do |tag|
next if tag.name != "img"
Expand All @@ -26,10 +21,6 @@ def run(processed_source)

generate_offense(self.class, processed_source, tag) if alt.empty?
end

if @config.counter_enabled?
counter_correct?(processed_source)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ class LinkHasHref < Linter

MESSAGE = "Links should go somewhere, you probably want to use a `<button>` instead."

class ConfigSchema < LinterConfig
property :counter_enabled, accepts: [true, false], default: false, reader: :counter_enabled?
end
self.config_schema = ConfigSchema

def run(processed_source)
tags(processed_source).each do |tag|
next if tag.name != "a"
Expand All @@ -26,10 +21,6 @@ def run(processed_source)
name = tag.attributes["name"]
generate_offense(self.class, processed_source, tag) if (!name && href.empty?) || href.include?("#")
end

if @config.counter_enabled?
counter_correct?(processed_source)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ class NestedInteractiveElements < Linter

MESSAGE = "Nesting interactive elements produces invalid HTML, and ssistive technologies, such as screen readers, might ignore or respond unexpectedly to such nested controls."

class ConfigSchema < LinterConfig
property :counter_enabled, accepts: [true, false], default: false, reader: :counter_enabled?
end
self.config_schema = ConfigSchema
def run(processed_source)
last_interactive_element = nil
tags(processed_source).each do |tag|
Expand All @@ -34,10 +30,6 @@ def run(processed_source)

last_interactive_element = tag unless tag&.name == "input"
end

if @config.counter_enabled?
counter_correct?(processed_source)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,11 @@ class NoAriaHiddenOnFocusable < Linter

MESSAGE = "Elements that are focusable should not have `aria-hidden='true' because it will cause confusion for assistive technology users."

class ConfigSchema < LinterConfig
property :counter_enabled, accepts: [true, false], default: false, reader: :counter_enabled?
end
self.config_schema = ConfigSchema

def run(processed_source)
tags(processed_source).each do |tag|
aria_hidden = possible_attribute_values(tag, "aria-hidden")
generate_offense(self.class, processed_source, tag) if aria_hidden.include?("true") && focusable?(tag)
end

if @config.counter_enabled?
counter_correct?(processed_source)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ class NoAriaLabelMisuse < Linter

MESSAGE = "[aria-label] and [aria-labelledby] usage are only reliably supported on interactive elements and a subset of ARIA roles"

class ConfigSchema < LinterConfig
property :counter_enabled, accepts: [true, false], default: false, reader: :counter_enabled?
end
self.config_schema = ConfigSchema

def run(processed_source)
tags(processed_source).each do |tag|
next if tag.closing?
Expand All @@ -39,9 +34,6 @@ def run(processed_source)
end
end
end
if @config.counter_enabled?
counter_correct?(processed_source)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,13 @@ class NoPositiveTabIndex < Linter

MESSAGE = "Do not use positive tabindex as it is error prone and can severely disrupt navigation experience for keyboard users"

class ConfigSchema < LinterConfig
property :counter_enabled, accepts: [true, false], default: false, reader: :counter_enabled?
end
self.config_schema = ConfigSchema

def run(processed_source)
tags(processed_source).each do |tag|
next if tag.closing?
next unless tag.attributes["tabindex"]&.value.to_i.positive?

generate_offense(self.class, processed_source, tag)
end

if @config.counter_enabled?
counter_correct?(processed_source)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ class NoRedundantImageAlt < Linter
MESSAGE = "<img> alt prop should not contain `image` or `picture` as screen readers already announce the element as an image"
REDUNDANT_ALT_WORDS = %w[image picture].freeze

class ConfigSchema < LinterConfig
property :counter_enabled, accepts: [true, false], default: false, reader: :counter_enabled?
end
self.config_schema = ConfigSchema

def run(processed_source)
tags(processed_source).each do |tag|
next if tag.name != "img"
Expand All @@ -28,10 +23,6 @@ def run(processed_source)

generate_offense(self.class, processed_source, tag) if (alt.downcase.split & REDUNDANT_ALT_WORDS).any?
end

if @config.counter_enabled?
counter_correct?(processed_source)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ class NoTitleAttribute < Linter

MESSAGE = "The title attribute should never be used unless for an `<iframe>` as it is inaccessible for several groups of users."

class ConfigSchema < LinterConfig
property :counter_enabled, accepts: [true, false], default: false, reader: :counter_enabled?
end
self.config_schema = ConfigSchema

def run(processed_source)
tags(processed_source).each do |tag|
next if tag.name == "iframe"
Expand All @@ -25,10 +20,6 @@ def run(processed_source)
title = possible_attribute_values(tag, "title")
generate_offense(self.class, processed_source, tag) if title.present?
end

if @config.counter_enabled?
counter_correct?(processed_source)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ class SvgHasAccessibleText < Linter

MESSAGE = "`<svg>` must have accessible text. Set `aria-label`, or `aria-labelledby`, or nest a `<title>` element. However, if the `<svg>` is purely decorative, hide it with `aria-hidden='true'.\nFor more info, see https://css-tricks.com/accessible-svgs/."

class ConfigSchema < LinterConfig
property :counter_enabled, accepts: [true, false], default: false, reader: :counter_enabled?
end
self.config_schema = ConfigSchema

def run(processed_source)
current_svg = nil
has_accessible_label = false
Expand All @@ -41,10 +36,6 @@ def run(processed_source)
has_accessible_label = aria_label.present? || aria_labelledby.present?
end
end

if @config.counter_enabled?
counter_correct?(processed_source)
end
end
end
end
Expand Down
Loading