-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathtrace_methods_cop.rb
More file actions
100 lines (93 loc) · 3.21 KB
/
trace_methods_cop.rb
File metadata and controls
100 lines (93 loc) · 3.21 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# frozen_string_literal: true
require 'rubocop'
module Cop
module Development
class TraceMethodsCop < RuboCop::Cop::Base
extend RuboCop::Cop::AutoCorrector
TRACE_HOOKS = [
:analyze_multiplex,
:analyze_query,
:authorized,
:authorized_lazy,
:begin_analyze_multiplex,
:begin_authorized,
:begin_dataloader,
:begin_dataloader_source,
:begin_execute_field,
:begin_resolve_type,
:begin_validate,
:dataloader_fiber_exit,
:dataloader_fiber_resume,
:dataloader_fiber_yield,
:dataloader_spawn_execution_fiber,
:dataloader_spawn_source_fiber,
:end_analyze_multiplex,
:end_authorized,
:end_dataloader,
:end_dataloader_source,
:end_execute_field,
:end_resolve_type,
:end_validate,
:execute_field,
:execute_field_lazy,
:execute_multiplex,
:execute_query,
:execute_query_lazy,
:lex,
:object_loaded,
:objects,
:parse,
:resolve_type,
:resolve_type_lazy,
:validate,
]
MSG = "Trace methods should call `super` to pass control to other traces"
def on_def(node)
if TRACE_HOOKS.include?(node.method_name) && !node.each_descendant(:super, :zsuper).any?
add_offense(node) do |corrector|
if node.body
offset = node.loc.column + 2
corrector.insert_after(node.body.loc.expression, "\n#{' ' * offset}super")
end
end
end
end
def on_module(node)
if node.defined_module_name.to_s.end_with?("Trace")
all_defs = []
node.body.each_child_node do |body_node|
if body_node.def_type?
all_defs << body_node.method_name
end
end
missing_defs = TRACE_HOOKS - all_defs
redundant_defs = [
# Not really necessary for making a good trace:
:lex, :analyze_query, :execute_query, :execute_query_lazy,
# Only useful for isolated event tracking:
:begin_dataloader, :end_dataloader,
:dataloader_fiber_exit, :dataloader_spawn_execution_fiber, :dataloader_spawn_source_fiber,
# Tracks object references, but not durations:
:objects, :object_loaded
]
missing_defs.each do |missing_def|
if all_defs.include?(:"begin_#{missing_def}") && all_defs.include?(:"end_#{missing_def}")
redundant_defs << missing_def
redundant_defs << :"#{missing_def}_lazy"
end
missing_name = missing_def.to_s
if missing_name.start_with?("begin") && all_defs.include?(:"#{missing_name.sub("begin_", "")}")
redundant_defs << missing_def
elsif missing_name.start_with?("end") && all_defs.include?(:"#{missing_name.sub("end_", "")}")
redundant_defs << missing_def
end
end
missing_defs -= redundant_defs
if missing_defs.any?
add_offense(node, message: "Missing some trace hook methods:\n\n- #{missing_defs.join("\n- ")}")
end
end
end
end
end
end