rubocop-rails-accessibility 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +9 -0
- data/CONTRIBUTING.md +22 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +108 -0
- data/LICENSE +21 -0
- data/README.md +63 -0
- data/Rakefile +34 -0
- data/config/default.yml +18 -0
- data/guides/image-has-alt.md +29 -0
- data/guides/link-has-href.md +29 -0
- data/guides/no-positive-tabindex.md +24 -0
- data/guides/no-redundant-image-alt.md +24 -0
- data/lib/rubocop/cop/inject.rb +18 -0
- data/lib/rubocop/cop/rubocop-rails-accessibility/image_has_alt.rb +24 -0
- data/lib/rubocop/cop/rubocop-rails-accessibility/link_has_href.rb +23 -0
- data/lib/rubocop/cop/rubocop-rails-accessibility/no_positive_tabindex.rb +32 -0
- data/lib/rubocop/cop/rubocop-rails-accessibility/no_redundant_image_alt.rb +34 -0
- data/lib/rubocop/cop/rubocop_rails_accessibility_cops.rb +11 -0
- data/lib/rubocop/cop/version.rb +5 -0
- data/rubocop-rails-accessibility.gemspec +41 -0
- metadata +180 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9be54c50ab9a31b3bcac4ecd45f4f73ad6861c042034005da32588303622baf4
|
4
|
+
data.tar.gz: c024d057c7ad6339be03b35bfd9153574515d1790c62f82e52e155bc34e18736
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 91dd212f8fab3b256a2eb34015dc9a7b19889cb6ff7403255bf6b60be299120972e44a061b7b478b9ad64bfb683af02abd21a89a594a870e08b82aaa55d849ab
|
7
|
+
data.tar.gz: 59a4b0dfcf465dec709f2fa27528607064c8968987a99365f0aad5f4cb202880e32dbd1b27432a570298357101bd2d2cfdd14b6836a9f5657c992dbd855c87e4
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# How to contribute
|
2
|
+
|
3
|
+
This is an open source project, so if you have suggestions for how this project could be improved, or want to report a bug, open an issue! I'd love all and any contributions. You can submit issues and enhancement requests through the [issues page on GitHub](https://github.com/github/rubocop-rails-accessibility/issues). If you have any question, please don't hesitate to contact me, I'd love to hear from you.
|
4
|
+
|
5
|
+
### Contributing guidelines
|
6
|
+
|
7
|
+
In general, I follow the "fork-and-pull" Git workflow:
|
8
|
+
|
9
|
+
1. **Fork and clone** the repo on GitHub.
|
10
|
+
2. **Configure and install** the dependencies `npm install`.
|
11
|
+
3. **Create** a new branch: `git checkout -b branch-name`.
|
12
|
+
4. **Commit** all your changes to your branch. Please write a [good commit message](https://chris.beams.io/posts/git-commit/).
|
13
|
+
5. **Push** your work back up to your fork.
|
14
|
+
6. **Submit a Pull Request** so that I can review your changes. Please add documentation and some details to the PR.
|
15
|
+
|
16
|
+
Keep your changes as focused as possible. If there are multiple changes you would like to make that are not dependent upon each other, consider submitting them as separate pull requests. Work in Progress pull requests are also welcome to get feedback early on, or if there is something blocked you.
|
17
|
+
|
18
|
+
## Resources
|
19
|
+
|
20
|
+
- [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/)
|
21
|
+
- [Using Pull Requests](https://help.github.com/articles/about-pull-requests/)
|
22
|
+
- [GitHub Help](https://help.github.com)
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rubocop-rails-accessibility (0.1.1)
|
5
|
+
rubocop (>= 1.0.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionview (7.0.3.1)
|
11
|
+
activesupport (= 7.0.3.1)
|
12
|
+
builder (~> 3.1)
|
13
|
+
erubi (~> 1.4)
|
14
|
+
rails-dom-testing (~> 2.0)
|
15
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
16
|
+
activesupport (7.0.3.1)
|
17
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
18
|
+
i18n (>= 1.6, < 2)
|
19
|
+
minitest (>= 5.1)
|
20
|
+
tzinfo (~> 2.0)
|
21
|
+
ast (2.4.2)
|
22
|
+
builder (3.2.4)
|
23
|
+
concurrent-ruby (1.1.10)
|
24
|
+
crass (1.0.6)
|
25
|
+
diff-lcs (1.5.0)
|
26
|
+
erubi (1.11.0)
|
27
|
+
i18n (1.12.0)
|
28
|
+
concurrent-ruby (~> 1.0)
|
29
|
+
json (2.6.2)
|
30
|
+
loofah (2.18.0)
|
31
|
+
crass (~> 1.0.2)
|
32
|
+
nokogiri (>= 1.5.9)
|
33
|
+
minitest (5.16.3)
|
34
|
+
nokogiri (1.13.8-x86_64-darwin)
|
35
|
+
racc (~> 1.4)
|
36
|
+
nokogiri (1.13.8-x86_64-linux)
|
37
|
+
racc (~> 1.4)
|
38
|
+
parallel (1.22.1)
|
39
|
+
parser (3.1.2.0)
|
40
|
+
ast (~> 2.4.1)
|
41
|
+
racc (1.6.0)
|
42
|
+
rack (2.2.4)
|
43
|
+
rails-dom-testing (2.0.3)
|
44
|
+
activesupport (>= 4.2.0)
|
45
|
+
nokogiri (>= 1.6)
|
46
|
+
rails-html-sanitizer (1.4.3)
|
47
|
+
loofah (~> 2.3)
|
48
|
+
rainbow (3.1.1)
|
49
|
+
rake (13.0.6)
|
50
|
+
regexp_parser (2.5.0)
|
51
|
+
rexml (3.2.5)
|
52
|
+
rspec (3.11.0)
|
53
|
+
rspec-core (~> 3.11.0)
|
54
|
+
rspec-expectations (~> 3.11.0)
|
55
|
+
rspec-mocks (~> 3.11.0)
|
56
|
+
rspec-core (3.11.0)
|
57
|
+
rspec-support (~> 3.11.0)
|
58
|
+
rspec-expectations (3.11.0)
|
59
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
60
|
+
rspec-support (~> 3.11.0)
|
61
|
+
rspec-mocks (3.11.1)
|
62
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
63
|
+
rspec-support (~> 3.11.0)
|
64
|
+
rspec-support (3.11.0)
|
65
|
+
rubocop (1.31.2)
|
66
|
+
json (~> 2.3)
|
67
|
+
parallel (~> 1.10)
|
68
|
+
parser (>= 3.1.0.0)
|
69
|
+
rainbow (>= 2.2.2, < 4.0)
|
70
|
+
regexp_parser (>= 1.8, < 3.0)
|
71
|
+
rexml (>= 3.2.5, < 4.0)
|
72
|
+
rubocop-ast (>= 1.18.0, < 2.0)
|
73
|
+
ruby-progressbar (~> 1.7)
|
74
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
75
|
+
rubocop-ast (1.19.1)
|
76
|
+
parser (>= 3.1.1.0)
|
77
|
+
rubocop-github (0.18.0)
|
78
|
+
rubocop (>= 1.0.0)
|
79
|
+
rubocop-performance
|
80
|
+
rubocop-rails
|
81
|
+
rubocop-performance (1.14.3)
|
82
|
+
rubocop (>= 1.7.0, < 2.0)
|
83
|
+
rubocop-ast (>= 0.4.0)
|
84
|
+
rubocop-rails (2.15.2)
|
85
|
+
activesupport (>= 4.2.0)
|
86
|
+
rack (>= 1.1)
|
87
|
+
rubocop (>= 1.7.0, < 2.0)
|
88
|
+
ruby-progressbar (1.11.0)
|
89
|
+
tzinfo (2.0.5)
|
90
|
+
concurrent-ruby (~> 1.0)
|
91
|
+
unicode-display_width (2.2.0)
|
92
|
+
|
93
|
+
PLATFORMS
|
94
|
+
x86_64-darwin-19
|
95
|
+
x86_64-linux
|
96
|
+
|
97
|
+
DEPENDENCIES
|
98
|
+
actionview
|
99
|
+
minitest
|
100
|
+
rake (~> 13.0)
|
101
|
+
rspec (~> 3.0)
|
102
|
+
rubocop-github
|
103
|
+
rubocop-performance
|
104
|
+
rubocop-rails
|
105
|
+
rubocop-rails-accessibility!
|
106
|
+
|
107
|
+
BUNDLED WITH
|
108
|
+
2.3.17
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2022 GitHub
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# RuboCop Rails Accessibility
|
2
|
+
|
3
|
+
This repository provides recommended [RuboCop](https://github.com/rubocop/rubocop) configuration and additional **accessibility RuboCop Rails cops** for use on open source and internal Rails projects.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the `rubocop-rails-accessibility` gem
|
8
|
+
|
9
|
+
```sh
|
10
|
+
gem install rubocop-rails-accessibility
|
11
|
+
```
|
12
|
+
|
13
|
+
or if you use bundler put this in your `Gemfile`
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'rubocop-rails-accessibility'
|
17
|
+
gem "rubocop-performance", require: false
|
18
|
+
gem "rubocop-rails", require: false
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
You need to tell RuboCop to load RuboCop Rails Accessibility.
|
24
|
+
|
25
|
+
Put this into your `.rubocop.yml`.
|
26
|
+
|
27
|
+
``` yaml
|
28
|
+
inherit_gem:
|
29
|
+
rubocop-rails-accessibility:
|
30
|
+
- config/default.yml
|
31
|
+
- config/rubocop_rails_accessibility.yml
|
32
|
+
```
|
33
|
+
|
34
|
+
## Testing
|
35
|
+
|
36
|
+
Install all of the required gems
|
37
|
+
|
38
|
+
```sh
|
39
|
+
bundle install
|
40
|
+
```
|
41
|
+
|
42
|
+
Run the tests
|
43
|
+
|
44
|
+
```sh
|
45
|
+
bundle exec rake
|
46
|
+
```
|
47
|
+
|
48
|
+
## The Cops
|
49
|
+
|
50
|
+
All cops are located under
|
51
|
+
[`lib/rubocop/cop/rubocop_rails_accessibility`](lib/rubocop/cop/rubocop_rails_accessibility), and contain
|
52
|
+
examples/documentation.
|
53
|
+
|
54
|
+
## Rules
|
55
|
+
|
56
|
+
- [RuboCop::Cop::Accessibility::ImageHasAlt](guides/image-has-alt.md)
|
57
|
+
- [RuboCop::Cop::Accessibility::LinkHasHref](guides/link-has-href.md)
|
58
|
+
- [RuboCop::Cop::Accessibility::NoPositiveTabindex](guides/no-positive-tabindex.md)
|
59
|
+
- [RuboCop::Cop::Accessibility::NoRedundantImageAlt](guides/no-redundant-image-alt.md)
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
If you have any suggestion for how this project could be improved, please read the [contributing guidelines](https://github.com/github/rubocop-rails-accessibility/blob/main/CONTRIBUTING.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
require "rspec/core/rake_task"
|
6
|
+
|
7
|
+
Rake::TestTask.new
|
8
|
+
|
9
|
+
RSpec::Core::RakeTask.new(:spec)
|
10
|
+
|
11
|
+
require "rubocop/rake_task"
|
12
|
+
|
13
|
+
RuboCop::RakeTask.new
|
14
|
+
|
15
|
+
task default: %i[test rubocop]
|
16
|
+
|
17
|
+
desc "Generate a new cop with a template"
|
18
|
+
task :new_cop, [:cop] do |_task, args|
|
19
|
+
require "rubocop"
|
20
|
+
|
21
|
+
cop_name = args.fetch(:cop) do
|
22
|
+
warn "usage: bundle exec rake 'new_cop[Department/Name]'"
|
23
|
+
exit!
|
24
|
+
end
|
25
|
+
|
26
|
+
generator = RuboCop::Cop::Generator.new(cop_name)
|
27
|
+
|
28
|
+
generator.write_source
|
29
|
+
generator.write_spec
|
30
|
+
generator.inject_require(root_file_path: "lib/rubocop-rails-accessibility.rb")
|
31
|
+
generator.inject_config(config_file_path: "config/default.yml")
|
32
|
+
|
33
|
+
puts generator.todo
|
34
|
+
end
|
data/config/default.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop/cop/rubocop_rails_accessibility_cops
|
3
|
+
|
4
|
+
AllCops:
|
5
|
+
DisabledByDefault: true
|
6
|
+
|
7
|
+
RubocopRailsAccessibility/ImageHasAlt:
|
8
|
+
Enabled: true
|
9
|
+
StyleGuide: https://github.com/github/rubocop-rails-accessibility/blob/master/guides/image-has-alt.md
|
10
|
+
RubocopRailsAccessibility/LinkHasHref:
|
11
|
+
Enabled: true
|
12
|
+
StyleGuide: https://github.com/github/rubocop-rails-accessibility/blob/master/guides/link-has-href.md
|
13
|
+
RubocopRailsAccessibility/NoPositiveTabindex:
|
14
|
+
Enabled: true
|
15
|
+
StyleGuide: https://github.com/github/rubocop-rails-accessibility/blob/master/guides/no-positive-tabindex.md
|
16
|
+
RubocopRailsAccessibility/NoRedundantImageAlt:
|
17
|
+
Enabled: true
|
18
|
+
StyleGuide: https://github.com/github/rubocop-rails-accessibility/blob/master/guides/no-redundant-image-alt.md
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# RubocopRailsAccessibility/ImageHasAlt
|
2
|
+
|
3
|
+
## Rule Details
|
4
|
+
|
5
|
+
Images should have an alt prop with meaningful text or an empty string for decorative images.
|
6
|
+
|
7
|
+
## Resources
|
8
|
+
|
9
|
+
- [W3C WAI Images Tutorial](https://www.w3.org/WAI/tutorials/images/)
|
10
|
+
- [Primer: Alternative text for images](https://primer.style/design/accessibility/alternative-text-for-images)
|
11
|
+
|
12
|
+
## Examples
|
13
|
+
### **Incorrect** code for this rule 👎
|
14
|
+
|
15
|
+
```erb
|
16
|
+
<%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12" %>
|
17
|
+
```
|
18
|
+
|
19
|
+
### **Correct** code for this rule 👍
|
20
|
+
|
21
|
+
```erb
|
22
|
+
<!-- good -->
|
23
|
+
<%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12", alt: "GitHub Logo spinner" %>
|
24
|
+
```
|
25
|
+
|
26
|
+
```erb
|
27
|
+
<!-- also good -->
|
28
|
+
<%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12", alt: "" %>
|
29
|
+
```
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# RubocopRailsAccessibility/LinkHasHref
|
2
|
+
|
3
|
+
## Rule Details
|
4
|
+
|
5
|
+
Links should go somewhere, you probably want to use a `<button>` instead.
|
6
|
+
|
7
|
+
## Resources
|
8
|
+
|
9
|
+
- [`<a>`: The Anchor element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a)
|
10
|
+
- [Primer: Links](https://primer.style/design/accessibility/links)
|
11
|
+
- [HTML Spec: The a element](https://html.spec.whatwg.org/multipage/text-level-semantics.html#the-a-element)
|
12
|
+
|
13
|
+
## Examples
|
14
|
+
### **Incorrect** code for this rule 👎
|
15
|
+
|
16
|
+
```erb
|
17
|
+
<%= link_to 'Go to GitHub' %>
|
18
|
+
```
|
19
|
+
|
20
|
+
```erb
|
21
|
+
<%= link_to 'Go to GitHub', '#' %>
|
22
|
+
```
|
23
|
+
|
24
|
+
### **Correct** code for this rule 👍
|
25
|
+
|
26
|
+
```erb
|
27
|
+
<!-- good -->
|
28
|
+
<%= link_to 'Go to GitHub', 'https://github.com/' %>
|
29
|
+
```
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# RubocopRailsAccessibility/NoPositiveTabindex
|
2
|
+
|
3
|
+
## Rule Details
|
4
|
+
|
5
|
+
Positive tabindex is error-prone and often inaccessible.
|
6
|
+
|
7
|
+
## Resources
|
8
|
+
|
9
|
+
- [F44: Failure of Success Criterion 2.4.3 due to using tabindex to create a tab order that does not preserve meaning and operability](https://www.w3.org/TR/WCAG20-TECHS/F44.html)
|
10
|
+
- [Deque University: Avoid Using Tabindex with Positive Numbers](https://dequeuniversity.com/tips/tabindex-positive-numbers)
|
11
|
+
|
12
|
+
## Examples
|
13
|
+
### **Incorrect** code for this rule 👎
|
14
|
+
|
15
|
+
```erb
|
16
|
+
<%= button_tag "Continue", :tabindex => 3 %>
|
17
|
+
```
|
18
|
+
|
19
|
+
### **Correct** code for this rule 👍
|
20
|
+
|
21
|
+
```erb
|
22
|
+
<!-- good -->
|
23
|
+
<%= button_tag "Continue", :tabindex => -1 %>
|
24
|
+
```
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# RubocopRailsAccessibility/NoRedundantImageAlt
|
2
|
+
|
3
|
+
## Rule Details
|
4
|
+
|
5
|
+
Alt prop should not contain `image` or `picture` as screen readers already announce the element as an image
|
6
|
+
|
7
|
+
## Resources
|
8
|
+
|
9
|
+
- [W3C WAI Images Tutorial](https://www.w3.org/WAI/tutorials/images/)
|
10
|
+
- [Primer: Alternative text for images](https://primer.style/design/accessibility/alternative-text-for-images)
|
11
|
+
|
12
|
+
## Examples
|
13
|
+
### **Incorrect** code for this rule 👎
|
14
|
+
|
15
|
+
```erb
|
16
|
+
<%= image_tag "cat.gif", size: "12x12", alt: "Picture of a cat" %>
|
17
|
+
```
|
18
|
+
|
19
|
+
### **Correct** code for this rule 👍
|
20
|
+
|
21
|
+
```erb
|
22
|
+
<!-- good -->
|
23
|
+
<%= image_tag "cat.gif", size: "12x12", alt: "A black cat" %>
|
24
|
+
```
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rubocop"
|
4
|
+
|
5
|
+
# The original code is from https://github.com/rubocop/rubocop-rspec/blob/main/lib/rubocop/rspec/inject.rb
|
6
|
+
# See https://github.com/rubocop/rubocop-rspec/blob/main/MIT-LICENSE.md
|
7
|
+
module RubocopRailsAccessibility
|
8
|
+
module Inject
|
9
|
+
def self.defaults!
|
10
|
+
path = CONFIG_DEFAULT.to_s
|
11
|
+
hash = ::RuboCop::ConfigLoader.send(:load_yaml_configuration, path)
|
12
|
+
config = ::RuboCop::Config.new(hash, path).tap(&:make_excludes_absolute)
|
13
|
+
puts "configuration from #{path}" if ::RuboCop::ConfigLoader.debug?
|
14
|
+
config = ::RuboCop::ConfigLoader.merge_with_default(config, path)
|
15
|
+
::RuboCop::ConfigLoader.instance_variable_set(:@default_configuration, config)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rubocop"
|
4
|
+
|
5
|
+
module RuboCop
|
6
|
+
module Cop
|
7
|
+
module RubocopRailsAccessibility
|
8
|
+
class ImageHasAlt < Base
|
9
|
+
MSG = "Images should have an alt prop with meaningful text or an empty string for decorative images"
|
10
|
+
|
11
|
+
def_node_search :has_alt_attribute?, "(sym :alt)"
|
12
|
+
|
13
|
+
def on_send(node)
|
14
|
+
receiver, method_name, = *node
|
15
|
+
|
16
|
+
return unless receiver.nil? && method_name == :image_tag
|
17
|
+
|
18
|
+
alt = has_alt_attribute?(node)
|
19
|
+
add_offense(node.loc.selector) if alt.nil?
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rubocop"
|
4
|
+
|
5
|
+
module RuboCop
|
6
|
+
module Cop
|
7
|
+
module RubocopRailsAccessibility
|
8
|
+
class LinkHasHref < Base
|
9
|
+
MSG = "Links should go somewhere, you probably want to use a `<button>` instead."
|
10
|
+
|
11
|
+
def on_send(node)
|
12
|
+
receiver, method_name, *args = *node
|
13
|
+
|
14
|
+
return unless receiver.nil? && method_name == :link_to
|
15
|
+
|
16
|
+
return unless args.length == 1 || (args.length > 1 && args[1].type == :str && args[1].children.first == "#")
|
17
|
+
|
18
|
+
add_offense(node.loc.selector)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rubocop"
|
4
|
+
|
5
|
+
module RuboCop
|
6
|
+
module Cop
|
7
|
+
module RubocopRailsAccessibility
|
8
|
+
class NoPositiveTabindex < Base
|
9
|
+
MSG = "Positive tabindex is error-prone and often inaccessible."
|
10
|
+
|
11
|
+
def on_send(node)
|
12
|
+
receiver, _method_name, *args = *node
|
13
|
+
|
14
|
+
return unless receiver.nil?
|
15
|
+
|
16
|
+
args_each = args.select do |arg|
|
17
|
+
arg.type == :hash
|
18
|
+
end
|
19
|
+
args_each.each do |hash|
|
20
|
+
hash.each_pair do |key, value|
|
21
|
+
next if key.type == :dsym
|
22
|
+
next unless key.respond_to?(:value)
|
23
|
+
break unless key.value == :tabindex && value.source.to_i.positive?
|
24
|
+
|
25
|
+
add_offense(hash)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rubocop"
|
4
|
+
|
5
|
+
module RuboCop
|
6
|
+
module Cop
|
7
|
+
module RubocopRailsAccessibility
|
8
|
+
class NoRedundantImageAlt < Base
|
9
|
+
MSG = "Alt prop should not contain `image` or `picture` as screen readers already announce the element as an image"
|
10
|
+
REDUNDANT_ALT_WORDS = %w[image picture].freeze
|
11
|
+
|
12
|
+
def_node_search :redundant_alt?, "(pair (sym :alt) (str #contains_redundant_alt_text?))"
|
13
|
+
|
14
|
+
def on_send(node)
|
15
|
+
receiver, method_name, = *node
|
16
|
+
|
17
|
+
return unless receiver.nil? && method_name == :image_tag
|
18
|
+
|
19
|
+
return unless redundant_alt?(node)
|
20
|
+
|
21
|
+
add_offense(node.loc.selector)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def contains_redundant_alt_text?(string)
|
27
|
+
return false if string.empty?
|
28
|
+
|
29
|
+
return true if (string.downcase.split & REDUNDANT_ALT_WORDS).any?
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "pathname"
|
4
|
+
require "yaml"
|
5
|
+
|
6
|
+
require_relative "inject"
|
7
|
+
require_relative "version"
|
8
|
+
require_relative "rubocop-rails-accessibility/image_has_alt"
|
9
|
+
require_relative "rubocop-rails-accessibility/link_has_href"
|
10
|
+
require_relative "rubocop-rails-accessibility/no_positive_tabindex"
|
11
|
+
require_relative "rubocop-rails-accessibility/no_redundant_image_alt"
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/rubocop/cop/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "rubocop-rails-accessibility"
|
7
|
+
spec.version = RubocopRailsAccessibility::VERSION
|
8
|
+
spec.authors = ["GitHub Accessibility Team"]
|
9
|
+
spec.email = ["accessibility@github.com"]
|
10
|
+
|
11
|
+
spec.summary = "Custom extension for RuboCop."
|
12
|
+
spec.homepage = "https://github.com/github/rubocop-rails-accessibility"
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.required_ruby_version = ">= 2.7.0"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
18
|
+
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
|
19
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
20
|
+
|
21
|
+
spec.add_dependency "rubocop", ">= 1.0.0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "actionview"
|
24
|
+
spec.add_development_dependency "minitest"
|
25
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
spec.add_development_dependency "rubocop-github"
|
28
|
+
spec.add_development_dependency "rubocop-rails"
|
29
|
+
spec.add_development_dependency "rubocop-performance"
|
30
|
+
|
31
|
+
# Specify which files should be added to the gem when it is released.
|
32
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
33
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
34
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
35
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
36
|
+
end
|
37
|
+
end
|
38
|
+
spec.bindir = "exe"
|
39
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
40
|
+
spec.require_paths = ["lib"]
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubocop-rails-accessibility
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- GitHub Accessibility Team
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-08-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rubocop
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: actionview
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '13.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '13.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop-github
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop-rails
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop-performance
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description:
|
126
|
+
email:
|
127
|
+
- accessibility@github.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".rspec"
|
133
|
+
- ".rubocop.yml"
|
134
|
+
- CONTRIBUTING.md
|
135
|
+
- Gemfile
|
136
|
+
- Gemfile.lock
|
137
|
+
- LICENSE
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- config/default.yml
|
141
|
+
- guides/image-has-alt.md
|
142
|
+
- guides/link-has-href.md
|
143
|
+
- guides/no-positive-tabindex.md
|
144
|
+
- guides/no-redundant-image-alt.md
|
145
|
+
- lib/rubocop/cop/inject.rb
|
146
|
+
- lib/rubocop/cop/rubocop-rails-accessibility/image_has_alt.rb
|
147
|
+
- lib/rubocop/cop/rubocop-rails-accessibility/link_has_href.rb
|
148
|
+
- lib/rubocop/cop/rubocop-rails-accessibility/no_positive_tabindex.rb
|
149
|
+
- lib/rubocop/cop/rubocop-rails-accessibility/no_redundant_image_alt.rb
|
150
|
+
- lib/rubocop/cop/rubocop_rails_accessibility_cops.rb
|
151
|
+
- lib/rubocop/cop/version.rb
|
152
|
+
- rubocop-rails-accessibility.gemspec
|
153
|
+
homepage: https://github.com/github/rubocop-rails-accessibility
|
154
|
+
licenses:
|
155
|
+
- MIT
|
156
|
+
metadata:
|
157
|
+
homepage_uri: https://github.com/github/rubocop-rails-accessibility
|
158
|
+
source_code_uri: https://github.com/github/rubocop-rails-accessibility
|
159
|
+
changelog_uri: https://github.com/github/rubocop-rails-accessibility/blob/main/CHANGELOG.md
|
160
|
+
rubygems_mfa_required: 'true'
|
161
|
+
post_install_message:
|
162
|
+
rdoc_options: []
|
163
|
+
require_paths:
|
164
|
+
- lib
|
165
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
166
|
+
requirements:
|
167
|
+
- - ">="
|
168
|
+
- !ruby/object:Gem::Version
|
169
|
+
version: 2.7.0
|
170
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
171
|
+
requirements:
|
172
|
+
- - ">="
|
173
|
+
- !ruby/object:Gem::Version
|
174
|
+
version: '0'
|
175
|
+
requirements: []
|
176
|
+
rubygems_version: 3.3.10
|
177
|
+
signing_key:
|
178
|
+
specification_version: 4
|
179
|
+
summary: Custom extension for RuboCop.
|
180
|
+
test_files: []
|