diff --git a/.rubocop.yml b/.rubocop.yml index 03af185..6624266 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,4 +1,5 @@ require: + - rubocop-performance - rubocop-rake AllCops: @@ -41,6 +42,13 @@ Metrics/ModuleLength: Metrics/PerceivedComplexity: Enabled: false +# TODO: Progressively fix performance offences and remove this setting +Performance: + Enabled: false + +Performance/RegexpMatch: + Enabled: true + Style/AndOr: Enabled: false diff --git a/CHANGELOG.md b/CHANGELOG.md index f26d991..e0b0074 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Unreleased +* Minor performance improvements + ### Version v1.20.0 * Remove `iconv` conditional require diff --git a/Gemfile b/Gemfile index 1ca5eff..4f83719 100644 --- a/Gemfile +++ b/Gemfile @@ -12,5 +12,6 @@ gem 'maxitest' gem 'memory_profiler' gem 'rake' gem 'rubocop' +gem 'rubocop-performance' gem 'rubocop-rake' gem 'webrick' diff --git a/Gemfile.lock b/Gemfile.lock index cd6ede7..a8bc568 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -43,6 +43,9 @@ GEM unicode-display_width (>= 2.4.0, < 3.0) rubocop-ast (1.31.2) parser (>= 3.3.0.4) + rubocop-performance (1.23.0) + rubocop (>= 1.48.1, < 2.0) + rubocop-ast (>= 1.31.1, < 2.0) rubocop-rake (0.6.0) rubocop (~> 1.0) ruby-progressbar (1.13.0) @@ -61,6 +64,7 @@ DEPENDENCIES memory_profiler rake rubocop + rubocop-performance rubocop-rake webrick diff --git a/lib/css_parser.rb b/lib/css_parser.rb index 2fbbd20..774245d 100644 --- a/lib/css_parser.rb +++ b/lib/css_parser.rb @@ -138,7 +138,7 @@ def self.convert_uris(css, base_uri) css.gsub(URI_RX) do uri = Regexp.last_match(1).to_s.gsub(/["']+/, '') # Don't process URLs that are already absolute - unless uri.match(%r{^[a-z]+://}i) + unless uri.match?(%r{^[a-z]+://}i) begin uri = base_uri.join(uri) rescue diff --git a/lib/css_parser/parser.rb b/lib/css_parser/parser.rb index e199e3b..f027ce8 100644 --- a/lib/css_parser/parser.rb +++ b/lib/css_parser/parser.rb @@ -416,7 +416,7 @@ def parse_block_into_rule_sets!(block, options = {}) # :nodoc: # restart our search for selectors and declarations rule_start = nil if options[:capture_offsets] end - elsif token =~ /@media/i + elsif /@media/i.match?(token) # found '@media', reset current media_types in_at_media_rule = true current_media_queries = [] @@ -718,7 +718,7 @@ def css_node_to_h(hash, key, val) nodes = {} lines.each do |line| parts = line.split(':', 2) - if parts[1] =~ /:/ + if /:/.match?(parts[1]) nodes[parts[0]] = css_node_to_h(hash, parts[0], parts[1]) else nodes[parts[0].to_s.strip] = parts[1].to_s.strip diff --git a/lib/css_parser/rule_set.rb b/lib/css_parser/rule_set.rb index 58ce513..d1bd4a8 100644 --- a/lib/css_parser/rule_set.rb +++ b/lib/css_parser/rule_set.rb @@ -455,17 +455,17 @@ def expand_font_shorthand! # :nodoc: else font_props['font-family'] = m end - elsif m =~ /normal|inherit/i + elsif /normal|inherit/i.match?(m) ['font-style', 'font-weight', 'font-variant'].each do |font_prop| font_props[font_prop] ||= m end - elsif m =~ /italic|oblique/i + elsif /italic|oblique/i.match?(m) font_props['font-style'] = m - elsif m =~ /small-caps/i + elsif /small-caps/i.match?(m) font_props['font-variant'] = m - elsif m =~ /[1-9]00$|bold|bolder|lighter/i + elsif /[1-9]00$|bold|bolder|lighter/i.match?(m) font_props['font-weight'] = m - elsif m =~ CssParser::FONT_UNITS_RX + elsif CssParser::FONT_UNITS_RX.match?(m) if m.include?('/') font_props['font-size'], font_props['line-height'] = m.split('/', 2) else @@ -488,7 +488,7 @@ def expand_list_style_shorthand! # :nodoc: value = declaration.value.dup replacement = - if value =~ CssParser::RE_INHERIT + if CssParser::RE_INHERIT.match?(value) LIST_STYLE_PROPERTIES.to_h { |key| [key, 'inherit'] } else { @@ -559,7 +559,7 @@ def create_border_shorthand! # :nodoc: next if declaration.important # can't merge if any value contains a space (i.e. has multiple values) # we temporarily remove any spaces after commas for the check (inside rgba, etc...) - next if declaration.value.gsub(/,\s/, ',').strip =~ /\s/ + next if /\s/.match?(declaration.value.gsub(/,\s/, ',').strip) declaration.value end.compact