Skip to content

Commit 9c8ba02

Browse files
authored
Merge pull request #172 from tagliala/chore/171-rubocop-performance
Introduce RuboCop Performance
2 parents f5bfeef + a707feb commit 9c8ba02

File tree

7 files changed

+25
-10
lines changed

7 files changed

+25
-10
lines changed

.rubocop.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require:
2+
- rubocop-performance
23
- rubocop-rake
34

45
AllCops:
@@ -41,6 +42,13 @@ Metrics/ModuleLength:
4142
Metrics/PerceivedComplexity:
4243
Enabled: false
4344

45+
# TODO: Progressively fix performance offences and remove this setting
46+
Performance:
47+
Enabled: false
48+
49+
Performance/RegexpMatch:
50+
Enabled: true
51+
4452
Style/AndOr:
4553
Enabled: false
4654

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Unreleased
44

5+
* Minor performance improvements
6+
57
### Version v1.20.0
68

79
* Remove `iconv` conditional require

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ gem 'maxitest'
1212
gem 'memory_profiler'
1313
gem 'rake'
1414
gem 'rubocop'
15+
gem 'rubocop-performance'
1516
gem 'rubocop-rake'
1617
gem 'webrick'

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ GEM
4343
unicode-display_width (>= 2.4.0, < 3.0)
4444
rubocop-ast (1.31.2)
4545
parser (>= 3.3.0.4)
46+
rubocop-performance (1.23.0)
47+
rubocop (>= 1.48.1, < 2.0)
48+
rubocop-ast (>= 1.31.1, < 2.0)
4649
rubocop-rake (0.6.0)
4750
rubocop (~> 1.0)
4851
ruby-progressbar (1.13.0)
@@ -61,6 +64,7 @@ DEPENDENCIES
6164
memory_profiler
6265
rake
6366
rubocop
67+
rubocop-performance
6468
rubocop-rake
6569
webrick
6670

lib/css_parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def self.convert_uris(css, base_uri)
138138
css.gsub(URI_RX) do
139139
uri = Regexp.last_match(1).to_s.gsub(/["']+/, '')
140140
# Don't process URLs that are already absolute
141-
unless uri.match(%r{^[a-z]+://}i)
141+
unless uri.match?(%r{^[a-z]+://}i)
142142
begin
143143
uri = base_uri.join(uri)
144144
rescue

lib/css_parser/parser.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ def parse_block_into_rule_sets!(block, options = {}) # :nodoc:
416416
# restart our search for selectors and declarations
417417
rule_start = nil if options[:capture_offsets]
418418
end
419-
elsif token =~ /@media/i
419+
elsif /@media/i.match?(token)
420420
# found '@media', reset current media_types
421421
in_at_media_rule = true
422422
current_media_queries = []
@@ -718,7 +718,7 @@ def css_node_to_h(hash, key, val)
718718
nodes = {}
719719
lines.each do |line|
720720
parts = line.split(':', 2)
721-
if parts[1] =~ /:/
721+
if /:/.match?(parts[1])
722722
nodes[parts[0]] = css_node_to_h(hash, parts[0], parts[1])
723723
else
724724
nodes[parts[0].to_s.strip] = parts[1].to_s.strip

lib/css_parser/rule_set.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -455,17 +455,17 @@ def expand_font_shorthand! # :nodoc:
455455
else
456456
font_props['font-family'] = m
457457
end
458-
elsif m =~ /normal|inherit/i
458+
elsif /normal|inherit/i.match?(m)
459459
['font-style', 'font-weight', 'font-variant'].each do |font_prop|
460460
font_props[font_prop] ||= m
461461
end
462-
elsif m =~ /italic|oblique/i
462+
elsif /italic|oblique/i.match?(m)
463463
font_props['font-style'] = m
464-
elsif m =~ /small-caps/i
464+
elsif /small-caps/i.match?(m)
465465
font_props['font-variant'] = m
466-
elsif m =~ /[1-9]00$|bold|bolder|lighter/i
466+
elsif /[1-9]00$|bold|bolder|lighter/i.match?(m)
467467
font_props['font-weight'] = m
468-
elsif m =~ CssParser::FONT_UNITS_RX
468+
elsif CssParser::FONT_UNITS_RX.match?(m)
469469
if m.include?('/')
470470
font_props['font-size'], font_props['line-height'] = m.split('/', 2)
471471
else
@@ -488,7 +488,7 @@ def expand_list_style_shorthand! # :nodoc:
488488
value = declaration.value.dup
489489

490490
replacement =
491-
if value =~ CssParser::RE_INHERIT
491+
if CssParser::RE_INHERIT.match?(value)
492492
LIST_STYLE_PROPERTIES.to_h { |key| [key, 'inherit'] }
493493
else
494494
{
@@ -559,7 +559,7 @@ def create_border_shorthand! # :nodoc:
559559
next if declaration.important
560560
# can't merge if any value contains a space (i.e. has multiple values)
561561
# we temporarily remove any spaces after commas for the check (inside rgba, etc...)
562-
next if declaration.value.gsub(/,\s/, ',').strip =~ /\s/
562+
next if /\s/.match?(declaration.value.gsub(/,\s/, ',').strip)
563563

564564
declaration.value
565565
end.compact

0 commit comments

Comments
 (0)