Sha256: 87a59c766e1f665910dcc2c577e44ec1bb0b6f97e5d2105b1d757ef0b39dd33b

Contents?: true

Size: 1.28 KB

Versions: 90

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 preceeding 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

90 entries across 90 versions & 2 rubygems

Version Path
graphql-2.2.16 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.1.13 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.0.31 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.3.2 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.3.1 lib/graphql/rubocop/graphql/base_cop.rb
graphql-1.13.23 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.3.0 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.2.14 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.2.13 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.2.12 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.0.29 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.1.12 lib/graphql/rubocop/graphql/base_cop.rb
graphql-1.13.22 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.2.11 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.2.10 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.2.9 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.2.8 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.2.7 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.2.6 lib/graphql/rubocop/graphql/base_cop.rb
graphql-2.1.11 lib/graphql/rubocop/graphql/base_cop.rb