Skip to content

Commit

Permalink
Remove deprecation from v1
Browse files Browse the repository at this point in the history
  • Loading branch information
stoivo committed May 27, 2024
1 parent c60260d commit 929d3da
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 100 deletions.
50 changes: 8 additions & 42 deletions lib/css_parser/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,49 +231,15 @@ def add_block!(block, options = {})
# optional fields for source location for source location
# +filename+ can be a string or uri pointing to the file or url location.
# +offset+ should be Range object representing the start and end byte locations where the rule was found in the file.
def add_rule!(*args, selectors: nil, block: nil, filename: nil, offset: nil, media_types: :all) # rubocop:disable Metrics/ParameterLists
if args.any?
media_types = nil
if selectors || block || filename || offset || media_types
raise ArgumentError, "don't mix positional and keyword arguments arguments"
end

warn '[DEPRECATION] `add_rule!` with positional arguments is deprecated. ' \
'Please use keyword arguments instead.', uplevel: 1

case args.length
when 2
selectors, block = args
when 3
selectors, block, media_types = args
else
raise ArgumentError
end
end

begin
rule_set = RuleSet.new(
selectors: selectors, block: block,
offset: offset, filename: filename
)

add_rule_set!(rule_set, media_types)
rescue CssParser::Error => e
raise e if @options[:rule_set_exceptions]
end
end

# Add a CSS rule by setting the +selectors+, +declarations+, +filename+, +offset+ and +media_types+.
#
# +filename+ can be a string or uri pointing to the file or url location.
# +offset+ should be Range object representing the start and end byte locations where the rule was found in the file.
# +media_types+ can be a symbol or an array of symbols.
def add_rule_with_offsets!(selectors, declarations, filename, offset, media_types = :all)
warn '[DEPRECATION] `add_rule_with_offsets!` is deprecated. Please use `add_rule!` instead.', uplevel: 1
add_rule!(
selectors: selectors, block: declarations, media_types: media_types,
filename: filename, offset: offset
def add_rule!(selectors: nil, block: nil, filename: nil, offset: nil, media_types: :all) # rubocop:disable Metrics/ParameterLists
rule_set = RuleSet.new(
selectors: selectors, block: block,
offset: offset, filename: filename
)

add_rule_set!(rule_set, media_types)
rescue CssParser::Error => e
raise e if @options[:rule_set_exceptions]
end

# Add a CssParser RuleSet object.
Expand Down
23 changes: 1 addition & 22 deletions lib/css_parser/rule_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,7 @@ class RuleSet
alias []= add_declaration!
alias remove_declaration! delete

def initialize(*args, selectors: nil, block: nil, offset: nil, filename: nil, specificity: nil) # rubocop:disable Metrics/ParameterLists
if args.any?
if selectors || block || offset || filename || specificity
raise ArgumentError, "don't mix positional and keyword arguments"
end

warn '[DEPRECATION] positional arguments are deprecated use keyword instead.', uplevel: 1

case args.length
when 2
selectors, block = args
when 3
selectors, block, specificity = args
when 4
filename, offset, selectors, block = args
when 5
filename, offset, selectors, block, specificity = args
else
raise ArgumentError
end
end

def initialize(selectors: nil, block: nil, offset: nil, filename: nil, specificity: nil) # rubocop:disable Metrics/ParameterLists
@selectors = []
@specificity = specificity

Expand Down
36 changes: 0 additions & 36 deletions test/test_css_parser_misc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,40 +237,4 @@ def test_catching_argument_exceptions_for_add_rule
cp_without_exceptions = Parser.new(rule_set_exceptions: false)
cp_without_exceptions.add_rule!(selectors: 'body', block: 'background-color: !important')
end

def test_catching_argument_exceptions_for_add_rule_positional
cp_with_exceptions = Parser.new(rule_set_exceptions: true)

error = assert_raises CssParser::EmptyValueError do
_, err = capture_io do
cp_with_exceptions.add_rule!('body', 'background-color: !important')
end
assert_includes err, "DEPRECATION"
end
assert_equal error.message, 'background-color value is empty'

cp_without_exceptions = Parser.new(rule_set_exceptions: false)
_, err = capture_io do
cp_without_exceptions.add_rule!('body', 'background-color: !important')
end
assert_includes err, "DEPRECATION"
end

def test_catching_argument_exceptions_for_add_rule_with_offsets
cp_with_exceptions = Parser.new(capture_offsets: true, rule_set_exceptions: true)

error = assert_raises CssParser::EmptyValueError do
_, err = capture_io do
cp_with_exceptions.add_rule_with_offsets!('body', 'background-color: !important', 'inline', 1)
end
assert_includes err, "DEPRECATION"
end
assert_equal error.message, 'background-color value is empty'

cp_without_exceptions = Parser.new(capture_offsets: true, rule_set_exceptions: false)
_, err = capture_io do
cp_without_exceptions.add_rule_with_offsets!('body', 'background-color: !important', 'inline', 1)
end
assert_includes err, "DEPRECATION"
end
end

0 comments on commit 929d3da

Please sign in to comment.