├── .github ├── dependabot.yml └── workflows │ └── test.yml ├── .gitignore ├── .rspec ├── .rubocop.yml ├── CODEOWNERS ├── CONTRIBUTING.md ├── Gemfile ├── Gemfile.lock ├── LICENSE ├── README.md ├── Rakefile ├── bin ├── console └── setup ├── config └── default.yml ├── guides ├── image-has-alt.md ├── no-positive-tabindex.md └── no-redundant-image-alt.md ├── lib ├── rubocop-rails-accessibility.rb └── rubocop │ ├── cop │ └── rails_accessibility │ │ ├── image_has_alt.rb │ │ ├── no_positive_tabindex.rb │ │ └── no_redundant_image_alt.rb │ ├── rails_accessibility.rb │ └── rails_accessibility │ ├── inject.rb │ └── version.rb ├── rubocop-rails-accessibility.gemspec └── test ├── cop_test.rb ├── test_image_has_alt.rb ├── test_no_positive_tabindex.rb └── test_no_redundant_image_alt.rb /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: bundler 4 | directory: "/" 5 | schedule: 6 | interval: weekly 7 | groups: 8 | all-dependencies: 9 | patterns: 10 | - "*" 11 | update-types: 12 | - "minor" 13 | - "patch" 14 | - package-ecosystem: github-actions 15 | directory: "/" 16 | schedule: 17 | interval: weekly 18 | groups: 19 | all-dependencies: 20 | patterns: 21 | - "*" 22 | update-types: 23 | - "minor" 24 | - "patch" 25 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: test 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | 9 | permissions: 10 | contents: write 11 | pull-requests: write 12 | 13 | jobs: 14 | build: 15 | runs-on: ubuntu-latest 16 | name: Ruby ${{ matrix.ruby }} 17 | strategy: 18 | matrix: 19 | ruby: 20 | - '3.2' 21 | 22 | steps: 23 | - uses: actions/checkout@v4 24 | - name: Set up Ruby 25 | uses: ruby/setup-ruby@13e7a03dc3ac6c3798f4570bfead2aed4d96abfb 26 | with: 27 | ruby-version: ${{ matrix.ruby }} 28 | bundler-cache: true 29 | - name: Run the default task 30 | run: bundle exec rake 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /_yardoc/ 4 | /coverage/ 5 | /doc/ 6 | /pkg/ 7 | /spec/reports/ 8 | /tmp/ 9 | vendor/ 10 | # rspec failure tracking 11 | .rspec_status 12 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | --require spec_helper 4 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | require: 2 | - rubocop-rails-accessibility 3 | 4 | inherit_gem: 5 | rubocop-github: 6 | - config/default.yml 7 | - config/rails.yml 8 | 9 | Naming/FileName: 10 | Enabled: true 11 | Exclude: 12 | - "rubocop-rails-accessibility.gemspec" 13 | - "lib/rubocop-rails-accessibility.rb" 14 | -------------------------------------------------------------------------------- /CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @github/accessibility-reviewers 2 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 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) 23 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source "https://rubygems.org" 4 | 5 | # Specify your gem's dependencies in rubocop-rails-accessibility.gemspec 6 | gemspec 7 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | rubocop-rails-accessibility (1.0.1) 5 | rubocop (>= 1.0.0) 6 | 7 | GEM 8 | remote: https://rubygems.org/ 9 | specs: 10 | actionview (8.0.2) 11 | activesupport (= 8.0.2) 12 | builder (~> 3.1) 13 | erubi (~> 1.11) 14 | rails-dom-testing (~> 2.2) 15 | rails-html-sanitizer (~> 1.6) 16 | activesupport (8.0.2) 17 | base64 18 | benchmark (>= 0.3) 19 | bigdecimal 20 | concurrent-ruby (~> 1.0, >= 1.3.1) 21 | connection_pool (>= 2.2.5) 22 | drb 23 | i18n (>= 1.6, < 2) 24 | logger (>= 1.4.2) 25 | minitest (>= 5.1) 26 | securerandom (>= 0.3) 27 | tzinfo (~> 2.0, >= 2.0.5) 28 | uri (>= 0.13.1) 29 | ast (2.4.3) 30 | base64 (0.3.0) 31 | benchmark (0.4.1) 32 | bigdecimal (3.2.1) 33 | builder (3.3.0) 34 | concurrent-ruby (1.3.5) 35 | connection_pool (2.5.3) 36 | crass (1.0.6) 37 | diff-lcs (1.6.2) 38 | drb (2.2.3) 39 | erubi (1.13.1) 40 | i18n (1.14.7) 41 | concurrent-ruby (~> 1.0) 42 | json (2.12.2) 43 | language_server-protocol (3.17.0.5) 44 | lint_roller (1.1.0) 45 | logger (1.7.0) 46 | loofah (2.24.0) 47 | crass (~> 1.0.2) 48 | nokogiri (>= 1.12.0) 49 | minitest (5.25.5) 50 | nokogiri (1.18.8-arm64-darwin) 51 | racc (~> 1.4) 52 | nokogiri (1.18.8-x86_64-darwin) 53 | racc (~> 1.4) 54 | nokogiri (1.18.8-x86_64-linux-gnu) 55 | racc (~> 1.4) 56 | parallel (1.27.0) 57 | parser (3.3.8.0) 58 | ast (~> 2.4.1) 59 | racc 60 | prism (1.4.0) 61 | racc (1.8.1) 62 | rack (3.1.16) 63 | rails-dom-testing (2.2.0) 64 | activesupport (>= 5.0.0) 65 | minitest 66 | nokogiri (>= 1.6) 67 | rails-html-sanitizer (1.6.2) 68 | loofah (~> 2.21) 69 | nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0) 70 | rainbow (3.1.1) 71 | rake (13.3.0) 72 | regexp_parser (2.10.0) 73 | rspec (3.13.1) 74 | rspec-core (~> 3.13.0) 75 | rspec-expectations (~> 3.13.0) 76 | rspec-mocks (~> 3.13.0) 77 | rspec-core (3.13.4) 78 | rspec-support (~> 3.13.0) 79 | rspec-expectations (3.13.5) 80 | diff-lcs (>= 1.2.0, < 2.0) 81 | rspec-support (~> 3.13.0) 82 | rspec-mocks (3.13.5) 83 | diff-lcs (>= 1.2.0, < 2.0) 84 | rspec-support (~> 3.13.0) 85 | rspec-support (3.13.4) 86 | rubocop (1.75.8) 87 | json (~> 2.3) 88 | language_server-protocol (~> 3.17.0.2) 89 | lint_roller (~> 1.1.0) 90 | parallel (~> 1.10) 91 | parser (>= 3.3.0.2) 92 | rainbow (>= 2.2.2, < 4.0) 93 | regexp_parser (>= 2.9.3, < 3.0) 94 | rubocop-ast (>= 1.44.0, < 2.0) 95 | ruby-progressbar (~> 1.7) 96 | unicode-display_width (>= 2.4.0, < 4.0) 97 | rubocop-ast (1.44.1) 98 | parser (>= 3.3.7.2) 99 | prism (~> 1.4) 100 | rubocop-github (0.25.0) 101 | rubocop (>= 1.72) 102 | rubocop-performance (>= 1.24) 103 | rubocop-rails (>= 2.23) 104 | rubocop-performance (1.25.0) 105 | lint_roller (~> 1.1) 106 | rubocop (>= 1.75.0, < 2.0) 107 | rubocop-ast (>= 1.38.0, < 2.0) 108 | rubocop-rails (2.32.0) 109 | activesupport (>= 4.2.0) 110 | lint_roller (~> 1.1) 111 | rack (>= 1.1) 112 | rubocop (>= 1.75.0, < 2.0) 113 | rubocop-ast (>= 1.44.0, < 2.0) 114 | ruby-progressbar (1.13.0) 115 | securerandom (0.4.1) 116 | tzinfo (2.0.6) 117 | concurrent-ruby (~> 1.0) 118 | unicode-display_width (3.1.4) 119 | unicode-emoji (~> 4.0, >= 4.0.4) 120 | unicode-emoji (4.0.4) 121 | uri (1.0.3) 122 | 123 | PLATFORMS 124 | arm64-darwin-21 125 | arm64-darwin-23 126 | x86_64-darwin-19 127 | x86_64-darwin-21 128 | x86_64-linux 129 | 130 | DEPENDENCIES 131 | actionview 132 | minitest 133 | rake (~> 13.0) 134 | rspec (~> 3.0) 135 | rubocop-github 136 | rubocop-performance 137 | rubocop-rails 138 | rubocop-rails-accessibility! 139 | 140 | BUNDLED WITH 141 | 2.4.10 142 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 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 | require: 29 | - rubocop-rails-accessibility 30 | ``` 31 | 32 | ## Testing 33 | 34 | Install all of the required gems 35 | 36 | ```sh 37 | bundle install 38 | ``` 39 | 40 | Run the tests 41 | 42 | ```sh 43 | bundle exec rake 44 | ``` 45 | 46 | ## The Cops 47 | 48 | All cops are located under 49 | [`lib/rubocop/cop/rails_accessibility`](lib/rubocop/cop/rails_accessibility), and contain 50 | examples/documentation. 51 | 52 | ## Rules 53 | 54 | - [RailsAccessibility/ImageHasAlt](guides/image-has-alt.md) 55 | - [RailsAccessibility/NoPositiveTabindex](guides/no-positive-tabindex.md) 56 | - [RailsAccessibility/NoRedundantImageAlt](guides/no-redundant-image-alt.md) 57 | 58 | ## Contributing 59 | 60 | 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). 61 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 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 35 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | require 'bundler/setup' 5 | require 'rubocop-rails-accessibility' 6 | 7 | # You can add fixtures and/or initialization code here to make experimenting 8 | # with your gem easier. You can also use a different console, if you like. 9 | 10 | # (If you use this, don't forget to add pry to your Gemfile!) 11 | # require "pry" 12 | # Pry.start 13 | 14 | require 'irb' 15 | IRB.start(__FILE__) 16 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | set -vx 5 | 6 | bundle install 7 | 8 | # Do any other automated setup that you need to do here 9 | -------------------------------------------------------------------------------- /config/default.yml: -------------------------------------------------------------------------------- 1 | RailsAccessibility/ImageHasAlt: 2 | Enabled: true 3 | StyleGuide: https://github.com/github/rubocop-rails-accessibility/blob/master/guides/image-has-alt.md 4 | 5 | RailsAccessibility/NoPositiveTabindex: 6 | Enabled: true 7 | StyleGuide: https://github.com/github/rubocop-rails-accessibility/blob/master/guides/no-positive-tabindex.md 8 | 9 | RailsAccessibility/NoRedundantImageAlt: 10 | Enabled: true 11 | StyleGuide: https://github.com/github/rubocop-rails-accessibility/blob/master/guides/no-redundant-image-alt.md 12 | -------------------------------------------------------------------------------- /guides/image-has-alt.md: -------------------------------------------------------------------------------- 1 | # RailsAccessibility/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 | 23 | <%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12", alt: "GitHub Logo spinner" %> 24 | ``` 25 | 26 | ```erb 27 | 28 | <%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12", alt: "" %> 29 | ``` 30 | -------------------------------------------------------------------------------- /guides/no-positive-tabindex.md: -------------------------------------------------------------------------------- 1 | # RailsAccessibility/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 | 23 | <%= button_tag "Continue", :tabindex => -1 %> 24 | ``` 25 | -------------------------------------------------------------------------------- /guides/no-redundant-image-alt.md: -------------------------------------------------------------------------------- 1 | # RailsAccessibility/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 | 23 | <%= image_tag "cat.gif", size: "12x12", alt: "A black cat" %> 24 | ``` 25 | -------------------------------------------------------------------------------- /lib/rubocop-rails-accessibility.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "rubocop" 4 | require "rubocop/rails_accessibility" 5 | require "rubocop/rails_accessibility/inject" 6 | require "rubocop/rails_accessibility/version" 7 | 8 | RuboCop::RailsAccessibility::Inject.defaults! 9 | 10 | require "rubocop/cop/rails_accessibility/image_has_alt" 11 | require "rubocop/cop/rails_accessibility/no_positive_tabindex" 12 | require "rubocop/cop/rails_accessibility/no_redundant_image_alt" 13 | -------------------------------------------------------------------------------- /lib/rubocop/cop/rails_accessibility/image_has_alt.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "rubocop" 4 | 5 | module RuboCop 6 | module Cop 7 | module RailsAccessibility 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 25 | -------------------------------------------------------------------------------- /lib/rubocop/cop/rails_accessibility/no_positive_tabindex.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "rubocop" 4 | 5 | module RuboCop 6 | module Cop 7 | module RailsAccessibility 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 33 | -------------------------------------------------------------------------------- /lib/rubocop/cop/rails_accessibility/no_redundant_image_alt.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "rubocop" 4 | 5 | module RuboCop 6 | module Cop 7 | module RailsAccessibility 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 35 | -------------------------------------------------------------------------------- /lib/rubocop/rails_accessibility.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "pathname" 4 | 5 | module RuboCop 6 | module RailsAccessibility 7 | PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze 8 | CONFIG_DEFAULT = PROJECT_ROOT.join("config", "default.yml").freeze 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /lib/rubocop/rails_accessibility/inject.rb: -------------------------------------------------------------------------------- 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 RuboCop 8 | module RailsAccessibility 9 | module Inject 10 | def self.defaults! 11 | path = CONFIG_DEFAULT.to_s 12 | hash = ::RuboCop::ConfigLoader.send(:load_yaml_configuration, path) 13 | config = ::RuboCop::Config.new(hash, path).tap(&:make_excludes_absolute) 14 | puts "configuration from #{path}" if ::RuboCop::ConfigLoader.debug? 15 | config = ::RuboCop::ConfigLoader.merge_with_default(config, path) 16 | ::RuboCop::ConfigLoader.instance_variable_set(:@default_configuration, config) 17 | end 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /lib/rubocop/rails_accessibility/version.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module RuboCop 4 | module RailsAccessibility 5 | VERSION = "1.0.1" 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /rubocop-rails-accessibility.gemspec: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require_relative "lib/rubocop/rails_accessibility/version" 4 | 5 | Gem::Specification.new do |spec| 6 | spec.name = "rubocop-rails-accessibility" 7 | spec.version = RuboCop::RailsAccessibility::VERSION 8 | spec.authors = ["GitHub Accessibility Team"] 9 | spec.email = ["accessibility@github.com"] 10 | 11 | spec.summary = "Custom RuboCop rules for Rails Accessibility." 12 | spec.homepage = "https://github.com/github/rubocop-rails-accessibility" 13 | spec.license = "MIT" 14 | spec.required_ruby_version = ">= 3.2.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 | spec.files = Dir[ 32 | "config/*.yml", 33 | "guides/*.md", 34 | "lib/**/*.rb", 35 | "CONTRIBUTING.md", 36 | "LICENSE", 37 | "README.md", 38 | ] 39 | 40 | spec.require_paths = ["lib"] 41 | end 42 | -------------------------------------------------------------------------------- /test/cop_test.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "action_view" 4 | require "minitest" 5 | 6 | class CopTest < Minitest::Test 7 | def cop_class 8 | raise NotImplementedError 9 | end 10 | 11 | attr_reader :cop 12 | 13 | def setup 14 | @config = RuboCop::Config.new 15 | @cop = cop_class.new(@config) 16 | end 17 | 18 | def investigate(cop, src, filename = nil) 19 | processed_source = RuboCop::ProcessedSource.new(src, RUBY_VERSION.to_f, filename) 20 | team = RuboCop::Cop::Team.new([cop], @config, raise_error: true) 21 | report = team.investigate(processed_source) 22 | report.offenses 23 | end 24 | 25 | def erb_investigate(cop, src, filename = nil) 26 | engine = ActionView::Template::Handlers::ERB::Erubi.new(src) 27 | investigate(cop, engine.src.dup, filename) 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /test/test_image_has_alt.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require_relative "./cop_test" 4 | require "minitest/autorun" 5 | require "rubocop/cop/rails_accessibility/image_has_alt" 6 | 7 | class TestImageHasAlt < CopTest 8 | def cop_class 9 | RuboCop::Cop::RailsAccessibility::ImageHasAlt 10 | end 11 | 12 | def test_image_has_alt_offense 13 | offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" 14 | <%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12" %> 15 | ERB 16 | 17 | assert_equal 1, offenses.count 18 | assert_equal "RailsAccessibility/ImageHasAlt: Images should have an alt prop with meaningful text or an empty string for decorative images", 19 | offenses[0].message 20 | end 21 | 22 | def test_image_has_alt_no_offense 23 | offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" 24 | <%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12", alt: "GitHub Logo spinner" %> 25 | ERB 26 | 27 | assert_equal 0, offenses.count 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /test/test_no_positive_tabindex.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require_relative "./cop_test" 4 | require "minitest/autorun" 5 | require "rubocop/cop/rails_accessibility/no_positive_tabindex" 6 | 7 | class NoPositiveTabindex < CopTest 8 | def cop_class 9 | RuboCop::Cop::RailsAccessibility::NoPositiveTabindex 10 | end 11 | 12 | def test_no_positive_tabindex_alt_offense 13 | offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" 14 | <%= button_tag "Continue", :tabindex => 3 %> 15 | ERB 16 | 17 | assert_equal 1, offenses.count 18 | assert_equal "RailsAccessibility/NoPositiveTabindex: Positive tabindex is error-prone and often inaccessible.", offenses[0].message 19 | end 20 | 21 | def test_no_positive_tabindex_alt_no_offense 22 | offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" 23 | <%= button_tag "Continue", :tabindex => -1 %> 24 | ERB 25 | 26 | assert_equal 0, offenses.count 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /test/test_no_redundant_image_alt.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require_relative "./cop_test" 4 | require "minitest/autorun" 5 | require "rubocop/cop/rails_accessibility/no_redundant_image_alt" 6 | 7 | class NoRedundantImageAlt < CopTest 8 | def cop_class 9 | RuboCop::Cop::RailsAccessibility::NoRedundantImageAlt 10 | end 11 | 12 | def test_no_redundant_image_alt_offense 13 | offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" 14 | <%= image_tag "cat.gif", size: "12x12", alt: "Picture of a cat" %> 15 | ERB 16 | 17 | assert_equal 1, offenses.count 18 | assert_equal "RailsAccessibility/NoRedundantImageAlt: Alt prop should not contain `image` or `picture` as screen readers already announce the element as an image", 19 | offenses[0].message 20 | end 21 | 22 | def test_no_redundant_image_alt_no_offense 23 | offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" 24 | <%= image_tag "cat.gif", size: "12x12", alt: "A black cat" %> 25 | ERB 26 | 27 | assert_equal 0, offenses.count 28 | end 29 | end 30 | --------------------------------------------------------------------------------