/
literal_in_interpolation.rb
91 lines (77 loc) · 2.42 KB
/
literal_in_interpolation.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
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
# This cop checks for interpolated literals.
#
# @example
#
# # bad
#
# "result is #{10}"
#
# @example
#
# # good
#
# "result is 10"
class LiteralInInterpolation < Cop
include RangeHelp
include PercentLiteral
MSG = 'Literal interpolation detected.'.freeze
COMPOSITE = %i[array hash pair irange erange].freeze
def on_dstr(node)
node.each_child_node(:begin) do |begin_node|
final_node = begin_node.children.last
next unless final_node
next if special_keyword?(final_node)
next unless prints_as_self?(final_node)
add_offense(final_node)
end
end
def autocorrect(node)
return if node.dstr_type? # nested, fixed in next iteration
value = autocorrected_value(node)
->(corrector) { corrector.replace(node.parent.source_range, value) }
end
private
def special_keyword?(node)
# handle strings like __FILE__
(node.str_type? && !node.loc.respond_to?(:begin)) ||
node.source_range.is?('__LINE__')
end
def autocorrected_value(node)
case node.type
when :int
node.children.last.to_i.to_s
when :float
node.children.last.to_f.to_s
when :str
node.children.last
when :sym
autocorrected_value_for_symbol(node)
when :array
autocorrected_value_for_array(node)
else
node.source.gsub('"', '\"')
end
end
def autocorrected_value_for_symbol(node)
end_pos =
node.loc.end ? node.loc.end.begin_pos : node.loc.expression.end_pos
range_between(node.loc.begin.end_pos, end_pos).source
end
def autocorrected_value_for_array(node)
return node.source.gsub('"', '\"') unless node.percent_literal?
contents_range(node).source.split(' ').to_s.gsub('"', '\"')
end
# Does node print its own source when converted to a string?
def prints_as_self?(node)
node.basic_literal? ||
(COMPOSITE.include?(node.type) &&
node.children.all? { |child| prints_as_self?(child) })
end
end
end
end
end