sevencop 0.15.0 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +19 -0
- data/Gemfile.lock +1 -1
- data/README.md +9 -25
- data/config/default.yml +15 -0
- data/lib/rubocop/cop/sevencop/autoload_ordered.rb +21 -11
- data/lib/rubocop/cop/sevencop/method_definition_arguments_multiline.rb +4 -1
- data/lib/rubocop/cop/sevencop/method_definition_ordered.rb +9 -35
- data/lib/rubocop/cop/sevencop/rails_inferred_spec_type.rb +4 -1
- data/lib/rubocop/cop/sevencop/require_ordered.rb +94 -0
- data/lib/rubocop/cop/sevencop/rspec_examples_in_same_group.rb +77 -0
- data/lib/sevencop/cop_concerns/ordered.rb +45 -0
- data/lib/sevencop/cop_concerns.rb +7 -0
- data/lib/sevencop/version.rb +1 -1
- data/lib/sevencop.rb +3 -0
- data/sevencop.gemspec +2 -3
- metadata +8 -5
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 3e346d243517396c6f0813c7369dab4817564a13f019700543e6bf10a8469a7e
         | 
| 4 | 
            +
              data.tar.gz: 66c31821ed67e2c39946ee62cea8192104924847de0d0e3fb0c029eb0e9c7d47
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 9f19c719aa7a9c7fe2c5c48b9a0bb15840ec743759722ccb4ae497472fde33269158105e7128fe43bcfeb1795a8be385738c8d325de9b7d9dc106a9aede56736
         | 
| 7 | 
            +
              data.tar.gz: b6ea5b4210c1aed498d72eaf6c1b088e8d7aae22904b5b7afd10f1ce84a584e5a5d01b7b0f265d885c2ea86d179fcdd74d2bc50f7e4c5d77157bedaae0406b36
         | 
    
        data/.rubocop.yml
    CHANGED
    
    | @@ -10,6 +10,9 @@ AllCops: | |
| 10 10 | 
             
              SuggestExtensions: false
         | 
| 11 11 | 
             
              TargetRubyVersion: 2.6
         | 
| 12 12 |  | 
| 13 | 
            +
            Gemspec/RequireMFA:
         | 
| 14 | 
            +
              Enabled: false
         | 
| 15 | 
            +
             | 
| 13 16 | 
             
            Metrics:
         | 
| 14 17 | 
             
              Enabled: false
         | 
| 15 18 |  | 
| @@ -23,9 +26,25 @@ RSpec: | |
| 23 26 | 
             
            RSpec/ExampleLength:
         | 
| 24 27 | 
             
              Enabled: false
         | 
| 25 28 |  | 
| 29 | 
            +
            RSpec/FilePath:
         | 
| 30 | 
            +
              CustomTransform:
         | 
| 31 | 
            +
                RSpecExamplesInSameGroup: rspec_examples_in_same_group
         | 
| 32 | 
            +
             | 
| 26 33 | 
             
            RSpec/MultipleExpectations:
         | 
| 27 34 | 
             
              Enabled: false
         | 
| 28 35 |  | 
| 36 | 
            +
            Sevencop/AutoloadOrdered:
         | 
| 37 | 
            +
              Enabled: true
         | 
| 38 | 
            +
             | 
| 39 | 
            +
            Sevencop/HashElementOrdered:
         | 
| 40 | 
            +
              Enabled: true
         | 
| 41 | 
            +
             | 
| 42 | 
            +
            Sevencop/MethodDefinitionArgumentsMultiline:
         | 
| 43 | 
            +
              Enabled: true
         | 
| 44 | 
            +
             | 
| 45 | 
            +
            Sevencop/MethodDefinitionKeywordArgumentOrdered:
         | 
| 46 | 
            +
              Enabled: true
         | 
| 47 | 
            +
             | 
| 29 48 | 
             
            Sevencop/MethodDefinitionOrdered:
         | 
| 30 49 | 
             
              Enabled: true
         | 
