Skip to content

Commit

Permalink
Give Sass::ScriptError a name parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
ntkme committed Sep 13, 2022
1 parent b08a6fd commit c9d4671
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 31 deletions.
6 changes: 5 additions & 1 deletion lib/sass/script_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@

module Sass
# An exception thrown by Sass Script.
class ScriptError < StandardError; end
class ScriptError < StandardError
def initialize(message, name = nil)
super(name.nil? ? message : "$#{name}: #{message}")
end
end
end
22 changes: 9 additions & 13 deletions lib/sass/value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,52 +57,52 @@ def to_nil
# @return [Boolean]
# @raise [ScriptError]
def assert_boolean(name = nil)
raise error("#{self} is not a boolean", name)
raise Sass::ScriptError.new("#{self} is not a boolean", name)
end

# @raise [ScriptError]
def assert_calculation(name = nil)
raise error("#{self} is not a calculation", name)
raise Sass::ScriptError.new("#{self} is not a calculation", name)
end

# @return [Color]
# @raise [ScriptError]
def assert_color(name = nil)
raise error("#{self} is not a color", name)
raise Sass::ScriptError.new("#{self} is not a color", name)
end

# @return [Function]
# @raise [ScriptError]
def assert_function(name = nil)
raise error("#{self} is not a function", name)
raise Sass::ScriptError.new("#{self} is not a function", name)
end

# @return [Map]
# @raise [ScriptError]
def assert_map(name = nil)
raise error("#{self} is not a map", name)
raise Sass::ScriptError.new("#{self} is not a map", name)
end

# @return [Number]
# @raise [ScriptError]
def assert_number(name = nil)
raise error("#{self} is not a number", name)
raise Sass::ScriptError.new("#{self} is not a number", name)
end

# @return [String]
# @raise [ScriptError]
def assert_string(name = nil)
raise error("#{self} is not a string", name)
raise Sass::ScriptError.new("#{self} is not a string", name)
end

# @param sass_index [Number]
# @return [Integer]
def sass_index_to_array_index(sass_index, name = nil)
index = sass_index.assert_number(name).assert_integer(name)
raise error('List index may not be 0', name) if index.zero?
raise Sass::ScriptError.new('List index may not be 0', name) if index.zero?

if index.abs > to_a_length
raise error("Invalid index #{sass_index} for a list with #{to_a_length} elements", name)
raise Sass::ScriptError.new("Invalid index #{sass_index} for a list with #{to_a_length} elements", name)
end

index.negative? ? to_a_length + index : index - 1
Expand All @@ -113,10 +113,6 @@ def sass_index_to_array_index(sass_index, name = nil)
def to_a_length
1
end

