Sha256: 5bb70cbf53b3a0dbc0a7c433af2f1eae32e588c6ac4729fdde90a02020bf1559

Contents?: true

Size: 1.28 KB

Versions: 33

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true
require "rubocop"

module GraphQL
  module Rubocop
    module GraphQL
      class BaseCop < RuboCop::Cop::Base
        extend RuboCop::Cop::AutoCorrector

        # Return the source of `send_node`, but without the keyword argument represented by `pair_node`
        def source_without_keyword_argument(send_node, pair_node)
          # work back to the preceding comma
          first_pos = pair_node.location.expression.begin_pos
          end_pos = pair_node.location.expression.end_pos
          node_source = send_node.source_range.source
          node_first_pos = send_node.location.expression.begin_pos

          relative_first_pos = first_pos - node_first_pos
          relative_last_pos = end_pos - node_first_pos

          begin_removal_pos = relative_first_pos
          while node_source[begin_removal_pos] != ","
            begin_removal_pos -= 1
            if begin_removal_pos < 1
              raise "Invariant: somehow backtracked to beginning of node looking for a comma (node source: #{node_source.inspect})"
            end
          end

          end_removal_pos = relative_last_pos
          cleaned_node_source = node_source[0...begin_removal_pos] + node_source[end_removal_pos..-1]
          cleaned_node_source
        end
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
graphql-2.4.14 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.3.22 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.3.21 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.4.13 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.4.12 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.4.11 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.4.10 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.4.9 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.4.8 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.4.7 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.4.6 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.4.5 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.4.4 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.4.3 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.4.2 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.4.1 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.4.0 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.3.20 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.3.19 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.3.18 lib/graphql/rubocop/graphql/base_cop.rb