| 31 50 |  | 
    
        data/Gemfile.lock
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -2,49 +2,31 @@ | |
| 2 2 |  | 
| 3 3 | 
             
            [](https://github.com/r7kamura/sevencop/actions/workflows/test.yml)
         | 
| 4 4 |  | 
| 5 | 
            -
             | 
| 5 | 
            +
            Opinionated custom cops for [RuboCop](https://github.com/rubocop/rubocop).
         | 
| 6 6 |  | 
| 7 | 
            -
            ##  | 
| 7 | 
            +
            ## Usage
         | 
| 8 8 |  | 
| 9 | 
            -
             | 
| 9 | 
            +
            Install `sevencop` gem:
         | 
| 10 10 |  | 
| 11 11 | 
             
            ```ruby
         | 
| 12 | 
            +
            # Gemfile
         | 
| 12 13 | 
             
            gem 'sevencop', require: false
         | 
| 13 14 | 
             
            ```
         | 
| 14 15 |  | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
            ```
         | 
| 18 | 
            -
            bundle install
         | 
| 19 | 
            -
            ```
         | 
| 20 | 
            -
             | 
| 21 | 
            -
            Or install it yourself as:
         | 
| 22 | 
            -
             | 
| 23 | 
            -
            ```
         | 
| 24 | 
            -
            gem install sevencop
         | 
| 25 | 
            -
            ```
         | 
| 26 | 
            -
             | 
| 27 | 
            -
            ## Usage
         | 
| 28 | 
            -
             | 
| 29 | 
            -
            Require sevencop from your .rubocop.yml:
         | 
| 16 | 
            +
            then require `sevencop` and enable the cops you want to use on .rubocop.yml:
         | 
| 30 17 |  | 
| 31 18 | 
             
            ```yaml
         | 
| 32 19 | 
             
            # .rubocop.yml
         | 
| 33 20 | 
             
            require:
         | 
| 34 21 | 
             
              - sevencop
         | 
| 35 | 
            -
            ```
         | 
| 36 22 |  | 
| 37 | 
            -
             | 
| 38 | 
            -
             | 
| 39 | 
            -
            ```yaml
         | 
| 40 | 
            -
            # .rubocop.yml
         | 
| 41 | 
            -
            Sevencop/RailsBelongsToOptional:
         | 
| 23 | 
            +
            Sevencop/MethodDefinitionOrdered:
         | 
| 42 24 | 
             
              Enabled: true
         | 
| 43 25 | 
             
            ```
         | 
| 44 26 |  | 
| 45 27 | 
             
            ## Cops
         | 
| 46 28 |  | 
| 47 | 
            -
             | 
| 29 | 
            +
            Choose the cops you want to use and enable them on your .rubocop.yml.
         | 
| 48 30 |  | 
| 49 31 | 
             
            - [Sevencop/AutoloadOrdered](lib/rubocop/cop/sevencop/autoload_ordered.rb)
         | 
| 50 32 | 
             
            - [Sevencop/HashElementOrdered](lib/rubocop/cop/sevencop/hash_element_ordered.rb)
         | 
| @@ -56,5 +38,7 @@ See YARD comments in each cop class for details: | |
| 56 38 | 
             
            - [Sevencop/RailsOrderField](lib/rubocop/cop/sevencop/rails_order_field.rb)
         | 
| 57 39 | 
             
            - [Sevencop/RailsUniquenessValidatorExplicitCaseSensitivity](lib/rubocop/cop/sevencop/rails_uniqueness_validator_explicit_case_sensitivity.rb)
         | 
| 58 40 | 
             
            - [Sevencop/RailsWhereNot](lib/rubocop/cop/sevencop/rails_where_not.rb)
         | 
| 41 | 
            +
            - [Sevencop/RequireOrdered](lib/rubocop/cop/sevencop/require_ordered.rb)
         | 
| 42 | 
            +
            - [Sevencop/RSpecExamplesInSameGroup](lib/rubocop/cop/sevencop/rspec_examples_in_same_group.rb)
         | 
| 59 43 |  | 
| 60 44 | 
             
            Note that all cops are `Enabled: false` by default.
         | 
    
        data/config/default.yml
    CHANGED
    
    | @@ -61,3 +61,18 @@ Sevencop/RailsWhereNot: | |
| 61 61 | 
             
              Enabled: false
         | 
| 62 62 | 
             
              Safe: false
         | 
| 63 63 | 
             
              VersionAdded: '0.7'
         | 
| 64 | 
            +
             | 
| 65 | 
            +
            Sevencop/RequireOrdered:
         | 
| 66 | 
            +
              Description: |
         | 
| 67 | 
            +
                Sort `require` and `require_relative` in alphabetical order.
         | 
| 68 | 
            +
              Enabled: false
         | 
| 69 | 
            +
              VersionAdded: '0.16'
         | 
| 70 | 
            +
             | 
| 71 | 
            +
            Sevencop/RSpecExamplesInSameGroup:
         | 
| 72 | 
            +
              Description: |
         | 
| 73 | 
            +
                Combine examples in the same groups in the time-consuming kinds of specs.
         | 
| 74 | 
            +
              Enabled: false
         | 
| 75 | 
            +
              VersionAdded: '0.17'
         | 
| 76 | 
            +
              Include:
         | 
| 77 | 
            +
                - spec/controllers/**/*
         | 
| 78 | 
            +
                - spec/requests/**/*
         | 
| @@ -25,6 +25,8 @@ module RuboCop | |
| 25 25 |  | 
| 26 26 | 
             
                    include RangeHelp
         | 
| 27 27 |  | 
| 28 | 
            +
                    include ::Sevencop::CopConcerns::Ordered
         | 
| 29 | 
            +
             | 
| 28 30 | 
             
                    MSG = 'Sort `autoload` in alphabetical order.'
         | 
| 29 31 |  | 
| 30 32 | 
             
                    RESTRICT_ON_SEND = %i[
         | 
| @@ -32,14 +34,15 @@ module RuboCop | |
| 32 34 | 
             
                    ].freeze
         | 
| 33 35 |  | 
| 34 36 | 
             
                    # @param node [RuboCop::AST::SendNode]
         | 
| 37 | 
            +
                    # @return [void]
         | 
| 35 38 | 
             
                    def on_send(node)
         | 
| 36 | 
            -
                       | 
| 37 | 
            -
                      return unless  | 
| 39 | 
            +
                      previous_older_sibling = find_previous_older_sibling(node)
         | 
| 40 | 
            +
                      return unless previous_older_sibling
         | 
| 38 41 |  | 
| 39 42 | 
             
                      add_offense(node) do |corrector|
         | 
| 40 43 | 
             
                        swap(
         | 
| 41 44 | 
             
                          range_by_whole_lines(
         | 
| 42 | 
            -
                             | 
| 45 | 
            +
                            previous_older_sibling.location.expression,
         | 
| 43 46 | 
             
                            include_final_newline: true
         | 
| 44 47 | 
             
                          ),
         | 
| 45 48 | 
             
                          range_by_whole_lines(
         | 
| @@ -54,12 +57,12 @@ module RuboCop | |
| 54 57 | 
             
                    private
         | 
| 55 58 |  | 
| 56 59 | 
             
                    # @param node [RuboCop::AST::SendNode]
         | 
| 57 | 
            -
                    # @return [RuboCop::AST::SendNode]
         | 
| 58 | 
            -
                    def  | 
| 59 | 
            -
                      node.left_siblings.find do |sibling|
         | 
| 60 | 
            -
                         | 
| 61 | 
            -
                         | 
| 62 | 
            -
                         | 
| 60 | 
            +
                    # @return [RuboCop::AST::SendNode, nil]
         | 
| 61 | 
            +
                    def find_previous_older_sibling(node)
         | 
| 62 | 
            +
                      node.left_siblings.grep(::RuboCop::AST::Node).reverse.find do |sibling|
         | 
| 63 | 
            +
                        break unless sibling.send_type?
         | 
| 64 | 
            +
                        break unless sibling.method?(:autoload)
         | 
| 65 | 
            +
                        break unless in_same_section?(sibling, node)
         | 
| 63 66 |  | 
| 64 67 | 
             
                        node.first_argument.source < sibling.first_argument.source
         | 
| 65 68 | 
             
                      end
         | 
| @@ -68,7 +71,10 @@ module RuboCop | |
| 68 71 | 
             
                    # @param node1 [RuboCop::AST::SendNode]
         | 
| 69 72 | 
             
                    # @param node2 [RuboCop::AST::SendNode]
         | 
| 70 73 | 
             
                    # @return [Boolean]
         | 
| 71 | 
            -
                    def in_same_section?( | 
| 74 | 
            +
                    def in_same_section?(
         | 
| 75 | 
            +
                      node1,
         | 
| 76 | 
            +
                      node2
         | 
| 77 | 
            +
                    )
         | 
| 72 78 | 
             
                      !node1.location.expression.with(
         | 
| 73 79 | 
             
                        end_pos: node2.location.expression.end_pos
         | 
| 74 80 | 
             
                      ).source.include?("\n\n")
         | 
| @@ -77,7 +83,11 @@ module RuboCop | |
| 77 83 | 
             
                    # @param range1 [Paresr::Source::Range]
         | 
| 78 84 | 
             
                    # @param range2 [Paresr::Source::Range]
         | 
| 79 85 | 
             
                    # @param corrector [RuboCop::AST::Corrector]
         | 
| 80 | 
            -
                    def swap( | 
| 86 | 
            +
                    def swap(
         | 
| 87 | 
            +
                      range1,
         | 
| 88 | 
            +
                      range2,
         | 
| 89 | 
            +
                      corrector:
         | 
| 90 | 
            +
                    )
         | 
| 81 91 | 
             
                      corrector.insert_before(range1, range2.source)
         | 
| 82 92 | 
             
                      corrector.remove(range2)
         | 
| 83 93 | 
             
                    end
         | 
| @@ -39,7 +39,10 @@ module RuboCop | |
| 39 39 | 
             
                    # @param corrector [RuboCop::AST::Corrector]
         | 
| 40 40 | 
             
                    # @param node [RuboCop::AST::ArgsNode]
         | 
| 41 41 | 
             
                    # @return [String]
         | 
| 42 | 
            -
                    def autocorrect( | 
| 42 | 
            +
                    def autocorrect(
         | 
| 43 | 
            +
                      corrector,
         | 
| 44 | 
            +
                      node
         | 
| 45 | 
            +
                    )
         | 
| 43 46 | 
             
                      indent = ' ' * node.parent.location.expression.column
         | 
| 44 47 |  | 
| 45 48 | 
             
                      corrector.replace(
         | 
| @@ -30,9 +30,12 @@ module RuboCop | |
| 30 30 | 
             
                    include RangeHelp
         | 
| 31 31 | 
             
                    include VisibilityHelp
         | 
| 32 32 |  | 
| 33 | 
            +
                    include ::Sevencop::CopConcerns::Ordered
         | 
| 34 | 
            +
             | 
| 33 35 | 
             
                    MSG = 'Sort method definition in alphabetical order.'
         | 
| 34 36 |  | 
| 35 37 | 
             
                    # @param node [RuboCop::AST::DefNode]
         | 
| 38 | 
            +
                    # @return [void]
         | 
| 36 39 | 
             
                    def on_def(node)
         | 
| 37 40 | 
             
                      previous_older_sibling = find_previous_older_sibling(node)
         | 
| 38 41 | 
             
                      return unless previous_older_sibling
         | 
| @@ -43,7 +46,8 @@ module RuboCop | |
| 43 46 | 
             
                        swap(
         | 
| 44 47 | 
             
                          range_with_comments_and_lines(previous_older_sibling),
         | 
| 45 48 | 
             
                          range_with_comments_and_lines(node),
         | 
| 46 | 
            -
                          corrector: corrector
         | 
| 49 | 
            +
                          corrector: corrector,
         | 
| 50 | 
            +
                          newline: true
         | 
| 47 51 | 
             
                        )
         | 
| 48 52 | 
             
                      end
         | 
| 49 53 | 
             
                    end
         | 
| @@ -52,7 +56,7 @@ module RuboCop | |
| 52 56 | 
             
                    private
         | 
| 53 57 |  | 
| 54 58 | 
             
                    # @param node [RuboCop::AST::DefNode]
         | 
| 55 | 
            -
                    # @return [RuboCop::AST::DefNode]
         | 
| 59 | 
            +
                    # @return [RuboCop::AST::DefNode, nil]
         | 
| 56 60 | 
             
                    def find_previous_older_sibling(node)
         | 
| 57 61 | 
             
                      previous_siblings_in_same_section_of(node).find do |sibling|
         | 
| 58 62 | 
             
                        next if sibling.type != node.type
         | 
| @@ -64,33 +68,14 @@ module RuboCop | |
| 64 68 | 
             
                    # @param node [RuboCop::AST::Node]
         | 
| 65 69 | 
             
                    # @return [Array<RuboCop::AST::Node>]
         | 
| 66 70 | 
             
                    def previous_siblings_in_same_section_of(node)
         | 
| 67 | 
            -
                       | 
| 71 | 
            +
                      siblings = node.left_siblings.grep(::RuboCop::AST::Node)
         | 
| 72 | 
            +
                      return siblings if node.defs_type?
         | 
| 68 73 |  | 
| 69 | 
            -
                       | 
| 74 | 
            +
                      siblings.reverse.take_while do |sibling|
         | 
| 70 75 | 
             
                        !visibility_block?(sibling)
         | 
| 71 | 
            -
                      end.reverse
         | 
| 72 | 
            -
                    end
         | 
| 73 | 
            -
             | 
| 74 | 
            -
                    # @param node [RuboCop::AST::Node]
         | 
| 75 | 
            -
                    # @return [Paresr::Source::Range]
         | 
| 76 | 
            -
                    def range_with_comments(node)
         | 
| 77 | 
            -
                      comment = processed_source.ast_with_comments[node].first
         | 
| 78 | 
            -
                      if comment
         | 
| 79 | 
            -
                        node.location.expression.with(begin_pos: comment.location.expression.begin_pos)
         | 
| 80 | 
            -
                      else
         | 
| 81 | 
            -
                        node.location.expression
         | 
| 82 76 | 
             
                      end
         | 
| 83 77 | 
             
                    end
         | 
| 84 78 |  | 
| 85 | 
            -
                    # @param node [RuboCop::AST::Node]
         | 
| 86 | 
            -
                    # @return [Parser::Source::Range]
         | 
| 87 | 
            -
                    def range_with_comments_and_lines(node)
         | 
| 88 | 
            -
                      range_by_whole_lines(
         | 
| 89 | 
            -
                        range_with_comments(node),
         | 
| 90 | 
            -
                        include_final_newline: true
         | 
| 91 | 
            -
                      )
         | 
| 92 | 
            -
                    end
         | 
| 93 | 
            -
             | 
| 94 79 | 
             
                    # @param node [RuboCop::AST::DefNode]
         | 
| 95 80 | 
             
                    # @return [#<=>]
         | 
| 96 81 | 
             
                    def sort_key_of(node)
         | 
| @@ -99,17 +84,6 @@ module RuboCop | |
| 99 84 | 
             
                        node.method_name
         | 
| 100 85 | 
             
                      ]
         | 
| 101 86 | 
             
                    end
         | 
| 102 | 
            -
             | 
| 103 | 
            -
                    # @param range1 [Paresr::Source::Range]
         | 
| 104 | 
            -
                    # @param range2 [Paresr::Source::Range]
         | 
| 105 | 
            -
                    # @param corrector [RuboCop::AST::Corrector]
         | 
| 106 | 
            -
                    def swap(range1, range2, corrector:)
         | 
| 107 | 
            -
                      corrector.insert_before(
         | 
| 108 | 
            -
                        range1,
         | 
| 109 | 
            -
                        "#{range2.source}\n"
         | 
| 110 | 
            -
                      )
         | 
| 111 | 
            -
                      corrector.remove(range2)
         | 
| 112 | 
            -
                    end
         | 
| 113 87 | 
             
                  end
         | 
| 114 88 | 
             
                end
         | 
| 115 89 | 
             
              end
         | 
| @@ -81,7 +81,10 @@ module RuboCop | |
| 81 81 |  | 
| 82 82 | 
             
                    # @param [RuboCop::AST::Corrector] corrector
         | 
| 83 83 | 
             
                    # @param [RuboCop::AST::Node] node
         | 
| 84 | 
            -
                    def autocorrect( | 
| 84 | 
            +
                    def autocorrect(
         | 
| 85 | 
            +
                      corrector,
         | 
| 86 | 
            +
                      node
         | 
| 87 | 
            +
                    )
         | 
| 85 88 | 
             
                      corrector.remove(
         | 
| 86 89 | 
             
                        node.location.expression.with(
         | 
| 87 90 | 
             
                          begin_pos: node.left_sibling.location.expression.end_pos
         | 
| @@ -0,0 +1,94 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module RuboCop
         | 
| 4 | 
            +
              module Cop
         | 
| 5 | 
            +
                module Sevencop
         | 
| 6 | 
            +
                  # Sort `require` and `require_relative` in alphabetical order.
         | 
| 7 | 
            +
                  #
         | 
| 8 | 
            +
                  # @example
         | 
| 9 | 
            +
                  #   # bad
         | 
| 10 | 
            +
                  #   require 'b'
         | 
| 11 | 
            +
                  #   require 'a'
         | 
| 12 | 
            +
                  #
         | 
| 13 | 
            +
                  #   # good
         | 
| 14 | 
            +
                  #   require 'a'
         | 
| 15 | 
            +
                  #   require 'b'
         | 
| 16 | 
            +
                  #
         | 
| 17 | 
            +
                  #   # bad
         | 
| 18 | 
            +
                  #   require_relative 'b'
         | 
| 19 | 
            +
                  #   require_relative 'a'
         | 
| 20 | 
            +
                  #
         | 
| 21 | 
            +
                  #   # good
         | 
| 22 | 
            +
                  #   require_relative 'a'
         | 
| 23 | 
            +
                  #   require_relative 'b'
         | 
| 24 | 
            +
                  #
         | 
| 25 | 
            +
                  #   # good
         | 
| 26 | 
            +
                  #   require 'a'
         | 
| 27 | 
            +
                  #   require 'd'
         | 
| 28 | 
            +
                  #
         | 
| 29 | 
            +
                  #   require 'b'
         | 
| 30 | 
            +
                  #   require 'c'
         | 
| 31 | 
            +
                  #
         | 
| 32 | 
            +
                  #   # good
         | 
| 33 | 
            +
                  #   require 'b'
         | 
| 34 | 
            +
                  #   require_relative 'c'
         | 
| 35 | 
            +
                  #   require 'a'
         | 
| 36 | 
            +
                  class RequireOrdered < Base
         | 
| 37 | 
            +
                    extend AutoCorrector
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                    include RangeHelp
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                    include ::Sevencop::CopConcerns::Ordered
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                    RESTRICT_ON_SEND = %i[
         | 
| 44 | 
            +
                      require
         | 
| 45 | 
            +
                      require_relative
         | 
| 46 | 
            +
                    ].freeze
         | 
| 47 | 
            +
             | 
| 48 | 
            +
                    # @param node [RuboCop::AST::SendNode]
         | 
| 49 | 
            +
                    # @return [void]
         | 
| 50 | 
            +
                    def on_send(node)
         | 
| 51 | 
            +
                      previous_older_sibling = find_previous_older_sibling(node)
         | 
| 52 | 
            +
                      return unless previous_older_sibling
         | 
| 53 | 
            +
             | 
| 54 | 
            +
                      add_offense(
         | 
| 55 | 
            +
                        range_with_comments(node),
         | 
| 56 | 
            +
                        message: "Sort `#{node.method_name}` in alphabetical order."
         | 
| 57 | 
            +
                      ) do |corrector|
         | 
| 58 | 
            +
                        swap(
         | 
| 59 | 
            +
                          range_with_comments_and_lines(previous_older_sibling),
         | 
| 60 | 
            +
                          range_with_comments_and_lines(node),
         | 
| 61 | 
            +
                          corrector: corrector
         | 
| 62 | 
            +
                        )
         | 
| 63 | 
            +
                      end
         | 
| 64 | 
            +
                    end
         | 
| 65 | 
            +
             | 
| 66 | 
            +
                    private
         | 
| 67 | 
            +
             | 
| 68 | 
            +
                    # @param node [RuboCop::AST::SendNode]
         | 
| 69 | 
            +
                    # @return [RuboCop::AST::SendNode, nil]
         | 
| 70 | 
            +
                    def find_previous_older_sibling(node)
         | 
| 71 | 
            +
                      node.left_siblings.reverse.find do |sibling|
         | 
| 72 | 
            +
                        break unless sibling.send_type?
         | 
| 73 | 
            +
                        break if sibling.method_name != node.method_name
         | 
| 74 | 
            +
                        break unless in_same_section?(sibling, node)
         | 
| 75 | 
            +
             | 
| 76 | 
            +
                        node.first_argument.source < sibling.first_argument.source
         | 
| 77 | 
            +
                      end
         | 
| 78 | 
            +
                    end
         | 
| 79 | 
            +
             | 
| 80 | 
            +
                    # @param node1 [RuboCop::AST::SendNode]
         | 
| 81 | 
            +
                    # @param node2 [RuboCop::AST::SendNode]
         | 
| 82 | 
            +
                    # @return [Boolean]
         | 
| 83 | 
            +
                    def in_same_section?(
         | 
| 84 | 
            +
                      node1,
         | 
| 85 | 
            +
                      node2
         | 
| 86 | 
            +
                    )
         | 
| 87 | 
            +
                      !node1.location.expression.with(
         | 
| 88 | 
            +
                        end_pos: node2.location.expression.end_pos
         | 
| 89 | 
            +
                      ).source.include?("\n\n")
         | 
| 90 | 
            +
                    end
         | 
| 91 | 
            +
                  end
         | 
| 92 | 
            +
                end
         | 
| 93 | 
            +
              end
         | 
| 94 | 
            +
            end
         | 
| @@ -0,0 +1,77 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module RuboCop
         | 
| 4 | 
            +
              module Cop
         | 
| 5 | 
            +
                module Sevencop
         | 
| 6 | 
            +
                  # Combine examples in same group in the time-consuming kinds of specs.
         | 
| 7 | 
            +
                  #
         | 
| 8 | 
            +
                  # @example
         | 
| 9 | 
            +
                  #
         | 
| 10 | 
            +
                  #   # bad
         | 
| 11 | 
            +
                  #   context 'when user is logged in' do
         | 
| 12 | 
            +
                  #     it 'returns 200' do
         | 
| 13 | 
            +
                  #       subject
         | 
| 14 | 
            +
                  #       expect(response).to have_http_status(200)
         | 
| 15 | 
            +
                  #     end
         | 
| 16 | 
            +
                  #
         | 
| 17 | 
            +
                  #     it 'creates Foo' do
         | 
| 18 | 
            +
                  #       expect { subject }.to change(Foo, :count).by(1)
         | 
| 19 | 
            +
                  #     end
         | 
| 20 | 
            +
                  #   end
         | 
| 21 | 
            +
                  #
         | 
| 22 | 
            +
                  #   # good
         | 
| 23 | 
            +
                  #   context 'when user is logged in' do
         | 
| 24 | 
            +
                  #     it 'creates Foo and returns 200' do
         | 
| 25 | 
            +
                  #       expect { subject }.to change(Foo, :count).by(1)
         | 
| 26 | 
            +
                  #       expect(response).to have_http_status(200)
         | 
| 27 | 
            +
                  #     end
         | 
| 28 | 
            +
                  #   end
         | 
| 29 | 
            +
                  #
         | 
| 30 | 
            +
                  class RSpecExamplesInSameGroup < Base
         | 
| 31 | 
            +
                    extend AutoCorrector
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                    EXAMPLE_METHOD_NAMES_REGULAR = ::Set[
         | 
| 34 | 
            +
                      :it,
         | 
| 35 | 
            +
                      :its,
         | 
| 36 | 
            +
                      :specify,
         | 
| 37 | 
            +
                      :example,
         | 
| 38 | 
            +
                      :scenario,
         | 
| 39 | 
            +
                    ].freeze
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                    MSG = 'Combine examples in the same group in the time-consuming kinds of specs.'
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                    # @param node [RuboCop::AST::BlockNode]
         | 
| 44 | 
            +
                    # @return [void]
         | 
| 45 | 
            +
                    def on_block(node)
         | 
| 46 | 
            +
                      return unless example?(node)
         | 
| 47 | 
            +
             | 
| 48 | 
            +
                      previous_sibling_example = previous_sibling_example_of(node)
         | 
| 49 | 
            +
                      return unless previous_sibling_example
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                      add_offense(node)
         | 
| 52 | 
            +
                    end
         | 
| 53 | 
            +
                    alias on_numblock on_block
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                    private
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                    # @!method example?(node)
         | 
| 58 | 
            +
                    #   @param node [RuboCop::AST::Node]
         | 
| 59 | 
            +
                    #   @return [Boolean]
         | 
| 60 | 
            +
                    def_node_matcher :example?, <<~PATTERN
         | 
| 61 | 
            +
                      (block
         | 
| 62 | 
            +
                        (send nil? EXAMPLE_METHOD_NAMES_REGULAR ...)
         | 
| 63 | 
            +
                        ...
         | 
| 64 | 
            +
                      )
         | 
| 65 | 
            +
                    PATTERN
         | 
| 66 | 
            +
             | 
| 67 | 
            +
                    # @param node [RuboCop::AST::BlockNode]
         | 
| 68 | 
            +
                    # @return [RuboCop::AST::BlockNode, nil]
         | 
| 69 | 
            +
                    def previous_sibling_example_of(node)
         | 
| 70 | 
            +
                      node.left_siblings.find do |sibling|
         | 
| 71 | 
            +
                        sibling.is_a?(::RuboCop::AST::Node) && example?(sibling)
         | 
| 72 | 
            +
                      end
         | 
| 73 | 
            +
                    end
         | 
| 74 | 
            +
                  end
         | 
| 75 | 
            +
                end
         | 
| 76 | 
            +
              end
         | 
| 77 | 
            +
            end
         | 
| @@ -0,0 +1,45 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Sevencop
         | 
| 4 | 
            +
              module CopConcerns
         | 
| 5 | 
            +
                module Ordered
         | 
| 6 | 
            +
                  private
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                  # @param node [RuboCop::AST::Node]
         | 
| 9 | 
            +
                  # @return [Paresr::Source::Range]
         | 
| 10 | 
            +
                  def range_with_comments(node)
         | 
| 11 | 
            +
                    comment = processed_source.ast_with_comments[node].first
         | 
| 12 | 
            +
                    if comment
         | 
| 13 | 
            +
                      node.location.expression.with(begin_pos: comment.location.expression.begin_pos)
         | 
| 14 | 
            +
                    else
         | 
| 15 | 
            +
                      node.location.expression
         | 
| 16 | 
            +
                    end
         | 
| 17 | 
            +
                  end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                  # @param node [RuboCop::AST::Node]
         | 
| 20 | 
            +
                  # @return [Parser::Source::Range]
         | 
| 21 | 
            +
                  def range_with_comments_and_lines(node)
         | 
| 22 | 
            +
                    range_by_whole_lines(
         | 
| 23 | 
            +
                      range_with_comments(node),
         | 
| 24 | 
            +
                      include_final_newline: true
         | 
| 25 | 
            +
                    )
         | 
| 26 | 
            +
                  end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                  # @param range1 [Paresr::Source::Range]
         | 
| 29 | 
            +
                  # @param range2 [Paresr::Source::Range]
         | 
| 30 | 
            +
                  # @param corrector [RuboCop::AST::Corrector]
         | 
| 31 | 
            +
                  # @param newline [Boolean]
         | 
| 32 | 
            +
                  def swap(
         | 
| 33 | 
            +
                    range1,
         | 
| 34 | 
            +
                    range2,
         | 
| 35 | 
            +
                    corrector:,
         | 
| 36 | 
            +
                    newline: false
         | 
| 37 | 
            +
                  )
         | 
| 38 | 
            +
                    inserted = range2.source
         | 
| 39 | 
            +
                    inserted += "\n" if newline
         | 
| 40 | 
            +
                    corrector.insert_before(range1, inserted)
         | 
| 41 | 
            +
                    corrector.remove(range2)
         | 
| 42 | 
            +
                  end
         | 
| 43 | 
            +
                end
         | 
| 44 | 
            +
              end
         | 
| 45 | 
            +
            end
         | 
    
        data/lib/sevencop/version.rb
    CHANGED
    
    
    
        data/lib/sevencop.rb
    CHANGED
    
    | @@ -1,5 +1,6 @@ | |
| 1 1 | 
             
            # frozen_string_literal: true
         | 
| 2 2 |  | 
| 3 | 
            +
            require_relative 'sevencop/cop_concerns'
         | 
| 3 4 | 
             
            require_relative 'sevencop/rubocop_extension'
         | 
| 4 5 | 
             
            require_relative 'sevencop/version'
         | 
| 5 6 |  | 
| @@ -13,3 +14,5 @@ require_relative 'rubocop/cop/sevencop/rails_inferred_spec_type' | |
| 13 14 | 
             
            require_relative 'rubocop/cop/sevencop/rails_order_field'
         | 
| 14 15 | 
             
            require_relative 'rubocop/cop/sevencop/rails_uniqueness_validator_explicit_case_sensitivity'
         | 
| 15 16 | 
             
            require_relative 'rubocop/cop/sevencop/rails_where_not'
         | 
| 17 | 
            +
            require_relative 'rubocop/cop/sevencop/require_ordered'
         | 
| 18 | 
            +
            require_relative 'rubocop/cop/sevencop/rspec_examples_in_same_group'
         | 
    
        data/sevencop.gemspec
    CHANGED
    
    | @@ -8,15 +8,14 @@ Gem::Specification.new do |spec| | |
| 8 8 | 
             
              spec.authors = ['Ryo Nakamura']
         | 
| 9 9 | 
             
              spec.email = ['r7kamura@gmail.com']
         | 
| 10 10 |  | 
| 11 | 
            -
              spec.summary = ' | 
| 11 | 
            +
              spec.summary = 'Opinionated custom cops for RuboCop.'
         | 
| 12 12 | 
             
              spec.homepage = 'https://github.com/r7kamura/sevencop'
         | 
| 13 13 | 
             
              spec.license = 'MIT'
         | 
| 14 14 | 
             
              spec.required_ruby_version = '>= 2.6.0'
         | 
| 15 15 |  | 
| 16 16 | 
             
              spec.metadata['homepage_uri'] = spec.homepage
         | 
| 17 17 | 
             
              spec.metadata['source_code_uri'] = spec.homepage
         | 
| 18 | 
            -
              spec.metadata['changelog_uri'] = "#{spec.homepage}/ | 
| 19 | 
            -
              spec.metadata['rubygems_mfa_required'] = 'true'
         | 
| 18 | 
            +
              spec.metadata['changelog_uri'] = "#{spec.homepage}/releases"
         | 
| 20 19 |  | 
| 21 20 | 
             
              # Specify which files should be added to the gem when it is released.
         | 
| 22 21 | 
             
              # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: sevencop
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.17.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Ryo Nakamura
         | 
| 8 8 | 
             
            autorequire:
         | 
| 9 9 | 
             
            bindir: exe
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2022- | 
| 11 | 
            +
            date: 2022-10-04 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: rubocop
         | 
| @@ -51,8 +51,12 @@ files: | |
| 51 51 | 
             
            - lib/rubocop/cop/sevencop/rails_order_field.rb
         | 
| 52 52 | 
             
            - lib/rubocop/cop/sevencop/rails_uniqueness_validator_explicit_case_sensitivity.rb
         | 
| 53 53 | 
             
            - lib/rubocop/cop/sevencop/rails_where_not.rb
         | 
| 54 | 
            +
            - lib/rubocop/cop/sevencop/require_ordered.rb
         | 
| 55 | 
            +
            - lib/rubocop/cop/sevencop/rspec_examples_in_same_group.rb
         | 
| 54 56 | 
             
            - lib/sevencop.rb
         | 
| 55 57 | 
             
            - lib/sevencop/config_loader.rb
         | 
| 58 | 
            +
            - lib/sevencop/cop_concerns.rb
         | 
| 59 | 
            +
            - lib/sevencop/cop_concerns/ordered.rb
         | 
| 56 60 | 
             
            - lib/sevencop/rubocop_extension.rb
         | 
| 57 61 | 
             
            - lib/sevencop/version.rb
         | 
| 58 62 | 
             
            - sevencop.gemspec
         | 
| @@ -62,8 +66,7 @@ licenses: | |
| 62 66 | 
             
            metadata:
         | 
| 63 67 | 
             
              homepage_uri: https://github.com/r7kamura/sevencop
         | 
| 64 68 | 
             
              source_code_uri: https://github.com/r7kamura/sevencop
         | 
| 65 | 
            -
              changelog_uri: https://github.com/r7kamura/sevencop/ | 
| 66 | 
            -
              rubygems_mfa_required: 'true'
         | 
| 69 | 
            +
              changelog_uri: https://github.com/r7kamura/sevencop/releases
         | 
| 67 70 | 
             
            post_install_message:
         | 
| 68 71 | 
             
            rdoc_options: []
         | 
| 69 72 | 
             
            require_paths:
         | 
| @@ -82,5 +85,5 @@ requirements: [] | |
| 82 85 | 
             
            rubygems_version: 3.3.7
         | 
| 83 86 | 
             
            signing_key:
         | 
| 84 87 | 
             
            specification_version: 4
         | 
| 85 | 
            -
            summary:  | 
| 88 | 
            +
            summary: Opinionated custom cops for RuboCop.
         | 
| 86 89 | 
             
            test_files: []
         |