Skip to content
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

Suggestions for PR #1 #2

Merged
merged 3 commits into from
Aug 24, 2022
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
ruby:
- '2.6'
- '2.7'

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
/pkg/
/spec/reports/
/tmp/

vendor/
# rspec failure tracking
.rspec_status
10 changes: 5 additions & 5 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
AllCops:
NewCops: enable
SuggestExtensions: false
TargetRubyVersion: 2.6
inherit_gem:
rubocop-github:
- config/default.yml
- config/rails.yml

Naming/FileName:
Enabled: true
Exclude:
- "rubocop-rails-accessibility.gemspec"
- "rubocop-rails-accessibility.gemspec"
6 changes: 1 addition & 5 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# frozen_string_literal: true

source 'https://rubygems.org'
source "https://rubygems.org"

# Specify your gem's dependencies in rubocop-rails-accessibility.gemspec
gemspec

gem 'rake', '~> 13.0'
gem 'rspec', '~> 3.0'
gem 'rubocop', '~> 1.21'
21 changes: 19 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ PATH
remote: .
specs:
rubocop-rails-accessibility (0.1.0)
rubocop
rubocop (>= 1.0.0)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -31,12 +31,15 @@ GEM
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)
Expand Down Expand Up @@ -71,20 +74,34 @@ 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
actionview
minitest
rake (~> 13.0)
rspec (~> 3.0)
rubocop (~> 1.21)
rubocop-github
rubocop-performance
rubocop-rails
rubocop-rails-accessibility!

BUNDLED WITH
Expand Down
16 changes: 8 additions & 8 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rake/testtask'
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[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]'"
Expand All @@ -27,8 +27,8 @@ task :new_cop, [:cop] do |_task, args|

