Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions lib/css_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ module CssParser
# TODO: declaration_hashes should be able to contain a RuleSet
# this should be a Class method
def self.merge(*rule_sets)
@folded_declaration_cache = {}

@sokolikp sokolikp Feb 23, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line was setting @folded_declaration_cache on the CssParser module object itself — completely separate from both the class-level one on Parser and the per-instance one created in reset!. Three different Ruby objects all had an ivar with the same name, but nothing ever read from this one.


# in case called like CssParser.merge([rule_set, rule_set])
rule_sets.flatten! if rule_sets[0].is_a?(Array)

Expand Down
6 changes: 0 additions & 6 deletions lib/css_parser/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ class Parser
# Array of CSS files that have been loaded.
attr_reader :loaded_uris

#--
# Class variable? see http://www.oreillynet.com/ruby/blog/2007/01/nubygems_dont_use_class_variab_1.html
#++
@folded_declaration_cache = {}
class << self; attr_reader :folded_declaration_cache; end

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class-level @folded_declaration_cache and its accessor were dead code. Nothing ever reads Parser.folded_declaration_cache (the class method). All actual usage (lines 693, 698, 702) references @folded_declaration_cache on self — the instance. The reset! method (called from initialize) creates a fresh per-instance @folded_declaration_cache = {} that shadows the class-level one. So the class-level hash just accumulated stale data forever with nothing reading it. Same story for the @folded_declaration_cache = {} reset in CssParser.merge — it was setting it on the CssParser module object, which nothing ever reads.


def initialize(options = {})
@options = {
absolute_paths: false,
Expand Down
42 changes: 42 additions & 0 deletions test/test_css_parser_folded_declaration_cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require_relative 'test_helper'

class CssParserFoldedDeclarationCacheTest < Minitest::Test
include CssParser

def test_folded_declaration_cache_is_per_instance
cp1 = Parser.new
cp2 = Parser.new

cp1.add_block!('p { color: red; }')
cp2.add_block!('p { color: blue; }')

cache1 = cp1.send(:instance_variable_get, :@folded_declaration_cache)
cache2 = cp2.send(:instance_variable_get, :@folded_declaration_cache)

refute_same cache1, cache2
end

def test_folded_declaration_cache_does_not_persist_across_instances
cp1 = Parser.new
cp1.add_block!('p { color: red; }')

cp2 = Parser.new
cache = cp2.send(:instance_variable_get, :@folded_declaration_cache)

assert_empty cache
end

def test_no_class_level_folded_declaration_cache
refute Parser.respond_to?(:folded_declaration_cache),
'Parser should not have a class-level folded_declaration_cache accessor'
end

def test_cache_is_reset_on_new_instance
cp = Parser.new
cache = cp.send(:instance_variable_get, :@folded_declaration_cache)
assert_instance_of Hash, cache
assert_empty cache
end
end
Loading