Skip to content

Commit

Permalink
Improve rubocop setup (#1139)
Browse files Browse the repository at this point in the history
* Rename rake rubocop to rake format_generated_files

* Add rubocop rules to ensure spaces are applied consistently

* Improve rubocop related CI workflows
  • Loading branch information
st0012 committed Jul 17, 2024
1 parent 8a5a4ae commit 27932d0
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 23 deletions.
12 changes: 12 additions & 0 deletions .generated_files_rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This rubocop configuration is ONLY for the generated files (listed in Rakefile). It is not meant to be used for RDoc's
# source code.
# The purpose of this file is to ensure the generated files don't have trailing whitespace or empty lines, which could
# be a problem for ruby/ruby's CI
AllCops:
TargetRubyVersion: 3.3
DisabledByDefault: true

Layout/TrailingWhitespace:
Enabled: true
Layout/TrailingEmptyLines:
Enabled: true
16 changes: 14 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,17 @@ jobs:
RUBYOPT: --enable-frozen_string_literal
- if: ${{ matrix.ruby == 'head' && startsWith(matrix.os, 'ubuntu') }}
run: bundle exec rake rdoc
- if: ${{ matrix.ruby == 'head' && startsWith(matrix.os, 'ubuntu') }}
run: bundle exec rake rubocop
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: "3.3"
bundler-cache: true
- name: Run rubocop
run: bundle exec rubocop
# Just to make sure the format_generated_files task is working
- name: Sanity check for the format_generated_files task
run: bundle exec rake generate format_generated_files
15 changes: 12 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
AllCops:
TargetRubyVersion: 2.6
TargetRubyVersion: 3.0
DisabledByDefault: true
Exclude:
- rdoc.gemspec
SuggestExtensions: false

Layout/TrailingWhitespace:
Enabled: true

Layout/TrailingEmptyLines:
Enabled: true

Layout/SpaceAroundKeyword:
Enabled: true

Layout/SpaceBeforeComma:
Enabled: true

Layout/SpaceAfterComma:
Enabled: true
6 changes: 3 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ begin
require 'rubocop/rake_task'
rescue LoadError
else
RuboCop::RakeTask.new(:rubocop) do |t|
t.options = [*parsed_files]
RuboCop::RakeTask.new(:format_generated_files) do |t|
t.options = parsed_files + ["--config=.generated_files_rubocop.yml"]
end
task :build => [:generate, "rubocop:autocorrect"]
task :build => [:generate, "format_generated_files:autocorrect"]
end
2 changes: 1 addition & 1 deletion lib/rdoc/code_object/alias.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def full_old_name
# HTML id-friendly version of +#new_name+.

def html_name
CGI.escape(@new_name.gsub('-', '-2D')).gsub('%','-').sub(/^-/, '')
CGI.escape(@new_name.gsub('-', '-2D')).gsub('%', '-').sub(/^-/, '')
end

def inspect # :nodoc:
Expand Down
6 changes: 3 additions & 3 deletions lib/rdoc/code_object/method_attr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ def block_params=(value)
when 'const_get' then 'const'
when 'new' then
$1.split('::').last. # ClassName => class_name
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
gsub(/([a-z\d])([A-Z])/, '\1_\2').
downcase
else
$2
Expand All @@ -291,7 +291,7 @@ def block_params=(value)
def html_name
require 'cgi/util'

CGI.escape(@name.gsub('-', '-2D')).gsub('%','-').sub(/^-/, '')
CGI.escape(@name.gsub('-', '-2D')).gsub('%', '-').sub(/^-/, '')
end

##
Expand Down
8 changes: 4 additions & 4 deletions lib/rdoc/code_object/top_level.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ def inspect # :nodoc:
"#<%s:0x%x %p modules: %p classes: %p>" % [
self.class, object_id,
base_name,
@modules.map { |n,m| m },
@classes.map { |n,c| c }
@modules.map { |n, m| m },
@classes.map { |n, c| c }
]
end

Expand Down Expand Up @@ -254,8 +254,8 @@ def pretty_print q # :nodoc:
q.text "base name: #{base_name.inspect}"
q.breakable

items = @modules.map { |n,m| m }
items.concat @modules.map { |n,c| c }
items = @modules.map { |n, m| m }
items.concat @modules.map { |n, c| c }
q.seplist items do |mod| q.pp mod end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/generator/pot/message_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def extract_from_klass klass
extract_text(klass.comment_location, klass.full_name)

klass.each_section do |section, constants, attributes|
extract_text(section.title ,"#{klass.full_name}: section title")
extract_text(section.title, "#{klass.full_name}: section title")
section.comments.each do |comment|
extract_text(comment, "#{klass.full_name}: #{section.title}")
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rdoc/markup/attribute_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def unmask_protected_sequences

def add_word_pair(start, stop, name, exclusive = false)
raise ArgumentError, "Word flags may not start with '<'" if
start[0,1] == '<'
start[0, 1] == '<'

bitmap = @attributes.bitmap_for name

Expand All @@ -271,7 +271,7 @@ def add_word_pair(start, stop, name, exclusive = false)
@word_pair_map[pattern] = bitmap
end

@protectable << start[0,1]
@protectable << start[0, 1]
@protectable.uniq!

@exclusive_bitmap |= bitmap if exclusive
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/markup/to_bs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def initialize markup = nil
def init_tags
add_tag :BOLD, '+b', '-b'
add_tag :EM, '+_', '-_'
add_tag :TT, '' , '' # we need in_tt information maintained
add_tag :TT, '', '' # we need in_tt information maintained
end

##
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ def parse argv
EOF

parsers = Hash.new { |h,parser| h[parser] = [] }
parsers = Hash.new { |h, parser| h[parser] = [] }

RDoc::Parser.parsers.each do |regexp, parser|
parsers[parser.name.sub('RDoc::Parser::', '')] << regexp.source
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/parser/c.rb
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def do_constants
# Scans #content for rb_include_module

def do_includes
@content.scan(/rb_include_module\s*\(\s*(\w+?),\s*(\w+?)\s*\)/) do |c,m|
@content.scan(/rb_include_module\s*\(\s*(\w+?),\s*(\w+?)\s*\)/) do |c, m|
next unless cls = @classes[c]
m = @known_classes[m] || m

Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/parser/changelog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class << self; prepend Git; end

entries << [entry_name, entry_body] if entry_name

entries.reject! do |(entry,_)|
entries.reject! do |(entry, _)|
entry == nil
end

Expand Down

0 comments on commit 27932d0

Please sign in to comment.