|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module RuboCop |
| 4 | + module Cop |
| 5 | + module Sagittarius |
| 6 | + class ErrorCode < RuboCop::Cop::Base |
| 7 | + MSG = 'Error code %s doesnt exist.' |
| 8 | + |
| 9 | + def_node_matcher :is_service_error?, <<~PATTERN |
| 10 | + (send (const _ :ServiceResponse) :error ...) |
| 11 | + PATTERN |
| 12 | + |
| 13 | + def on_send(node) |
| 14 | + return unless in_service?(node) |
| 15 | + return unless is_service_error?(node) |
| 16 | + |
| 17 | + node.children.each do |child| |
| 18 | + next unless child.is_a?(RuboCop::AST::HashNode) |
| 19 | + |
| 20 | + child.children.each do |child_child| |
| 21 | + next unless child_child.is_a?(RuboCop::AST::PairNode) |
| 22 | + next unless child_child.children[0].sym_type? |
| 23 | + next unless child_child.children[1].sym_type? |
| 24 | + next unless child_child.children[0].value == :error_code |
| 25 | + |
| 26 | + code = child_child.children[1].value |
| 27 | + add_offense(child_child.children[1], message: MSG % ":#{code}") unless exists_error_code?(code) |
| 28 | + end |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + def exists_error_code?(code) |
| 33 | + @exists_error_code ||= extract_all_error_codes |
| 34 | + |
| 35 | + @exists_error_code.include?(code) |
| 36 | + end |
| 37 | + |
| 38 | + def extract_error_code_hash_from_ast(ast) |
| 39 | + return {} unless ast |
| 40 | + |
| 41 | + method_defs = ast.each_node(:def, :defs).select do |node| |
| 42 | + node.method_name == :error_codes |
| 43 | + end |
| 44 | + |
| 45 | + return {} if method_defs.empty? |
| 46 | + |
| 47 | + # Collect all hash literals returned by these methods |
| 48 | + hash_nodes = method_defs.filter_map do |m| |
| 49 | + body = m.body |
| 50 | + next unless body |
| 51 | + |
| 52 | + # The body can be: |
| 53 | + # - a literal hash |
| 54 | + # - a call to `super.merge({ ... })` |
| 55 | + if body.hash_type? |
| 56 | + body |
| 57 | + elsif body.send_type? && body.method_name == :merge |
| 58 | + merge_arg = body.arguments.first |
| 59 | + merge_arg if merge_arg&.hash_type? |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + hash_nodes.flat_map(&:pairs).each_with_object({}) do |pair, h| |
| 64 | + key = pair.key |
| 65 | + next unless key.sym_type? |
| 66 | + |
| 67 | + h[key.value] = pair.value |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + def extract_all_error_codes |
| 72 | + files = Dir.glob("#{__dir__}/../../../../**/app/services/**/error_code.rb") |
| 73 | + |
| 74 | + merged = {} |
| 75 | + |
| 76 | + files.each do |path| |
| 77 | + next unless File.exist?(path) |
| 78 | + |
| 79 | + ast = RuboCop::ProcessedSource.new(File.read(path), RUBY_VERSION.to_f).ast |
| 80 | + partial = extract_error_code_hash_from_ast(ast) |
| 81 | + merged.merge!(partial) # child overrides parent, matches super.merge behavior |
| 82 | + end |
| 83 | + |
| 84 | + merged.keys |
| 85 | + end |
| 86 | + |
| 87 | + def dirname(node) |
| 88 | + File.dirname(filepath(node)) |
| 89 | + end |
| 90 | + |
| 91 | + def filepath(node) |
| 92 | + node.location.expression.source_buffer.name |
| 93 | + end |
| 94 | + |
| 95 | + def in_service?(node) |
| 96 | + dirname(node).include?('app/services') # .include? because the path is ../app/services/... |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | +end |
0 commit comments