Skip to content

Commit 76d4fe4

Browse files
committed
(PUP-11993) Style/RedundantRegexpCharacterClass
This commit enables the Style/RedundantRegexpCharacterClass cop and fixes 32 autocorrectable offenses.
1 parent d9b9a3c commit 76d4fe4

File tree

12 files changed

+23
-38
lines changed

12 files changed

+23
-38
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -668,21 +668,6 @@ Style/RedundantFetchBlock:
668668
Style/RedundantInterpolation:
669669
Enabled: false
670670

671-
# This cop supports safe auto-correction (--auto-correct).
672-
Style/RedundantRegexpCharacterClass:
673-
Exclude:
674-
- 'lib/puppet/application/apply.rb'
675-
- 'lib/puppet/module.rb'
676-
- 'lib/puppet/pops/lookup/interpolation.rb'
677-
- 'lib/puppet/pops/parser/lexer2.rb'
678-
- 'lib/puppet/pops/parser/slurp_support.rb'
679-
- 'lib/puppet/pops/patterns.rb'
680-
- 'lib/puppet/pops/pcore.rb'
681-
- 'lib/puppet/provider/package/openbsd.rb'
682-
- 'lib/puppet/settings/config_file.rb'
683-
- 'lib/puppet/type/exec.rb'
684-
- 'lib/puppet/util/command_line/trollop.rb'
685-
686671
# This cop supports safe auto-correction (--auto-correct).
687672
Style/RedundantRegexpEscape:
688673
Enabled: false

lib/puppet/application/apply.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def main
224224
$stderr.puts _("%{file} is not readable") % { file: file }
225225
exit(63)
226226
end
227-
node.classes = Puppet::FileSystem.read(file, :encoding => 'utf-8').split(/[\s]+/)
227+
node.classes = Puppet::FileSystem.read(file, :encoding => 'utf-8').split(/\s+/)
228228
end
229229
end
230230

lib/puppet/module.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def self.is_module_directory_name?(name)
5858

5959
def self.is_module_namespaced_name?(name)
6060
# it must match the full module name according to forge validator
61-
return true if name =~ /^[a-zA-Z0-9]+[-][a-z][a-z0-9_]*$/
61+
return true if name =~ /^[a-zA-Z0-9]+-[a-z][a-z0-9_]*$/
6262

6363
return false
6464
end

lib/puppet/pops/lookup/interpolation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def nil_in_scope?(scope, key)
144144
end
145145

