Class: RuboCop::Cop::GraphQL::Heredoc

Inherits:
RuboCop::Cop show all
Defined in:
lib/rubocop/cop/graphql/heredoc.rb

Overview

Public: Cop for enforcing non-interpolated GRAPHQL heredocs.

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



29
30
31
32
33
# File 'lib/rubocop/cop/graphql/heredoc.rb', line 29

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

#check_str(node) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rubocop/cop/graphql/heredoc.rb', line 17

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

#on_dstr(node) ⇒ Object



9
10
11
# File 'lib/rubocop/cop/graphql/heredoc.rb', line 9

def on_dstr(node)
  check_str(node)
end

#on_str(node) ⇒ Object



13
14
15
# File 'lib/rubocop/cop/graphql/heredoc.rb', line 13

def on_str(node)
  check_str(node)
end