diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4ef9ab5..3963d08 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -13,7 +13,7 @@ jobs: strategy: matrix: ruby: - - '2.6' + - '2.7' steps: - uses: actions/checkout@v2 diff --git a/.gitignore b/.gitignore index b04a8c8..4936e10 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,6 @@ /pkg/ /spec/reports/ /tmp/ - +vendor/ # rspec failure tracking .rspec_status diff --git a/.rubocop.yml b/.rubocop.yml index 47b1089..8cbb45e 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,10 +1,9 @@ -AllCops: - NewCops: enable - SuggestExtensions: false - TargetRubyVersion: 2.6 +inherit_gem: + rubocop-github: + - config/default.yml + - config/rails.yml -Metrics: - Enabled: false - -Style/Documentation: - Enabled: false +Naming/FileName: + Enabled: true + Exclude: + - "rubocop-rails-accessibility.gemspec" diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 92ea4b6..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,7 +0,0 @@ -# Changelog - -## Unreleased - -### Added - -- Initial release. diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md deleted file mode 100644 index 773b34d..0000000 --- a/CODE_OF_CONDUCT.md +++ /dev/null @@ -1,84 +0,0 @@ -# Contributor Covenant Code of Conduct - -## Our Pledge - -We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. - -We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community. - -## Our Standards - -Examples of behavior that contributes to a positive environment for our community include: - -* Demonstrating empathy and kindness toward other people -* Being respectful of differing opinions, viewpoints, and experiences -* Giving and gracefully accepting constructive feedback -* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience -* Focusing on what is best not just for us as individuals, but for the overall community - -Examples of unacceptable behavior include: - -* The use of sexualized language or imagery, and sexual attention or - advances of any kind -* Trolling, insulting or derogatory comments, and personal or political attacks -* Public or private harassment -* Publishing others' private information, such as a physical or email - address, without their explicit permission -* Other conduct which could reasonably be considered inappropriate in a - professional setting - -## Enforcement Responsibilities - -Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful. - -Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate. - -## Scope - -This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. - -## Enforcement - -Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at r7kamura@gmail.com. All complaints will be reviewed and investigated promptly and fairly. - -All community leaders are obligated to respect the privacy and security of the reporter of any incident. - -## Enforcement Guidelines - -Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct: - -### 1. Correction - -**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community. - -**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested. - -### 2. Warning - -**Community Impact**: A violation through a single incident or series of actions. - -**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban. - -### 3. Temporary Ban - -**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior. - -**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban. - -### 4. Permanent Ban - -**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. - -**Consequence**: A permanent ban from any sort of public interaction within the community. - -## Attribution - -This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0, -available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. - -Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity). - -[homepage]: https://www.contributor-covenant.org - -For answers to common questions about this code of conduct, see the FAQ at -https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..8ef4b95 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,22 @@ +# How to contribute + +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. + +### Contributing guidelines + +In general, I follow the "fork-and-pull" Git workflow: + +1. **Fork and clone** the repo on GitHub. +2. **Configure and install** the dependencies `npm install`. +3. **Create** a new branch: `git checkout -b branch-name`. +4. **Commit** all your changes to your branch. Please write a [good commit message](https://chris.beams.io/posts/git-commit/). +5. **Push** your work back up to your fork. +6. **Submit a Pull Request** so that I can review your changes. Please add documentation and some details to the PR. + +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. + +## Resources + +- [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/) +- [Using Pull Requests](https://help.github.com/articles/about-pull-requests/) +- [GitHub Help](https://help.github.com) diff --git a/Gemfile b/Gemfile index b4fb0b5..fb446d7 100644 --- a/Gemfile +++ b/Gemfile @@ -1,10 +1,6 @@ # frozen_string_literal: true -source 'https://rubygems.org' +source "https://rubygems.org" -# Specify your gem's dependencies in my_extension.gemspec +# Specify your gem's dependencies in rubocop-rails-accessibility.gemspec gemspec - -gem 'rake', '~> 13.0' -gem 'rspec', '~> 3.0' -gem 'rubocop', '~> 1.21' diff --git a/Gemfile.lock b/Gemfile.lock index 62d754a..ecc1cb7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,18 +1,50 @@ PATH remote: . specs: - my_extension (0.1.0) - rubocop + rubocop-rails-accessibility (0.1.0) + rubocop (>= 1.0.0) GEM remote: https://rubygems.org/ specs: + actionview (7.0.3.1) + activesupport (= 7.0.3.1) + builder (~> 3.1) + erubi (~> 1.4) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.1, >= 1.2.0) + activesupport (7.0.3.1) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 1.6, < 2) + minitest (>= 5.1) + tzinfo (~> 2.0) ast (2.4.2) + builder (3.2.4) + concurrent-ruby (1.1.10) + crass (1.0.6) diff-lcs (1.5.0) + erubi (1.11.0) + i18n (1.12.0) + concurrent-ruby (~> 1.0) json (2.6.2) + loofah (2.18.0) + crass (~> 1.0.2) + nokogiri (>= 1.5.9) + minitest (5.16.3) + nokogiri (1.13.8-x86_64-darwin) + racc (~> 1.4) + nokogiri (1.13.8-x86_64-linux) + racc (~> 1.4) parallel (1.22.1) parser (3.1.2.0) ast (~> 2.4.1) + racc (1.6.0) + rack (2.2.4) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) + rails-html-sanitizer (1.4.3) + loofah (~> 2.3) rainbow (3.1.1) rake (13.0.6) regexp_parser (2.5.0) @@ -42,17 +74,35 @@ GEM unicode-display_width (>= 1.4.0, < 3.0) rubocop-ast (1.19.1) parser (>= 3.1.1.0) + rubocop-github (0.18.0) + rubocop (>= 1.0.0) + rubocop-performance + rubocop-rails + rubocop-performance (1.14.3) + rubocop (>= 1.7.0, < 2.0) + rubocop-ast (>= 0.4.0) + rubocop-rails (2.15.2) + activesupport (>= 4.2.0) + rack (>= 1.1) + rubocop (>= 1.7.0, < 2.0) ruby-progressbar (1.11.0) + tzinfo (2.0.5) + concurrent-ruby (~> 1.0) unicode-display_width (2.2.0) PLATFORMS + x86_64-darwin-19 x86_64-linux DEPENDENCIES - my_extension! + actionview + minitest rake (~> 13.0) rspec (~> 3.0) - rubocop (~> 1.21) + rubocop-github + rubocop-performance + rubocop-rails + rubocop-rails-accessibility! BUNDLED WITH 2.3.17 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..fac6e63 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 GitHub + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index c5ca2fd..678a2d7 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,63 @@ -# rubocop-extension-template +# RuboCop Rails Accessibility -Custom [RuboCop](https://github.com/rubocop/rubocop) extension template. +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. + +## Installation + +Install the `rubocop-rails-accessibility` gem + +```sh +gem install rubocop-rails-accessibility +``` + +or if you use bundler put this in your `Gemfile` + +```ruby +gem 'rubocop-rails-accessibility' +gem "rubocop-performance", require: false +gem "rubocop-rails", require: false +``` ## Usage -1. [Use this template](https://github.com/r7kamura/rubocop-extension-template/generate) to create a repository -2. Rename gem name from `my_extension` to your own -3. Run `bin/setup` -4. Add your custom cop by running `bundle exec rake new_cop[Foo/Bar]` -5. Edit `lib/rubocop/cop/foo/bar.rb` +You need to tell RuboCop to load RuboCop Rails Accessibility. + +Put this into your `.rubocop.yml`. + +``` yaml +inherit_gem: + rubocop-rails-accessibility: + - config/default.yml + - config/rubocop_rails_accessibility.yml +``` + +## Testing + +Install all of the required gems + +```sh[] +bundle install +```[] + +Run the tests + +```sh +bundle exec rake +``` + +## The Cops + +All cops are located under +[`lib/rubocop/cop/rubocop_rails_accessibility`](lib/rubocop/cop/rubocop_rails_accessibility), and contain +examples/documentation. + +## Rules + +- [RuboCop::Cop::Accessibility::ImageHasAlt](guides/image-has-alt.md) +- [RuboCop::Cop::Accessibility::LinkHasHref](guides/link-has-href.md) +- [RuboCop::Cop::Accessibility::NoPositiveTabindex](guides/no-positive-tabindex.md) +- [RuboCop::Cop::Accessibility::NoRedundantImageAlt](guides/no-redundant-image-alt.md) + +## Contributing + +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). \ No newline at end of file diff --git a/Rakefile b/Rakefile index 2cc39f9..10da0ea 100644 --- a/Rakefile +++ b/Rakefile @@ -1,19 +1,22 @@ # frozen_string_literal: true -require 'bundler/gem_tasks' -require 'rspec/core/rake_task' +require "bundler/gem_tasks" +require "rake/testtask" +require "rspec/core/rake_task" + +Rake::TestTask.new RSpec::Core::RakeTask.new(:spec) -require 'rubocop/rake_task' +require "rubocop/rake_task" RuboCop::RakeTask.new -task default: %i[spec rubocop] +task default: %i[test rubocop] -desc 'Generate a new cop with a template' +desc "Generate a new cop with a template" task :new_cop, [:cop] do |_task, args| - require 'rubocop' + require "rubocop" cop_name = args.fetch(:cop) do warn "usage: bundle exec rake 'new_cop[Department/Name]'" @@ -24,8 +27,8 @@ task :new_cop, [:cop] do |_task, args| generator.write_source generator.write_spec - generator.inject_require(root_file_path: 'lib/my_extension.rb') - generator.inject_config(config_file_path: 'config/default.yml') + generator.inject_require(root_file_path: "lib/rubocop-rails-accessibility.rb") + generator.inject_config(config_file_path: "config/default.yml") puts generator.todo end diff --git a/bin/console b/bin/console index 381c37c..92100c7 100755 --- a/bin/console +++ b/bin/console @@ -2,7 +2,7 @@ # frozen_string_literal: true require 'bundler/setup' -require 'my_extension' +require 'rubocop-rails-accessibility' # You can add fixtures and/or initialization code here to make experimenting # with your gem easier. You can also use a different console, if you like. diff --git a/config/default.yml b/config/default.yml index 88db962..c5cbbd3 100644 --- a/config/default.yml +++ b/config/default.yml @@ -1,4 +1,18 @@ -MyExtension/Example: - Description: 'TODO: Write a description of the cop.' - Enabled: pending - VersionAdded: '0.1' +require: + - rubocop/cop/rubocop_rails_accessibility_cops + +AllCops: + DisabledByDefault: true + +RubocopRailsAccessibility/ImageHasAlt: + Enabled: true + StyleGuide: https://github.com/github/rubocop-rails-accessibility/blob/master/guides/image-has-alt.md +RubocopRailsAccessibility/LinkHasHref: + Enabled: true + StyleGuide: https://github.com/github/rubocop-rails-accessibility/blob/master/guides/link-has-href.md +RubocopRailsAccessibility/NoPositiveTabindex: + Enabled: true + StyleGuide: https://github.com/github/rubocop-rails-accessibility/blob/master/guides/no-positive-tabindex.md +RubocopRailsAccessibility/NoRedundantImageAlt: + Enabled: true + StyleGuide: https://github.com/github/rubocop-rails-accessibility/blob/master/guides/no-redundant-image-alt.md diff --git a/guides/image-has-alt.md b/guides/image-has-alt.md new file mode 100644 index 0000000..3dab40b --- /dev/null +++ b/guides/image-has-alt.md @@ -0,0 +1,29 @@ +# RubocopRailsAccessibility/ImageHasAlt + +## Rule Details + +Images should have an alt prop with meaningful text or an empty string for decorative images. + +## Resources + +- [W3C WAI Images Tutorial](https://www.w3.org/WAI/tutorials/images/) +- [Primer: Alternative text for images](https://primer.style/design/accessibility/alternative-text-for-images) + +## Examples +### **Incorrect** code for this rule 👎 + +```erb +<%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12" %> +``` + +### **Correct** code for this rule 👍 + +```erb + +<%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12", alt: "GitHub Logo spinner" %> +``` + +```erb + +<%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12", alt: "" %> +``` \ No newline at end of file diff --git a/guides/link-has-href.md b/guides/link-has-href.md new file mode 100644 index 0000000..9ace0e3 --- /dev/null +++ b/guides/link-has-href.md @@ -0,0 +1,29 @@ +# RubocopRailsAccessibility/LinkHasHref + +## Rule Details + +Links should go somewhere, you probably want to use a `