def error(message, name = nil)
Sass::ScriptError.new name.nil? ? message : "$#{name}: #{message}"
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/sass/value/color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def initialize(red: nil,
hwb_to_rgb
@whiteness = @blackness = nil
else
raise error 'Invalid Color'
raise Sass::ScriptError, 'Invalid Color'
end
end

Expand Down
3 changes: 1 addition & 2 deletions lib/sass/value/fuzzy_math.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ def assert_between(number, min, max, name)
result = between(number, min, max)
return result unless result.nil?

message = "#{number} must be between #{min} and #{max}."
raise Sass::ScriptError, name.nil? ? message : "$#{name}: #{message}"
raise Sass::ScriptError.new("#{number} must be between #{min} and #{max}.", name)
end

def hash(number)
Expand Down
2 changes: 1 addition & 1 deletion lib/sass/value/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class List
# @param bracketed [::Boolean]
def initialize(contents = [], separator: ',', bracketed: false)
if separator.nil? && contents.length > 1
raise error 'A list with more than one element must have an explicit separator'
raise Sass::ScriptError, 'A list with more than one element must have an explicit separator'
end

@contents = contents.freeze
Expand Down
29 changes: 18 additions & 11 deletions lib/sass/value/number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ def initialize(value, unit = nil)
denominator_units = []
when ::Hash
numerator_units = unit.fetch(:numerator_units, [])
raise error "invalid numerator_units #{numerator_units.inspect}" unless numerator_units.is_a? Array
unless numerator_units.is_a? Array
raise Sass::ScriptError, "invalid numerator_units #{numerator_units.inspect}"
end

denominator_units = unit.fetch(:denominator_units, [])
raise error "invalid denominator_units #{denominator_units.inspect}" unless denominator_units.is_a? Array
unless denominator_units.is_a? Array
raise Sass::ScriptError, "invalid denominator_units #{denominator_units.inspect}"
end
else
raise error "invalid unit #{unit.inspect}"
raise Sass::ScriptError, "invalid unit #{unit.inspect}"
end

unless denominator_units.empty? && numerator_units.empty?
Expand Down Expand Up @@ -105,7 +109,7 @@ def unitless?
# @return [Number]
# @raise [ScriptError]
def assert_unitless(name = nil)
raise error "Expected #{self} to have no units", name unless unitless?
raise Sass::ScriptError.new("Expected #{self} to have no units", name) unless unitless?

self
end
Expand All @@ -125,7 +129,7 @@ def unit?(unit)
# @return [Number]
# @raise [ScriptError]
def assert_unit(unit, name = nil)
raise error "Expected #{self} to have unit #{unit.inspect}", name unless unit?(unit)
raise Sass::ScriptError.new("Expected #{self} to have unit #{unit.inspect}", name) unless unit?(unit)

self
end
Expand All @@ -138,7 +142,7 @@ def integer?
# @return [Integer]
# @raise [ScriptError]
def assert_integer(name = nil)
raise error "#{self} is not an integer", name unless integer?
raise Sass::ScriptError.new("#{self} is not an integer", name) unless integer?

to_i
end
Expand Down Expand Up @@ -271,7 +275,8 @@ def coerce_or_convert_value(new_numerator_units, new_denominator_units,
other: nil,
other_name: nil)
if other && (other.numerator_units != new_denominator_units && other.denominator_units != new_denominator_units)
raise error "Expect #{other} to have units #{unit_string(new_numerator_units, new_denominator_units).inspect}"
raise Sass::ScriptError, "Expect #{other} to have units #{unit_string(new_numerator_units,
new_denominator_units).inspect}"
end

return value if numerator_units == new_numerator_units && denominator_units == new_denominator_units
Expand All @@ -288,19 +293,21 @@ def coerce_or_convert_value(new_numerator_units, new_denominator_units,
message << " $#{other_name}:" unless other_name.nil?
message << " #{other} have incompatible units"
message << " (one has units and the other doesn't)" if unitless? || other_unitless
return error message, name
return Sass::ScriptError.new(message, name)
end

return error "Expected #{self} to have no units", name unless other_unitless
return Sass::ScriptError.new("Expected #{self} to have no units", name) unless other_unitless

if new_numerator_units.length == 1 && new_denominator_units.empty?
type = Unit::TYPES_BY_UNIT[new_numerator_units.first]
return error "Expected #{self} to have a #{type} unit (#{Unit::UNITS_BY_TYPE[type].join(', ')})", name
return Sass::ScriptError.new(
"Expected #{self} to have a #{type} unit (#{Unit::UNITS_BY_TYPE[type].join(', ')})", name
)
end

unit_length = new_numerator_units.length + new_denominator_units.length
units = unit_string(new_numerator_units, new_denominator_units)
error "Expected #{self} to have unit#{unit_length > 1 ? 's' : ''} #{units}", name
Sass::ScriptError.new("Expected #{self} to have unit#{unit_length > 1 ? 's' : ''} #{units}", name)
}

result = value
Expand Down
4 changes: 2 additions & 2 deletions lib/sass/value/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ def assert_string(_name = nil)
# @return [Integer]
def sass_index_to_string_index(sass_index, name = nil)
index = sass_index.assert_number(name).assert_integer(name)
raise error('String index may not be 0', name) if index.zero?
raise Sass::ScriptError.new('String index may not be 0', name) if index.zero?

if index.abs > text.length
raise error("Invalid index #{sass_index} for a string with #{text.length} characters", name)
raise Sass::ScriptError.new("Invalid index #{sass_index} for a string with #{text.length} characters", name)
end

index.negative? ? text.length + index : index - 1
Expand Down

0 comments on commit c9d4671

Please sign in to comment.