/
word_array.rb
101 lines (88 loc) · 2.88 KB
/
word_array.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
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# This cop can check for array literals made up of word-like
# strings, that are not using the %w() syntax.
#
# Alternatively, it can check for uses of the %w() syntax, in projects
# which do not want to include that syntax.
#
# NOTE: When using the `percent` style, %w() arrays containing a space
# will be registered as offenses.
#
# Configuration option: MinSize
# If set, arrays with fewer elements than this value will not trigger the
# cop. For example, a `MinSize` of `3` will not enforce a style on an
# array of 2 or fewer elements.
#
# @example EnforcedStyle: percent (default)
# # good
# %w[foo bar baz]
#
# # bad
# ['foo', 'bar', 'baz']
#
# # bad (contains spaces)
# %w[foo\ bar baz\ quux]
#
# @example EnforcedStyle: brackets
# # good
# ['foo', 'bar', 'baz']
#
# # bad
# %w[foo bar baz]
#
# # good (contains spaces)
# ['foo bar', 'baz quux']
class WordArray < Base
include ArrayMinSize
include ArraySyntax
include ConfigurableEnforcedStyle
include PercentArray
extend AutoCorrector
PERCENT_MSG = 'Use `%w` or `%W` for an array of words.'
ARRAY_MSG = 'Use `[]` for an array of words.'
class << self
attr_accessor :largest_brackets
end
def on_array(node)
if bracketed_array_of?(:str, node)
return if complex_content?(node.values)
check_bracketed_array(node, 'w')
elsif node.percent_literal?(:string)
check_percent_array(node)
end
end
private
def complex_content?(strings, complex_regex: word_regex)
strings.any? do |s|
next unless s.str_content
string = s.str_content.dup.force_encoding(::Encoding::UTF_8)
!string.valid_encoding? ||
(complex_regex && !complex_regex.match?(string)) ||
/ /.match?(string)
end
end
def invalid_percent_array_contents?(node)
# Disallow %w() arrays that contain invalid encoding or spaces
complex_content?(node.values, complex_regex: false)
end
def word_regex
Regexp.new(cop_config['WordRegex'])
end
def correct_bracketed(corrector, node)
words = node.children.map do |word|
if word.dstr_type?
string_literal = to_string_literal(word.source)
trim_string_interporation_escape_character(string_literal)
else
to_string_literal(word.children[0])
end
end
corrector.replace(node, "[#{words.join(', ')}]")
end
end
end
end
end