Skip to content

Commit dee42cf

Browse files
authored
Merge pull request #57 from github/kh-rename-from-counter
Start migrating rules away from counter system
2 parents 85c6bb3 + 868855d commit dee42cf

File tree

5 files changed

+84
-40
lines changed

5 files changed

+84
-40
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ linters:
5555
5656
## Rules
5757
58-
- [GitHub::Accessibility::AvoidBothDisabledAndAriaDisabledCounter](./docs/rules/accessibility/avoid-both-disabled-and-aria-disabled-counter.md)
58+
- [GitHub::Accessibility::AvoidBothDisabledAndAriaDisabledCounter](./docs/rules/accessibility/avoid-both-disabled-and-aria-disabled.md)
5959
- [GitHub::Accessibility::AvoidGenericLinkTextCounter](./docs/rules/accessibility/avoid-generic-link-text-counter.md)
6060
- [GitHub::Accessibility::DisabledAttributeCounter](./docs/rules/accessibility/disabled-attribute-counter.md)
6161
- [GitHub::Accessibility::LandmarkHasLabelCounter](./docs/rules/accessibility/landmark-has-label-counter.md)

docs/counter-migration-guide.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Counter migration guide
2+
3+
[ERBLint v0.4.0](https://github.com/Shopify/erb-lint/releases/tag/v0.4.0) introduces support for inline lint rule disables.
4+
5+
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. 🎉
6+
7+
Our latest `erblint-github` release removes the `-Counter` prefix from all of the lint rules. Please update your `.erb-lint.yml` accordingly.
8+
9+
If your configuration looks something like:
10+
11+
```yaml
12+
---
13+
linters:
14+
GitHub::Accessibility::AvoidBothDisabledAndAriaCounter:
15+
enabled: true
16+
GitHub::Accessibility::AvoidGenericLinkTextCounter:
17+
enabled: true
18+
GitHub::Accessibility::DisabledAttributeCounter:
19+
enabled: true
20+
```
21+
22+
It should become the following, with `-Counter` removed to make sure the rules run correctly.
23+
24+
```yaml
25+
---
26+
linters:
27+
GitHub::Accessibility::AvoidBothDisabledAndAria:
28+
enabled: true
29+
GitHub::Accessibility::AvoidGenericLinkText:
30+
enabled: true
31+
GitHub::Accessibility::DisabledAttribute:
32+
enabled: true
33+
```
34+
35+
## Easing migration
36+
37+
In order to ease migration for codebases where counter comments are in place (especially large codebases), we will continue to support counters for existing lint rules for a few releases until we deprecate it completely. This should allow you to migrate rules one-by-one, rather than all at once.
38+
39+
With this release, the counter system will now be toggled off by default, so please explicitly enable them in your `.erb-lint.yml` config if you would like to enable counters.
40+
41+
```yaml
42+
---
43+
linters:
44+
GitHub::Accessibility::AvoidBothDisabledAndAria:
45+
enabled: true
46+
counter_enabled: true
47+
GitHub::Accessibility::AvoidGenericLinkText:
48+
enabled: true
49+
counter_enabled: true
50+
```
51+
52+
With this `counter_enabled: true` config, your counter comments like `<%# erblint:counter GitHub::Accessibility::AvoidBothDisabledAndAriaCounter 1` should work as it did before.
53+
54+
However, we will drop support for the counter system within the next few releases so please take time to migrate your counter comments to native inline disable comments. Any new rules added to this library will not support the counter system.
55+
56+
Once your files do not have any counter comments, remove the `counter_enabled: true` from your configuration to ensure no new ones are added.
57+
58+
## Automate migration
59+
60+
Adding inline disables for large codebases can be extremely tedious. We recommend using a script to automate this process.
61+
62+
<!-- TODO: Add script that consumers can use -->

docs/rules/accessibility/avoid-both-disabled-and-aria-disabled-counter.md renamed to docs/rules/accessibility/avoid-both-disabled-and-aria-disabled.md

File renamed without changes.

lib/erblint-github/linters/github/accessibility/avoid_both_disabled_and_aria_disabled_counter.rb renamed to lib/erblint-github/linters/github/accessibility/avoid_both_disabled_and_aria_disabled.rb

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@ module ERBLint
66
module Linters
77
module GitHub
88
module Accessibility
9-
class AvoidBothDisabledAndAriaDisabledCounter < Linter
9+
class AvoidBothDisabledAndAriaDisabled < Linter
1010
include ERBLint::Linters::CustomHelpers
1111
include LinterRegistry
1212

1313
ELEMENTS_WITH_NATIVE_DISABLED_ATTRIBUTE_SUPPORT = %w[button fieldset input optgroup option select textarea].freeze
1414
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."
1515

16+
class ConfigSchema < LinterConfig
17+
property :counter_enabled, accepts: [true, false], default: false, reader: :counter_enabled?
18+
end
19+
self.config_schema = ConfigSchema
20+
1621
def run(processed_source)
1722
tags(processed_source).each do |tag|
1823
next if tag.closing?
@@ -22,20 +27,8 @@ def run(processed_source)
2227
generate_offense(self.class, processed_source, tag)
2328
end
2429

25-
counter_correct?(processed_source)
26-
end
27-
28-
def autocorrect(processed_source, offense)
29-
return unless offense.context
30-
31-
lambda do |corrector|
32-
if processed_source.file_content.include?("erblint:counter #{simple_class_name}")
33-
# update the counter if exists
34-
corrector.replace(offense.source_range, offense.context)
35-
else
36-
# add comment with counter if none
37-
corrector.insert_before(processed_source.source_buffer.source_range, "#{offense.context}\n")
38-
end
30+
if @config.counter_enabled?
31+
counter_correct?(processed_source)
3932
end
4033
end
4134
end

test/linters/accessibility/avoid_both_disabled_and_aria_disabled_counter_test.rb renamed to test/linters/accessibility/avoid_both_disabled_and_aria_disabled_test.rb

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class AvoidBothDisabledAndAriaDisabled < LinterTestCase
66
def linter_class
7-
ERBLint::Linters::GitHub::Accessibility::AvoidBothDisabledAndAriaDisabledCounter
7+
ERBLint::Linters::GitHub::Accessibility::AvoidBothDisabledAndAriaDisabled
88
end
99

1010
ELEMENTS_WITH_NATIVE_DISABLED_ATTRIBUTE_SUPPORT = %w[button fieldset input optgroup option select textarea].freeze
@@ -15,7 +15,7 @@ def test_warns_if_both_disabled_and_aria_disabled_set_on_html_elements_with_disa
1515
end.join
1616
@linter.run(processed_source)
1717

18-
assert_equal @linter.offenses.count, 8
18+
assert_equal @linter.offenses.count, 7
1919
end
2020

2121
def test_does_not_warn_if_only_disabled_attribute_is_set
@@ -36,35 +36,24 @@ def test_does_not_warn_if_only_aria_disabled_attribute_is_set
3636
assert_empty @linter.offenses
3737
end
3838

39-
def test_does_not_raise_when_ignore_comment_with_correct_count
40-
@file = <<~ERB
41-
<%# erblint:counter GitHub::Accessibility::AvoidBothDisabledAndAriaDisabledCounter 1 %>
42-
<button disabled aria-disabled="true">Some text</span>
43-
ERB
44-
39+
def test_adds_extra_offense_to_add_counter_comment_if_counter_config_enabled
40+
@file = ELEMENTS_WITH_NATIVE_DISABLED_ATTRIBUTE_SUPPORT.map do |element|
41+
"<#{element} aria-disabled='true' disabled> </#{element}>"
42+
end.join
43+
@linter.config.counter_enabled = true
4544
@linter.run(processed_source)
46-
assert_empty @linter.offenses
47-
end
4845

49-
def test_does_not_autocorrect_when_ignores_are_correct
50-
@file = <<~ERB
51-
<%# erblint:counter GitHub::Accessibility::AvoidBothDisabledAndAriaDisabledCounter 1 %>
52-
<button disabled aria-disabled="true">Some text</button>
53-
ERB
54-
55-
assert_equal @file, corrected_content
46+
assert_equal @linter.offenses.count, 8
47+
assert_match(/If you must, add <%# erblint:counter GitHub::Accessibility::AvoidBothDisabledAndAriaDisabled 7 %> to bypass this check/, @linter.offenses.last.message)
5648
end
5749

58-
def test_does_autocorrect_when_ignores_are_not_correct
50+
def test_does_not_raise_when_ignore_comment_with_correct_count_if_counter_enabled
5951
@file = <<~ERB
60-
<button disabled aria-disabled="true">Some text</button>
61-
ERB
62-
refute_equal @file, corrected_content
63-
64-
expected_content = <<~ERB
6552
<%# erblint:counter GitHub::Accessibility::AvoidBothDisabledAndAriaDisabledCounter 1 %>
6653
<button disabled aria-disabled="true">Some text</button>
6754
ERB
68-
assert_equal expected_content, corrected_content
55+
@linter.config.counter_enabled = true
56+
@linter.run(processed_source)
57+
assert_empty @linter.offenses
6958
end
7059
end

0 commit comments

Comments
 (0)