/
cop.rb
146 lines (117 loc) · 3.78 KB
/
cop.rb
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# frozen_string_literal: true
require 'uri'
require_relative 'legacy/corrections_proxy'
module RuboCop
module Cop
# @deprecated Use Cop::Base instead
# Legacy scaffold for Cops.
# See https://docs.rubocop.org/rubocop/cop_api_v1_changelog.html
class Cop < Base
attr_reader :offenses
exclude_from_registry
# @deprecated
Correction = Struct.new(:lambda, :node, :cop) do
def call(corrector)
lambda.call(corrector)
rescue StandardError => e
raise ErrorWithAnalyzedFileLocation.new(cause: e, node: node, cop: cop)
end
end
def add_offense(node_or_range, location: :expression, message: nil, severity: nil, &block)
@v0_argument = node_or_range
range = find_location(node_or_range, location)
if block.nil? && !support_autocorrect?
super(range, message: message, severity: severity)
else
super(range, message: message, severity: severity) do |corrector|
emulate_v0_callsequence(corrector, &block)
end
end
end
def find_location(node, loc)
# Location can be provided as a symbol, e.g.: `:keyword`
loc.is_a?(Symbol) ? node.loc.public_send(loc) : loc
end
# @deprecated Use class method
def support_autocorrect?
# warn 'deprecated, use cop.class.support_autocorrect?' TODO
self.class.support_autocorrect?
end
def self.support_autocorrect?
method_defined?(:autocorrect)
end
def self.joining_forces
return unless method_defined?(:join_force?)
cop = new
Force.all.select { |force_class| cop.join_force?(force_class) }
end
# @deprecated
def corrections
# warn 'Cop#corrections is deprecated' TODO
return [] unless @last_corrector
Legacy::CorrectionsProxy.new(@last_corrector)
end
# Called before all on_... have been called
def on_new_investigation
investigate(processed_source) if respond_to?(:investigate)
super
end
# Called after all on_... have been called
def on_investigation_end
investigate_post_walk(processed_source) if respond_to?(:investigate_post_walk)
super
end
### Deprecated registry access
# @deprecated Use Registry.global
def self.registry
Registry.global
end
# @deprecated Use Registry.all
def self.all
Registry.all
end
# @deprecated Use Registry.qualified_cop_name
def self.qualified_cop_name(name, origin)
Registry.qualified_cop_name(name, origin)
end
private
def begin_investigation(processed_source)
super
@offenses = @current_offenses
@last_corrector = @current_corrector
end
# Override Base
def callback_argument(_range)
@v0_argument
end
def apply_correction(corrector)
suppress_clobbering { super }
end
# Just for legacy
def emulate_v0_callsequence(corrector)
lambda = correction_lambda
yield corrector if block_given?
unless corrector.empty?
raise 'Your cop must inherit from Cop::Base and extend AutoCorrector'
end
return unless lambda
suppress_clobbering { lambda.call(corrector) }
end
def correction_lambda
return unless support_autocorrect?
dedup_on_node(@v0_argument) { autocorrect(@v0_argument) }
end
def dedup_on_node(node)
@corrected_nodes ||= {}.compare_by_identity
yield unless @corrected_nodes.key?(node)
ensure
@corrected_nodes[node] = true
end
def suppress_clobbering
yield
rescue ::Parser::ClobberingError
# ignore Clobbering errors
end
end
end
end