146146
def get_method_and_data(data, allow_methods)
147-
match = data.match(/^(\w+)\((?:["]([^"]+)["]|[']([^']+)['])\)$/)
147+
match = data.match(/^(\w+)\((?:"([^"]+)"|'([^']+)')\)$/)
148148
if match
149149
fail(Issues::HIERA_INTERPOLATION_METHOD_SYNTAX_NOT_ALLOWED) unless allow_methods
150150

lib/puppet/pops/parser/lexer2.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,10 @@ class Lexer2
170170
# The NAME and CLASSREF in 4x are strict. Each segment must start with
171171
# a letter a-z and may not contain dashes (\w includes letters, digits and _).
172172
#
173-
PATTERN_CLASSREF = %r{((::){0,1}[A-Z][\w]*)+}
174-
PATTERN_NAME = %r{^((::)?[a-z][\w]*)(::[a-z][\w]*)*$}
173+
PATTERN_CLASSREF = %r{((::){0,1}[A-Z]\w*)+}
174+
PATTERN_NAME = %r{^((::)?[a-z]\w*)(::[a-z]\w*)*$}
175175

176-
PATTERN_BARE_WORD = %r{((?:::){0,1}(?:[a-z_](?:[\w-]*[\w])?))+}
176+
PATTERN_BARE_WORD = %r{((?:::){0,1}(?:[a-z_](?:[\w-]*\w)?))+}
177177

178178
PATTERN_DOLLAR_VAR = %r{\$(::)?(\w+::)*\w+}
179179
PATTERN_NUMBER = %r{\b(?:0[xX][0-9A-Fa-f]+|0?\d+(?:\.\d+)?(?:[eE]-?\d+)?)\b}

lib/puppet/pops/parser/slurp_support.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ module Parser
1313
module SlurpSupport
1414
include LexerSupport
1515

16-
SLURP_SQ_PATTERN = /(?:[^\\]|^)(?:[\\]{2})*[']/
17-
SLURP_DQ_PATTERN = /(?:[^\\]|^)(?:[\\]{2})*(["]|[$]\{?)/
18-
SLURP_UQ_PATTERN = /(?:[^\\]|^)(?:[\\]{2})*([$]\{?|\z)/
16+
SLURP_SQ_PATTERN = /(?:[^\\]|^)(?:\\{2})*'/
17+
SLURP_DQ_PATTERN = /(?:[^\\]|^)(?:\\{2})*("|[$]\{?)/
18+
SLURP_UQ_PATTERN = /(?:[^\\]|^)(?:\\{2})*([$]\{?|\z)/
1919
# unquoted, no escapes
2020
SLURP_UQNE_PATTERN = /(\$\{?|\z)/m
2121
SLURP_ALL_PATTERN = /.*(\z)/m
@@ -76,7 +76,7 @@ def slurp(scanner, pattern, escapes, ignore_invalid_escapes)
7676
# If later a \u is found it is warned not to be a unicode escape
7777
if escapes.include?('u')
7878
# gsub must be repeated to cater for adjacent escapes
79-
while str.gsub!(/((?:[^\\]|^)(?:[\\]{2})*)\\u(?:([\da-fA-F]{4})|\{([\da-fA-F]{1,6})\})/m) { ::Regexp.last_match(1) + [(::Regexp.last_match(2) || ::Regexp.last_match(3)).hex].pack("U") }
79+
while str.gsub!(/((?:[^\\]|^)(?:\\{2})*)\\u(?:([\da-fA-F]{4})|\{([\da-fA-F]{1,6})\})/m) { ::Regexp.last_match(1) + [(::Regexp.last_match(2) || ::Regexp.last_match(3)).hex].pack("U") }
8080
# empty block. Everything happens in the gsub block
8181
end
8282
end

lib/puppet/pops/patterns.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ module Puppet::Pops::Patterns
2929
# CLASSREF_EXT matches a class reference the same way as the lexer - i.e. the external source form
3030
# where each part must start with a capital letter A-Z.
3131
#
32-
CLASSREF_EXT = %r{\A((::){0,1}[A-Z][\w]*)+\z}
32+
CLASSREF_EXT = %r{\A((::){0,1}[A-Z]\w*)+\z}
3333

3434
# Same as CLASSREF_EXT but cannot start with '::'
3535
#
36-
CLASSREF_EXT_DECL = %r{\A[A-Z][\w]*(?:::[A-Z][\w]*)*\z}
36+
CLASSREF_EXT_DECL = %r{\A[A-Z]\w*(?:::[A-Z]\w*)*\z}
3737

3838
# CLASSREF matches a class reference the way it is represented internally in the
3939
# model (i.e. in lower case).
4040
#
41-
CLASSREF = %r{\A((::){0,1}[a-z][\w]*)+\z}
41+
CLASSREF = %r{\A((::){0,1}[a-z]\w*)+\z}
4242

4343
# Same as CLASSREF but cannot start with '::'
4444
#
45-
CLASSREF_DECL = %r{\A[a-z][\w]*(?:::[a-z][\w]*)*\z}
45+
CLASSREF_DECL = %r{\A[a-z]\w*(?:::[a-z]\w*)*\z}
4646

4747
# DOLLAR_VAR matches a variable name including the initial $ character
4848
DOLLAR_VAR = %r{\$(::)?(\w+::)*\w+}

lib/puppet/pops/pcore.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Pcore
1010
TYPE_URI = Types::TypeFactory.pattern(TYPE_URI_RX)
1111
TYPE_URI_ALIAS = Types::PTypeAliasType.new('Pcore::URI', nil, TYPE_URI)
1212
TYPE_SIMPLE_TYPE_NAME = Types::TypeFactory.pattern(/\A[A-Z]\w*\z/)
13-
TYPE_QUALIFIED_REFERENCE = Types::TypeFactory.pattern(/\A[A-Z][\w]*(?:::[A-Z][\w]*)*\z/)
13+
TYPE_QUALIFIED_REFERENCE = Types::TypeFactory.pattern(/\A[A-Z]\w*(?:::[A-Z]\w*)*\z/)
1414
TYPE_MEMBER_NAME = Types::PPatternType.new([Types::PRegexpType.new(Patterns::PARAM_NAME)])
1515

1616
KEY_PCORE_URI = 'pcore_uri'

lib/puppet/provider/package/openbsd.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def self.instances
3030
begin
3131
execpipe(listcmd) do |process|
3232
# our regex for matching pkg_info output
33-
regex = /^(.*)-(\d[^-]*)[-]?([\w-]*)(.*)$/
33+
regex = /^(.*)-(\d[^-]*)-?([\w-]*)(.*)$/
3434
fields = [:name, :ensure, :flavor]
3535
hash = {}
3636

@@ -89,15 +89,15 @@ def latest
8989
return version
9090
else
9191
# Remove all fuzzy matches first.
92-
output = output.split.select { |p| p =~ /^#{resource[:name]}-(\d[^-]*)[-]?(\w*)/ }.join
92+
output = output.split.select { |p| p =~ /^#{resource[:name]}-(\d[^-]*)-?(\w*)/ }.join
9393
debug "pkg_info -Q for #{resource[:name]}: #{output}"
9494
end
9595

96-
if output =~ /^#{resource[:name]}-(\d[^-]*)[-]?(\w*) \(installed\)$/
96+
if output =~ /^#{resource[:name]}-(\d[^-]*)-?(\w*) \(installed\)$/
9797
debug "Package is already the latest available"
9898
return version
9999
else
100-
match = /^(.*)-(\d[^-]*)[-]?(\w*)$/.match(output)
100+
match = /^(.*)-(\d[^-]*)-?(\w*)$/.match(output)
101101
debug "Latest available for #{resource[:name]}: #{match[2]}"
102102

103103
if version.to_sym == :absent || version.to_sym == :purged
@@ -201,7 +201,7 @@ def get_full_name(latest = false)
201201
def get_version
202202
execpipe([command(:pkginfo), "-I", @resource[:name]]) do |process|
203203
# our regex for matching pkg_info output
204-
regex = /^(.*)-(\d[^-]*)[-]?(\w*)(.*)$/
204+
regex = /^(.*)-(\d[^-]*)-?(\w*)(.*)$/
205205
master_version = 0
206206
version = -1
207207

lib/puppet/settings/config_file.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def extract_fileinfo(string)
125125
value = string.sub(/\{\s*([^}]+)\s*\}/) do
126126
params = ::Regexp.last_match(1)
127127
params.split(/\s*,\s*/).each do |str|
128-
if str =~ /^\s*(\w+)\s*=\s*([\w]+)\s*$/
128+
if str =~ /^\s*(\w+)\s*=\s*(\w+)\s*$/
129129
param = ::Regexp.last_match(1).intern
130130
value = ::Regexp.last_match(2)
131131
result[param] = value

0 commit comments

Comments
 (0)