diff --git a/.github/workflows/rubocop.yml b/.github/workflows/rubocop.yml index 671f7676..8eb2c070 100644 --- a/.github/workflows/rubocop.yml +++ b/.github/workflows/rubocop.yml @@ -9,11 +9,17 @@ on: - ".rubocop*.yml" - ".github/workflows/rubocop.yml" - "**/*.rb" + - "**/*.gemspec" + - "**/*.gemfile" + - "Gemfile" pull_request: paths: - ".rubocop*.yml" - ".github/workflows/rubocop.yml" - "**/*.rb" + - "**/*.gemspec" + - "**/*.gemfile" + - "Gemfile" jobs: formatting-check: @@ -28,12 +34,10 @@ jobs: uses: ruby/setup-ruby@v1 with: ruby-version: "2.7" - - - name: Install rubocop - run: gem install rubocop --no-doc + bundler-cache: true - name: Set-up RuboCop Problem Matcher uses: r7kamura/rubocop-problem-matchers-action@v1 - name: Run rubocop - run: rubocop + run: bundle exec rubocop diff --git a/.rubocop.yml b/.rubocop.yml index 6e46f13a..a487a402 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -4,6 +4,9 @@ AllCops: TargetRubyVersion: 2.7 NewCops: enable +Layout/SpaceAroundEqualsInParameterDefault: + EnforcedStyle: no_space + Naming/MethodName: EnforcedStyle: snake_case Exclude: diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 4ed89c38..06b1219a 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -16,86 +16,6 @@ Layout/MultilineHashBraceLayout: - 'test/sample/file.rb' - 'test/tests.rb' -# Offense count: 26 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: space, no_space -Layout/SpaceAroundEqualsInParameterDefault: - Exclude: - - 'lib/ox/bag.rb' - - 'lib/ox/document.rb' - - 'lib/ox/element.rb' - - 'test/ox/change.rb' - - 'test/ox/doc.rb' - - 'test/ox/oval.rb' - - 'test/ox/rect.rb' - - 'test/ox/shape.rb' - - 'test/ox/text.rb' - - 'test/perf_mars.rb' - - 'test/sample.rb' - - 'test/sample/change.rb' - - 'test/sample/doc.rb' - - 'test/sample/oval.rb' - - 'test/sample/rect.rb' - - 'test/sample/shape.rb' - - 'test/sample/text.rb' - - 'test/tests.rb' - -# Offense count: 10 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowForAlignment, EnforcedStyleForExponentOperator. -# SupportedStylesForExponentOperator: space, no_space -Layout/SpaceAroundOperators: - Exclude: - - 'contrib/xbuilder_test.rb' - - 'lib/ox/element.rb' - - 'test/perf_mars.rb' - - 'test/sax/sax_test.rb' - - 'test/tests.rb' - -# Offense count: 4 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters. -# SupportedStyles: space, no_space -# SupportedStylesForEmptyBraces: space, no_space -Layout/SpaceInsideBlockBraces: - Exclude: - - 'contrib/xbuilder.rb' - - 'test/perf.rb' - - 'test/sax/smart_test.rb' - - 'test/tests.rb' - -# Offense count: 106 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces. -# SupportedStyles: space, no_space, compact -# SupportedStylesForEmptyBraces: space, no_space -Layout/SpaceInsideHashLiteralBraces: - Exclude: - - 'contrib/xbuilder_test.rb' - - 'examples/obj.rb' - - 'lib/ox/bag.rb' - - 'lib/ox/document.rb' - - 'lib/ox/hasattrs.rb' - - 'test/ox/doc.rb' - - 'test/ox/hasprops.rb' - - 'test/perf_gen.rb' - - 'test/perf_mars.rb' - - 'test/sample/doc.rb' - - 'test/sample/file.rb' - - 'test/sample/hasprops.rb' - - 'test/sax/sax_test.rb' - - 'test/sax/smart_test.rb' - - 'test/tests.rb' - -# Offense count: 12 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: space, compact, no_space -Layout/SpaceInsideParens: - Exclude: - - 'test/tests.rb' - # Offense count: 2 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: AllowSafeAssignment. diff --git a/contrib/sax_benchmark.rb b/contrib/sax_benchmark.rb index a3ce7f90..99298a87 100644 --- a/contrib/sax_benchmark.rb +++ b/contrib/sax_benchmark.rb @@ -93,7 +93,7 @@ def end_element(key) end end - def start_element(key, attrs = []) + def start_element(key, attrs=[]) @stack << @node = {} attrs.each do |attr| key, val = *attr diff --git a/contrib/xbuilder.rb b/contrib/xbuilder.rb index 4fb7c0cc..15f5f0c1 100644 --- a/contrib/xbuilder.rb +++ b/contrib/xbuilder.rb @@ -16,7 +16,7 @@ def x(name, *args) when Hash arg.each { |k, v| n[k.to_s] = v } when Array - arg.each { |c| n << c if c} + arg.each { |c| n << c if c } else n << arg if arg end diff --git a/contrib/xbuilder_test.rb b/contrib/xbuilder_test.rb index 823db6c7..79ed23ab 100755 --- a/contrib/xbuilder_test.rb +++ b/contrib/xbuilder_test.rb @@ -32,13 +32,13 @@ def test_build x_if(false, 'child5') ] - n=x('parent', - x('child1', {'atr1'=>'atr1_value', :atr2 => 'atr2_value'}, - x('subchild1', 'some text'), - x('subchild2', {'atr3' => 'default_value'}, {'atr3' => 'atr3_value'}, - x('sometag'), - x_if(false, 'never'))), - children) + n = x('parent', + x('child1', { 'atr1' => 'atr1_value', :atr2 => 'atr2_value' }, + x('subchild1', 'some text'), + x('subchild2', { 'atr3' => 'default_value' }, { 'atr3' => 'atr3_value' }, + x('sometag'), + x_if(false, 'never'))), + children) assert_equal(Ox.dump(n), Ox.dump(Ox.parse(xml))) end diff --git a/examples/obj.rb b/examples/obj.rb index 17264d4a..96a49e4f 100755 --- a/examples/obj.rb +++ b/examples/obj.rb @@ -21,7 +21,7 @@ def to_s end end -obj = Classy.new(23, ['abc', {x: true}]) +obj = Classy.new(23, ['abc', { x: true }]) doc = Ox.dump(obj, mode: :object) diff --git a/examples/sax_ractor.rb b/examples/sax_ractor.rb index aa3d7b0e..0b717323 100755 --- a/examples/sax_ractor.rb +++ b/examples/sax_ractor.rb @@ -18,7 +18,7 @@ class Saxtor < Ox::Sax # yield it if its `ietf` matches our `needle`, # and throw it away otherwise. CYO = Struct.new(:ietf, :globs, :description) do - def initialize(ietf = nil, globs = [], description = nil) + def initialize(ietf=nil, globs=[], description=nil) super(ietf, globs, description) end diff --git a/lib/ox/bag.rb b/lib/ox/bag.rb index f6fb13c8..e6c29f17 100644 --- a/lib/ox/bag.rb +++ b/lib/ox/bag.rb @@ -14,7 +14,7 @@ class Bag # # Ox::Bag.new(:@x => 42, :@y => 57) # - def initialize(args={ }) + def initialize(args={}) args.each do |k, v| instance_variable_set(k, v) end diff --git a/lib/ox/document.rb b/lib/ox/document.rb index e86ded65..3e11062f 100644 --- a/lib/ox/document.rb +++ b/lib/ox/document.rb @@ -9,7 +9,7 @@ class Document < Element # - _:standalone_ [String] indicates the document is standalone def initialize(prolog={}) super(nil) - @attributes = { } + @attributes = {} @attributes[:version] = prolog[:version] unless prolog[:version].nil? @attributes[:encoding] = prolog[:encoding] unless prolog[:encoding].nil? @attributes[:standalone] = prolog[:standalone] unless prolog[:standalone].nil? diff --git a/lib/ox/element.rb b/lib/ox/element.rb index bc2069e5..11334b0c 100644 --- a/lib/ox/element.rb +++ b/lib/ox/element.rb @@ -269,7 +269,7 @@ def alocate(path, found) name = step qual = nil else - name = step[0..i-1] + name = step[0..i - 1] raise InvalidPath.new(path) unless step.end_with?(']') i += 1 @@ -350,7 +350,7 @@ def del_locate(path) name = step qual = nil else - name = step[0..i-1] + name = step[0..i - 1] raise InvalidPath.new(path) unless step.end_with?(']') i += 1 diff --git a/lib/ox/hasattrs.rb b/lib/ox/hasattrs.rb index fed21d4b..c62f757d 100644 --- a/lib/ox/hasattrs.rb +++ b/lib/ox/hasattrs.rb @@ -8,7 +8,7 @@ module HasAttrs # Returns all the attributes of the Instruct as a Hash. # *return* [Hash] all attributes and attribute values. def attributes - @attributes = { } if !instance_variable_defined?(:@attributes) or @attributes.nil? + @attributes = {} if !instance_variable_defined?(:@attributes) or @attributes.nil? @attributes end @@ -26,7 +26,7 @@ def [](attr) def []=(attr, value) raise 'argument to [] must be a Symbol or a String.' unless attr.is_a?(Symbol) or attr.is_a?(String) - @attributes = { } if !instance_variable_defined?(:@attributes) or @attributes.nil? + @attributes = {} if !instance_variable_defined?(:@attributes) or @attributes.nil? a_str = attr.to_s a_sym = attr.to_sym if @attributes.has_key?(a_str) diff --git a/test/ox/doc.rb b/test/ox/doc.rb index 73812720..7eb5c36c 100644 --- a/test/ox/doc.rb +++ b/test/ox/doc.rb @@ -24,7 +24,7 @@ def initialize(title) @title = title @user = ENV['USER'] @create_time = Time.now - @layers = { } + @layers = {} @change_history = [] end diff --git a/test/ox/hasprops.rb b/test/ox/hasprops.rb index e86b0e14..c85fb75f 100644 --- a/test/ox/hasprops.rb +++ b/test/ox/hasprops.rb @@ -2,12 +2,12 @@ module Test module Ox module HasProps def add_prop(key, value) - @props = { } unless instance_variable_defined?(:@props) + @props = {} unless instance_variable_defined?(:@props) @props[key] = value end def props - @props = { } unless instance_variable_defined?(:@props) + @props = {} unless instance_variable_defined?(:@props) @props end end # HashProps diff --git a/test/perf.rb b/test/perf.rb index 64848706..d1d981c8 100644 --- a/test/perf.rb +++ b/test/perf.rb @@ -17,7 +17,7 @@ def before(title, &blk) end def run(iter) - base = Item.new(nil, nil) { } + base = Item.new(nil, nil) {} base.run(iter, 0.0) @items.each do |i| i.run(iter, base.duration) diff --git a/test/perf_gen.rb b/test/perf_gen.rb index 501b3065..11f41ca8 100755 --- a/test/perf_gen.rb +++ b/test/perf_gen.rb @@ -55,7 +55,7 @@ opts.on('-h', '--help', 'Show this display') { puts opts; Process.exit!(0) } files = opts.parse(ARGV) -Ox.default_options = {mode: :generic} +Ox.default_options = { mode: :generic } data = [] diff --git a/test/perf_mars.rb b/test/perf_mars.rb index 192d9fd6..0251265d 100755 --- a/test/perf_mars.rb +++ b/test/perf_mars.rb @@ -61,10 +61,10 @@ def initialize(v=[]) data[:Symbol].values << "Symbol#{i}".to_sym data[:Time].values << (Time.now + i) data[:Array].values << [] - data[:Hash].values << { } + data[:Hash].values << {} data[:Range].values << (0..7) data[:Regexp].values << /^[0-9]/ - data[:Bignum].values << (7 ** 55) + data[:Bignum].values << (7**55) data[:Complex].values << Complex(1, 2) data[:Rational].values << Rational(1, 3) data[:Struct].values << s.new(1, 3, 5) diff --git a/test/perf_sax.rb b/test/perf_sax.rb index 4615c630..c784b754 100755 --- a/test/perf_sax.rb +++ b/test/perf_sax.rb @@ -117,7 +117,7 @@ def warning(message); end class NoAllSax < NoSax - def start_element(name, attrs = []); end + def start_element(name, attrs=[]); end def characters(text); end def cdata_block(string); end def comment(string); end diff --git a/test/sample/doc.rb b/test/sample/doc.rb index d51473d0..d47e2a91 100644 --- a/test/sample/doc.rb +++ b/test/sample/doc.rb @@ -23,7 +23,7 @@ def initialize(title) @title = title @user = ENV['USER'] @create_time = Time.now - @layers = { } + @layers = {} @change_history = [] end diff --git a/test/sample/file.rb b/test/sample/file.rb index 46dc6d1f..bedc7c64 100644 --- a/test/sample/file.rb +++ b/test/sample/file.rb @@ -17,15 +17,15 @@ def initialize(filename) 'user' => { 'read' => (0 != (stat.mode & 0x0100)), 'write' => (0 != (stat.mode & 0x0080)), - 'execute' => (0 != (stat.mode & 0x0040))}, + 'execute' => (0 != (stat.mode & 0x0040)) }, 'group' => { 'read' => (0 != (stat.mode & 0x0020)), 'write' => (0 != (stat.mode & 0x0010)), - 'execute' => (0 != (stat.mode & 0x0008))}, + 'execute' => (0 != (stat.mode & 0x0008)) }, 'other' => { 'read' => (0 != (stat.mode & 0x0004)), 'write' => (0 != (stat.mode & 0x0002)), - 'execute' => (0 != (stat.mode & 0x0001))} + 'execute' => (0 != (stat.mode & 0x0001)) } } else @permissions = { diff --git a/test/sample/hasprops.rb b/test/sample/hasprops.rb index 2833b7d6..090bfe0c 100644 --- a/test/sample/hasprops.rb +++ b/test/sample/hasprops.rb @@ -1,12 +1,12 @@ module Sample module HasProps def add_prop(key, value) - @props = { } unless instance_variable_defined?(:@props) + @props = {} unless instance_variable_defined?(:@props) @props[key] = value end def props - @props = { } unless instance_variable_defined?(:@props) + @props = {} unless instance_variable_defined?(:@props) @props end end # HasProps diff --git a/test/sax/helpers.rb b/test/sax/helpers.rb index 763e643a..d547b38b 100644 --- a/test/sax/helpers.rb +++ b/test/sax/helpers.rb @@ -15,7 +15,7 @@ module SaxTestHelpers # [:end_element, :top] # ], AllSax, :convert_special => true, :smart => true) # - def parse_compare(xml, expected, handler_class = AllSax, opts = {}, handler_attr = :calls) + def parse_compare(xml, expected, handler_class=AllSax, opts={}, handler_attr=:calls) handler = handler_class.new input = StringIO.new(xml) options = { diff --git a/test/sax/sax_test.rb b/test/sax/sax_test.rb index 93c6cde2..0934cc1d 100755 --- a/test/sax/sax_test.rb +++ b/test/sax/sax_test.rb @@ -1276,7 +1276,7 @@ def test_sax_html_inactive Ox.default_options = $ox_sax_options handler = AllSax.new - Ox.sax_html(handler, '
Hello
', :overlay => {'hr'=>:inactive}) + Ox.sax_html(handler, 'Hello
', :overlay => { 'hr' => :inactive }) assert_equal([ [:start_element, :html], [:start_element, :h1], diff --git a/test/sax/smart_test.rb b/test/sax/smart_test.rb index 0e69f458..42f7fef9 100755 --- a/test/sax/smart_test.rb +++ b/test/sax/smart_test.rb @@ -40,7 +40,7 @@ class SaxSmartTest < Test::Unit::TestCase 'cite' => {}, 'colgroup' => { 'parents' => ['table'], 'childs' => ['col'] }, 'data' => {}, - 'datalist' => { 'childs' => ['option']}, + 'datalist' => { 'childs' => ['option'] }, 'dl' => { 'childs' => ['dt', 'dd'] }, 'dt' => { 'parents' => ['dl'] }, 'dd' => { 'parents' => ['dl'] }, @@ -136,7 +136,7 @@ class SaxSmartTest < Test::Unit::TestCase } # Make the :smart => true option the default one - def smart_parse_compare(xml, expected, handler = AllSax, opts = {}, handler_attr = :calls) + def smart_parse_compare(xml, expected, handler=AllSax, opts={}, handler_attr=:calls) parse_compare(xml, expected, handler, opts.merge(:smart => true, :skip => :skip_white), handler_attr) end end @@ -155,7 +155,7 @@ def parents_of(el) NORMALELEMENTS[el]['parents'] || ['body'] end - def ancestry_of(el, parent = nil) + def ancestry_of(el, parent=nil) p = parent || parents_of(el)[0] [el] + (p ? ancestry_of(p) : []) end @@ -357,7 +357,7 @@ def test_comment_active