class SCSSBeautifier::Formatters::Shorthand

Constants

LIST_LITERAL_REGEX
SHORTHANDABLE_PROPERTIES

Public Instance Methods

update_script_list(pName, node) click to toggle source
# File lib/scss_beautifier/formatters/shorthand.rb, line 23
def update_script_list(pName, node)
  nValue = node.value
  check_shorthand(pName, node, nValue.children.map(&:to_sass))
end
update_script_literal(pName, node) click to toggle source
# File lib/scss_beautifier/formatters/shorthand.rb, line 14
def update_script_literal(pName, node)
  nValue = node.value

  return unless values = nValue.value.to_s.strip[LIST_LITERAL_REGEX, 1]
  values = values.split(' ')

  check_shorthand(pName, node, values)
end
visit_prop(node) click to toggle source
# File lib/scss_beautifier/formatters/shorthand.rb, line 2
def visit_prop(node)
  property_name = node.name.join
  return unless SHORTHANDABLE_PROPERTIES.include?(property_name)

  case node.value
  when Sass::Script::Tree::Literal
    update_script_literal(property_name, node)
  when Sass::Script::Tree::ListLiteral
    update_script_list(property_name, node)
  end
end

Private Instance Methods

allowed?(size) click to toggle source
# File lib/scss_beautifier/formatters/shorthand.rb, line 54
def allowed?(size)
  # return false unless config['allowed_shorthands']
  # config['allowed_shorthands'].map(&:to_i).include?(size)
  [1, 2, 3, 4].map(&:to_i).include?(size)
end
check_shorthand(prop, node, values) click to toggle source
# File lib/scss_beautifier/formatters/shorthand.rb, line 46
def check_shorthand(prop, node, values)
  shortest_form = condensed_shorthand(*values)

  return if values == shortest_form

  node.instance_variable_set(:@value, Sass::Script::Value::String.new(shortest_form.join(' ')))
end
condense_to_one_value?(top, right, bottom, left) click to toggle source

@param top [String] @param right [String] @param bottom [String] @param left [String] @return [Boolean]

# File lib/scss_beautifier/formatters/shorthand.rb, line 82
def condense_to_one_value?(top, right, bottom, left)
  return unless allowed?(1)
  return unless top == right

  top == bottom && (bottom == left || left.nil?) ||
    bottom.nil? && left.nil?
end
condense_to_three_values?(_, right, __, left) click to toggle source

@param right [String] @param left [String] @return [Boolean]

# File lib/scss_beautifier/formatters/shorthand.rb, line 105
def condense_to_three_values?(_, right, __, left)
  return unless allowed?(3)

  right == left
end
condense_to_two_values?(top, right, bottom, left) click to toggle source

@param top [String] @param right [String] @param bottom [String] @param left [String] @return [Boolean]

# File lib/scss_beautifier/formatters/shorthand.rb, line 95
def condense_to_two_values?(top, right, bottom, left)
  return unless allowed?(2)

  top == bottom && right == left ||
    top == bottom && left.nil? && top != right
end
condensed_shorthand(top, right, bottom = nil, left = nil) click to toggle source

@param top [String] @param right [String] @param bottom [String] @param left [String] @return [Array]

# File lib/scss_beautifier/formatters/shorthand.rb, line 65
def condensed_shorthand(top, right, bottom = nil, left = nil)
  if condense_to_one_value?(top, right, bottom, left)
    [top]
  elsif condense_to_two_values?(top, right, bottom, left)
    [top, right]
  elsif condense_to_three_values?(top, right, bottom, left)
    [top, right, bottom]
  else
    [top, right, bottom, left].compact
  end
end