File: heredoc.rb

package info (click to toggle)
ruby-graphql-client 0.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 252 kB
  • sloc: ruby: 1,878; makefile: 4
file content (37 lines) | stat: -rw-r--r-- 1,043 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true
require "rubocop"

module RuboCop
  module Cop
    module GraphQL
      # Public: Cop for enforcing non-interpolated GRAPHQL heredocs.
      class Heredoc < Cop
        def on_dstr(node)
          check_str(node)
        end

        def on_str(node)
          check_str(node)
        end

        def check_str(node)
          return unless node.location.is_a?(Parser::Source::Map::Heredoc)
          return unless node.location.expression.source =~ /^<<(-|~)?GRAPHQL/

          node.each_child_node(:begin) do |begin_node|
            add_offense(begin_node, location: :expression, message: "Do not interpolate variables into GraphQL queries, " \
              "used variables instead.")
          end

          add_offense(node, location: :expression, message: "GraphQL heredocs should be quoted. <<-'GRAPHQL'")
        end

        def autocorrect(node)
          ->(corrector) do
            corrector.replace(node.location.expression, "<<-'GRAPHQL'")
          end
        end
      end
    end
  end
end