From e8076680dec8e007be35b1e7f6b7476d1d7f59bf Mon Sep 17 00:00:00 2001 From: paydaylight Date: Thu, 4 Feb 2021 16:59:50 +0600 Subject: [PATCH] [Fix #220] changed namespace to DatarocketsStyle --- bin/console | 2 +- config/base.yml | 2 +- datarockets-style.gemspec | 4 +- lib/datarockets/style.rb | 14 ---- .../cop/layout/array_alignment_extended.rb | 83 ------------------- .../style/cop/style/nested_interpolation.rb | 37 --------- .../todo_list_formatter/report_summary.rb | 57 ------------- lib/datarockets/style/version.rb | 5 -- lib/datarockets_style.rb | 12 +++ .../cop/layout/array_alignment_extended.rb | 81 ++++++++++++++++++ .../cop/style/nested_interpolation.rb | 35 ++++++++ .../formatter/todo_list_formatter.rb | 2 +- .../todo_list_formatter/report_summary.rb | 55 ++++++++++++ lib/datarockets_style/version.rb | 3 + spec/datarockets/style/version_spec.rb | 7 -- .../layout/array_alignment_extended_spec.rb | 2 +- .../cop/style/nested_interpolation_spec.rb | 2 +- .../report_summary_spec.rb | 2 +- .../formatter/todo_list_formatter_spec.rb | 0 spec/datarockets_style/version_spec.rb | 7 ++ spec/spec_helper.rb | 2 +- 21 files changed, 202 insertions(+), 212 deletions(-) delete mode 100644 lib/datarockets/style.rb delete mode 100644 lib/datarockets/style/cop/layout/array_alignment_extended.rb delete mode 100644 lib/datarockets/style/cop/style/nested_interpolation.rb delete mode 100644 lib/datarockets/style/formatter/todo_list_formatter/report_summary.rb delete mode 100644 lib/datarockets/style/version.rb create mode 100644 lib/datarockets_style.rb create mode 100644 lib/datarockets_style/cop/layout/array_alignment_extended.rb create mode 100644 lib/datarockets_style/cop/style/nested_interpolation.rb rename lib/{datarockets/style => datarockets_style}/formatter/todo_list_formatter.rb (92%) create mode 100644 lib/datarockets_style/formatter/todo_list_formatter/report_summary.rb create mode 100644 lib/datarockets_style/version.rb delete mode 100644 spec/datarockets/style/version_spec.rb rename spec/{datarockets/style => datarockets_style}/cop/layout/array_alignment_extended_spec.rb (99%) rename spec/{datarockets/style => datarockets_style}/cop/style/nested_interpolation_spec.rb (96%) rename spec/{datarockets/style => datarockets_style}/formatter/todo_list_formatter/report_summary_spec.rb (91%) rename spec/{datarockets/style => datarockets_style}/formatter/todo_list_formatter_spec.rb (100%) create mode 100644 spec/datarockets_style/version_spec.rb diff --git a/bin/console b/bin/console index 1172402..dd6a980 100755 --- a/bin/console +++ b/bin/console @@ -1,7 +1,7 @@ #!/usr/bin/env ruby require "bundler/setup" -require "datarockets/style" +require "datarockets_style" # 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/base.yml b/config/base.yml index cff45b3..d67d749 100644 --- a/config/base.yml +++ b/config/base.yml @@ -1,4 +1,4 @@ -require: datarockets/style +require: datarockets_style AllCops: NewCops: enable diff --git a/datarockets-style.gemspec b/datarockets-style.gemspec index 69f1d1a..018531e 100644 --- a/datarockets-style.gemspec +++ b/datarockets-style.gemspec @@ -1,10 +1,10 @@ lib = File.expand_path("lib", __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require "datarockets/style/version" +require "datarockets_style/version" Gem::Specification.new do |spec| spec.name = "datarockets-style" - spec.version = Datarockets::Style::VERSION + spec.version = DatarocketsStyle::VERSION spec.authors = ["Roman Dubrovsky"] spec.email = ["r.dubrovsky@datarockets.com"] diff --git a/lib/datarockets/style.rb b/lib/datarockets/style.rb deleted file mode 100644 index dd80dcd..0000000 --- a/lib/datarockets/style.rb +++ /dev/null @@ -1,14 +0,0 @@ -require "rubocop" -require "datarockets/style/formatter/todo_list_formatter" - -require "datarockets/style/version" - -require "datarockets/style/cop/layout/array_alignment_extended" -require "datarockets/style/cop/style/nested_interpolation" - -module Datarockets - # Datarickors sharable config - module Style - # Your code goes here... - end -end diff --git a/lib/datarockets/style/cop/layout/array_alignment_extended.rb b/lib/datarockets/style/cop/layout/array_alignment_extended.rb deleted file mode 100644 index 9b32a4e..0000000 --- a/lib/datarockets/style/cop/layout/array_alignment_extended.rb +++ /dev/null @@ -1,83 +0,0 @@ -# frozen_string_literal: true - -module Datarockets - module Style - module Cop - module Layout - # Here we check if the elements of a multi-line array literal are - # aligned. - # - # @example EnforcedStyle: with_first_argument (default) - # # good - # - # array = [1, 2, 3, - # 4, 5, 6] - # array = ['run', - # 'forrest', - # 'run'] - # - # # bad - # - # array = [1, 2, 3, - # 4, 5, 6] - # array = ['run', - # 'forrest', - # 'run'] - # - # @example EnforcedStyle: with_fixed_indentation - # # good - # - # array = [1, 2, 3, - # 4, 5, 6] - # - # # bad - # - # array = [1, 2, 3, - # 4, 5, 6] - class ArrayAlignmentExtended < RuboCop::Cop::Cop - include RuboCop::Cop::Alignment - - ALIGN_PARAMS_MSG = "Align the elements of an array literal if they span more than one line." - - FIXED_INDENT_MSG = "Use one level of indentation for elements " \ - "following the first line of a multi-line array." - - def on_array(node) - return if node.children.size < 2 - - check_alignment(node.children, base_column(node, node.children)) - end - - def autocorrect(node) - RuboCop::Cop::AlignmentCorrector.correct(processed_source, node, column_delta) - end - - private - - def message(_node) - fixed_indentation? ? FIXED_INDENT_MSG : ALIGN_PARAMS_MSG - end - - def fixed_indentation? - cop_config["EnforcedStyle"] == "with_fixed_indentation" - end - - def base_column(node, args) - fixed_indentation? ? line_indentation(node) : display_column(args.first.source_range) - end - - def line_indentation(node) - lineno = target_method_lineno(node) - line = node.source_range.source_buffer.source_line(lineno) - line_indentation = /\S.*/.match(line).begin(0) - line_indentation + configured_indentation_width - end - - def target_method_lineno(node) - node.loc.line - end - end - end - end - end -end diff --git a/lib/datarockets/style/cop/style/nested_interpolation.rb b/lib/datarockets/style/cop/style/nested_interpolation.rb deleted file mode 100644 index f79a2c3..0000000 --- a/lib/datarockets/style/cop/style/nested_interpolation.rb +++ /dev/null @@ -1,37 +0,0 @@ -module Datarockets - module Style - module Cop - module Style - # This cop checks nested interpolations - # - # @example - # - # # bad - # "Hello, #{user.blank? ? 'guest' : "dear #{user.name}"}" - # - # # good - # user_name = user.blank? ? 'guest' : "dear #{user.name}" - # "Hello, #{user_name}" - class NestedInterpolation < RuboCop::Cop::Cop - include RuboCop::Cop::Interpolation - - MSG = "Redundant nested interpolation.".freeze - - def on_interpolation(node) - node.each_descendant(:dstr) do |descendant_node| - detect_double_interpolation(descendant_node) - end - end - - private - - def detect_double_interpolation(node) - node.each_child_node(:begin) do |begin_node| - add_offense(begin_node) - end - end - end - end - end - end -end diff --git a/lib/datarockets/style/formatter/todo_list_formatter/report_summary.rb b/lib/datarockets/style/formatter/todo_list_formatter/report_summary.rb deleted file mode 100644 index 7547ee1..0000000 --- a/lib/datarockets/style/formatter/todo_list_formatter/report_summary.rb +++ /dev/null @@ -1,57 +0,0 @@ -module Datarockets - module Style - module Formatter - module TodoListFormatter - # Get file of pairs: file path and cop name - and prepare report for ToDo list formatter. - # - # Example of result: - # - # LineLength - # Exclude: - # - "really/bad/file.rb" # 100500 - # - "almost/ok.rb" # 1 - class ReportSummary - attr_reader :offense_list - - FileGroup = Struct.new(:file, :offenses_count) do - def print(output) - output.puts " - '#{file}' # #{offenses_count}" - end - end - - OffenseGroup = Struct.new(:cop_name, :offenses) do - def file_groups - @_file_groups ||= offenses.group_by(&:file_path).map do |file, offenses| - FileGroup.new(file, offenses.length) - end - end - - def print(output) - output.puts("#{cop_name}:") - output.puts(" Exclude:") - file_groups.sort_by(&:file).each do |file_group| - file_group.print(output) - end - output.puts - end - end - - def initialize(offense_list) - @offense_list = offense_list - end - - def call(output) - offense_groups.sort_by(&:cop_name).each { |group| group.print(output) } - end - - private - - def offense_groups - @_offense_groups ||= offense_list.group_by(&:cop_name) - .map { |cop_name, offenses| OffenseGroup.new(cop_name, offenses) } - end - end - end - end - end -end diff --git a/lib/datarockets/style/version.rb b/lib/datarockets/style/version.rb deleted file mode 100644 index 2b92bb2..0000000 --- a/lib/datarockets/style/version.rb +++ /dev/null @@ -1,5 +0,0 @@ -module Datarockets - module Style - VERSION = "1.0.0".freeze - end -end diff --git a/lib/datarockets_style.rb b/lib/datarockets_style.rb new file mode 100644 index 0000000..3351789 --- /dev/null +++ b/lib/datarockets_style.rb @@ -0,0 +1,12 @@ +require "rubocop" +require "datarockets_style/formatter/todo_list_formatter" + +require "datarockets_style/version" + +require "datarockets_style/cop/layout/array_alignment_extended" +require "datarockets_style/cop/style/nested_interpolation" + +# Top level module for datarockets-style +module DatarocketsStyle + # Datarickors sharable config +end diff --git a/lib/datarockets_style/cop/layout/array_alignment_extended.rb b/lib/datarockets_style/cop/layout/array_alignment_extended.rb new file mode 100644 index 0000000..4c7b899 --- /dev/null +++ b/lib/datarockets_style/cop/layout/array_alignment_extended.rb @@ -0,0 +1,81 @@ +# frozen_string_literal: true + +module DatarocketsStyle + module Cop + module Layout + # Here we check if the elements of a multi-line array literal are + # aligned. + # + # @example EnforcedStyle: with_first_argument (default) + # # good + # + # array = [1, 2, 3, + # 4, 5, 6] + # array = ['run', + # 'forrest', + # 'run'] + # + # # bad + # + # array = [1, 2, 3, + # 4, 5, 6] + # array = ['run', + # 'forrest', + # 'run'] + # + # @example EnforcedStyle: with_fixed_indentation + # # good + # + # array = [1, 2, 3, + # 4, 5, 6] + # + # # bad + # + # array = [1, 2, 3, + # 4, 5, 6] + class ArrayAlignmentExtended < RuboCop::Cop::Cop + include RuboCop::Cop::Alignment + + ALIGN_PARAMS_MSG = "Align the elements of an array literal if they span more than one line." + + FIXED_INDENT_MSG = "Use one level of indentation for elements " \ + "following the first line of a multi-line array." + + def on_array(node) + return if node.children.size < 2 + + check_alignment(node.children, base_column(node, node.children)) + end + + def autocorrect(node) + RuboCop::Cop::AlignmentCorrector.correct(processed_source, node, column_delta) + end + + private + + def message(_node) + fixed_indentation? ? FIXED_INDENT_MSG : ALIGN_PARAMS_MSG + end + + def fixed_indentation? + cop_config["EnforcedStyle"] == "with_fixed_indentation" + end + + def base_column(node, args) + fixed_indentation? ? line_indentation(node) : display_column(args.first.source_range) + end + + def line_indentation(node) + lineno = target_method_lineno(node) + line = node.source_range.source_buffer.source_line(lineno) + line_indentation = /\S.*/.match(line).begin(0) + line_indentation + configured_indentation_width + end + + def target_method_lineno(node) + node.loc.line + end + end + end + end +end diff --git a/lib/datarockets_style/cop/style/nested_interpolation.rb b/lib/datarockets_style/cop/style/nested_interpolation.rb new file mode 100644 index 0000000..9dbc391 --- /dev/null +++ b/lib/datarockets_style/cop/style/nested_interpolation.rb @@ -0,0 +1,35 @@ +module DatarocketsStyle + module Cop + module Style + # This cop checks nested interpolations + # + # @example + # + # # bad + # "Hello, #{user.blank? ? 'guest' : "dear #{user.name}"}" + # + # # good + # user_name = user.blank? ? 'guest' : "dear #{user.name}" + # "Hello, #{user_name}" + class NestedInterpolation < RuboCop::Cop::Cop + include RuboCop::Cop::Interpolation + + MSG = "Redundant nested interpolation.".freeze + + def on_interpolation(node) + node.each_descendant(:dstr) do |descendant_node| + detect_double_interpolation(descendant_node) + end + end + + private + + def detect_double_interpolation(node) + node.each_child_node(:begin) do |begin_node| + add_offense(begin_node) + end + end + end + end + end +end diff --git a/lib/datarockets/style/formatter/todo_list_formatter.rb b/lib/datarockets_style/formatter/todo_list_formatter.rb similarity index 92% rename from lib/datarockets/style/formatter/todo_list_formatter.rb rename to lib/datarockets_style/formatter/todo_list_formatter.rb index 6876cb8..e44426f 100644 --- a/lib/datarockets/style/formatter/todo_list_formatter.rb +++ b/lib/datarockets_style/formatter/todo_list_formatter.rb @@ -46,6 +46,6 @@ def finished(inspected_files) @total_correctable_count) output.puts - Datarockets::Style::Formatter::TodoListFormatter::ReportSummary.new(offense_list).call(output) + DatarocketsStyle::Formatter::TodoListFormatter::ReportSummary.new(offense_list).call(output) end end diff --git a/lib/datarockets_style/formatter/todo_list_formatter/report_summary.rb b/lib/datarockets_style/formatter/todo_list_formatter/report_summary.rb new file mode 100644 index 0000000..3e12199 --- /dev/null +++ b/lib/datarockets_style/formatter/todo_list_formatter/report_summary.rb @@ -0,0 +1,55 @@ +module DatarocketsStyle + module Formatter + module TodoListFormatter + # Get file of pairs: file path and cop name - and prepare report for ToDo list formatter. + # + # Example of result: + # + # LineLength + # Exclude: + # - "really/bad/file.rb" # 100500 + # - "almost/ok.rb" # 1 + class ReportSummary + attr_reader :offense_list + + FileGroup = Struct.new(:file, :offenses_count) do + def print(output) + output.puts " - '#{file}' # #{offenses_count}" + end + end + + OffenseGroup = Struct.new(:cop_name, :offenses) do + def file_groups + @_file_groups ||= offenses.group_by(&:file_path).map do |file, offenses| + FileGroup.new(file, offenses.length) + end + end + + def print(output) + output.puts("#{cop_name}:") + output.puts(" Exclude:") + file_groups.sort_by(&:file).each do |file_group| + file_group.print(output) + end + output.puts + end + end + + def initialize(offense_list) + @offense_list = offense_list + end + + def call(output) + offense_groups.sort_by(&:cop_name).each { |group| group.print(output) } + end + + private + + def offense_groups + @_offense_groups ||= offense_list.group_by(&:cop_name) + .map { |cop_name, offenses| OffenseGroup.new(cop_name, offenses) } + end + end + end + end +end diff --git a/lib/datarockets_style/version.rb b/lib/datarockets_style/version.rb new file mode 100644 index 0000000..fa8df35 --- /dev/null +++ b/lib/datarockets_style/version.rb @@ -0,0 +1,3 @@ +module DatarocketsStyle + VERSION = "1.0.0".freeze +end diff --git a/spec/datarockets/style/version_spec.rb b/spec/datarockets/style/version_spec.rb deleted file mode 100644 index 04b8d82..0000000 --- a/spec/datarockets/style/version_spec.rb +++ /dev/null @@ -1,7 +0,0 @@ -RSpec.describe "Datarockets::Style::Version" do - describe "::VERSION" do - it "contains the current version" do - expect(Datarockets::Style::VERSION).not_to be nil - end - end -end diff --git a/spec/datarockets/style/cop/layout/array_alignment_extended_spec.rb b/spec/datarockets_style/cop/layout/array_alignment_extended_spec.rb similarity index 99% rename from spec/datarockets/style/cop/layout/array_alignment_extended_spec.rb rename to spec/datarockets_style/cop/layout/array_alignment_extended_spec.rb index 8693ae9..60a441e 100644 --- a/spec/datarockets/style/cop/layout/array_alignment_extended_spec.rb +++ b/spec/datarockets_style/cop/layout/array_alignment_extended_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe Datarockets::Style::Cop::Layout::ArrayAlignmentExtended, skip: true do +RSpec.describe DatarocketsStyle::Cop::Layout::ArrayAlignmentExtended, skip: true do subject(:cop) { described_class.new(config) } let(:config) do diff --git a/spec/datarockets/style/cop/style/nested_interpolation_spec.rb b/spec/datarockets_style/cop/style/nested_interpolation_spec.rb similarity index 96% rename from spec/datarockets/style/cop/style/nested_interpolation_spec.rb rename to spec/datarockets_style/cop/style/nested_interpolation_spec.rb index 29fa321..4300d3d 100644 --- a/spec/datarockets/style/cop/style/nested_interpolation_spec.rb +++ b/spec/datarockets_style/cop/style/nested_interpolation_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe Datarockets::Style::Cop::Style::NestedInterpolation do +RSpec.describe DatarocketsStyle::Cop::Style::NestedInterpolation do subject(:cop) { described_class.new } it "accepts single interpolation" do diff --git a/spec/datarockets/style/formatter/todo_list_formatter/report_summary_spec.rb b/spec/datarockets_style/formatter/todo_list_formatter/report_summary_spec.rb similarity index 91% rename from spec/datarockets/style/formatter/todo_list_formatter/report_summary_spec.rb rename to spec/datarockets_style/formatter/todo_list_formatter/report_summary_spec.rb index db13c13..c09bec0 100644 --- a/spec/datarockets/style/formatter/todo_list_formatter/report_summary_spec.rb +++ b/spec/datarockets_style/formatter/todo_list_formatter/report_summary_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe Datarockets::Style::Formatter::TodoListFormatter::ReportSummary do +RSpec.describe DatarocketsStyle::Formatter::TodoListFormatter::ReportSummary do subject(:report_offenses) { described_class.new(offense_list).call(output) } let(:output) { StringIO.new } diff --git a/spec/datarockets/style/formatter/todo_list_formatter_spec.rb b/spec/datarockets_style/formatter/todo_list_formatter_spec.rb similarity index 100% rename from spec/datarockets/style/formatter/todo_list_formatter_spec.rb rename to spec/datarockets_style/formatter/todo_list_formatter_spec.rb diff --git a/spec/datarockets_style/version_spec.rb b/spec/datarockets_style/version_spec.rb new file mode 100644 index 0000000..a5969f6 --- /dev/null +++ b/spec/datarockets_style/version_spec.rb @@ -0,0 +1,7 @@ +RSpec.describe "DatarocketsStyle::Version" do + describe "::VERSION" do + it "contains the current version" do + expect(DatarocketsStyle::VERSION).not_to be nil + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bf1ecdf..cd9bf00 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,4 @@ -require "datarockets/style" +require "datarockets_style" require "rubocop/rspec/support" require "pry"