generator.write_source
generator.write_spec
generator.inject_require(root_file_path: 'lib/rubocop-rails-accessibility.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
1 change: 0 additions & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require:
- rubocop/cop/rubocop_rails_accessibility_cops
- rubocop-performance

AllCops:
DisabledByDefault: true
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/inject.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'rubocop'
require "rubocop"

# The original code is from https://github.com/rubocop/rubocop-rspec/blob/main/lib/rubocop/rspec/inject.rb
# See https://github.com/rubocop/rubocop-rspec/blob/main/MIT-LICENSE.md
Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/cop/rubocop-rails-accessibility/image_has_alt.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# frozen_string_literal: true

require 'rubocop'
require "rubocop"

module RuboCop
module Cop
module RubocopRailsAccessibility
class ImageHasAlt < Base
MSG = 'Images should have an alt prop with meaningful text or an empty string for decorative images'
MSG = "Images should have an alt prop with meaningful text or an empty string for decorative images"

def_node_search :has_alt_attribute?, '(sym :alt)'
def_node_search :has_alt_attribute?, "(sym :alt)"

def on_send(node)
receiver, method_name, = *node
Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/cop/rubocop-rails-accessibility/link_has_href.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# frozen_string_literal: true

require 'rubocop'
require "rubocop"

module RuboCop
module Cop
module RubocopRailsAccessibility
class LinkHasHref < Base
MSG = 'Links should go somewhere, you probably want to use a `<button>` instead.'
MSG = "Links should go somewhere, you probably want to use a `<button>` instead."

def on_send(node)
receiver, method_name, *args = *node

return unless receiver.nil? && method_name == :link_to

return unless args.length == 1 || (args.length > 1 && args[1].type == :str && args[1].children.first == '#')
return unless args.length == 1 || (args.length > 1 && args[1].type == :str && args[1].children.first == "#")

add_offense(node.loc.selector)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# frozen_string_literal: true

require 'rubocop'
require "rubocop"

module RuboCop
module Cop
module RubocopRailsAccessibility
class NoPositiveTabindex < Base
MSG = 'Positive tabindex is error-prone and often inaccessible.'
MSG = "Positive tabindex is error-prone and often inaccessible."

def on_send(node)
receiver, _method_name, *args = *node
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# frozen_string_literal: true

require 'rubocop'
require "rubocop"

module RuboCop
module Cop
module RubocopRailsAccessibility
class NoRedundantImageAlt < Base
MSG = 'Alt prop should not contain `image` or `picture` as screen readers already announce the element as an image'
MSG = "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

def_node_search :redundant_alt?, '(pair (sym :alt) (str #contains_redundant_alt_text?))'
def_node_search :redundant_alt?, "(pair (sym :alt) (str #contains_redundant_alt_text?))"

def on_send(node)
receiver, method_name, = *node
Expand Down
16 changes: 8 additions & 8 deletions lib/rubocop/cop/rubocop_rails_accessibility_cops.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

require 'pathname'
require 'yaml'
require "pathname"
require "yaml"

require_relative 'inject'
require_relative 'version'
require_relative 'rubocop-rails-accessibility/image_has_alt'
require_relative 'rubocop-rails-accessibility/link_has_href'
require_relative 'rubocop-rails-accessibility/no_positive_tabindex'
require_relative 'rubocop-rails-accessibility/no_redundant_image_alt'
require_relative "inject"
require_relative "version"
require_relative "rubocop-rails-accessibility/image_has_alt"
require_relative "rubocop-rails-accessibility/link_has_href"
require_relative "rubocop-rails-accessibility/no_positive_tabindex"
require_relative "rubocop-rails-accessibility/no_redundant_image_alt"
2 changes: 1 addition & 1 deletion lib/rubocop/cop/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module RubocopRailsAccessibility
VERSION = '0.1.0'
VERSION = "0.1.0"
end
41 changes: 23 additions & 18 deletions rubocop-rails-accessibility.gemspec
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
# frozen_string_literal: true

require_relative 'lib/rubocop/cop/version'
require_relative "lib/rubocop/cop/version"

Gem::Specification.new do |spec|
spec.name = 'rubocop-rails-accessibility'
spec.name = "rubocop-rails-accessibility"
spec.version = RubocopRailsAccessibility::VERSION
spec.authors = ['dummy']
spec.email = ['[email protected]']
spec.authors = ["dummy"]
spec.email = ["[email protected]"]

spec.summary = 'Custom extension for RuboCop.'
spec.homepage = 'https://github.com/dummy/rubocop-rails-accessibility'
spec.license = 'MIT'
spec.required_ruby_version = '>= 2.6.0'
spec.summary = "Custom extension for RuboCop."
spec.homepage = "https://github.com/dummy/rubocop-rails-accessibility"
spec.license = "MIT"
spec.required_ruby_version = ">= 2.7.0"

spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = spec.homepage
spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/main/CHANGELOG.md"
spec.metadata['rubygems_mfa_required'] = 'true'
spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = spec.homepage
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
spec.metadata["rubygems_mfa_required"] = "true"

spec.add_development_dependency 'actionview'
spec.add_development_dependency 'minitest'
spec.add_dependency "rubocop", ">= 1.0.0"

spec.add_development_dependency "actionview"
spec.add_development_dependency "minitest"
spec.add_development_dependency "rake", "~> 13.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "rubocop-github"
spec.add_development_dependency "rubocop-rails"
spec.add_development_dependency "rubocop-performance"

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
Expand All @@ -28,9 +35,7 @@ Gem::Specification.new do |spec|
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
end
end
spec.bindir = 'exe'
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_runtime_dependency 'rubocop'
spec.require_paths = ["lib"]
end
4 changes: 2 additions & 2 deletions test/cop_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'action_view'
require 'minitest'
require "action_view"
require "minitest"

class CopTest < MiniTest::Test
def cop_class
Expand Down
13 changes: 7 additions & 6 deletions test/test_image_has_alt.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
# frozen_string_literal: true

require_relative './cop_test'
require 'minitest/autorun'
require 'rubocop/cop/rubocop-rails-accessibility/image_has_alt'
require_relative "./cop_test"
require "minitest/autorun"
require "rubocop/cop/rubocop-rails-accessibility/image_has_alt"

class TestImageHasAlt < CopTest
def cop_class
RuboCop::Cop::RubocopRailsAccessibility::ImageHasAlt
end

def test_image_has_alt_offense
offenses = erb_investigate cop, <<-ERB, 'app/views/products/index.html.erb'
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
<%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12" %>
ERB

assert_equal 1, offenses.count
assert_equal 'Images should have an alt prop with meaningful text or an empty string for decorative images', offenses[0].message
assert_equal "Images should have an alt prop with meaningful text or an empty string for decorative images",
offenses[0].message
end

def test_image_has_alt_no_offense
offenses = erb_investigate cop, <<-ERB, 'app/views/products/index.html.erb'
offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb"
<%= image_tag "spinners/octocat-spinner-16px.gif", size: "12x12", alt: "GitHub Logo spinner" %>
ERB

Expand Down
Loading