Sha256: 3ede8e1786e7d5392a1f2a14d87fa000c2a5a9e2587f2a9cb3b1191fdc251ba5

Contents?: true

Size: 1.04 KB

Versions: 8

Compression:

Stored size: 1.04 KB

Contents

class SCSSBeautifier::Formatters::StringQuotes < SCSSBeautifier::Formatters::Base
  def visit_prop(node)
    check_quotes(node)
    visit_children(node)
  end
  def check_quotes(node)
    return unless node.value.is_a?(Sass::Script::Tree::Literal)

    # TODO Check for single or double quote preference
    str_without_quotes = extract_string_without_quotes(node.value.value.to_s)
    change_to_single_quotes(node) if str_without_quotes && !str_without_quotes.match(/'/)
  end

  private

  STRING_WITHOUT_QUOTES_REGEX = %r{
    \A
    ["'](.*)["']    # Extract text between quotes
    \s*\)?\s*;?\s*  # Sometimes the Sass parser includes a trailing ) or ;
    (//.*)?         # Exclude any trailing comments that might have snuck in
    \z
  }x

  def change_to_single_quotes(node)
    new_val = node.value.value.to_s.gsub('"', '\'')
    node.instance_variable_set(:@value, Sass::Script::Value::String.new(new_val))
  end

  def extract_string_without_quotes(source)
    return unless match = STRING_WITHOUT_QUOTES_REGEX.match(source)
    match[1]
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
scss_beautifier-0.1.19 lib/scss_beautifier/formatters/string_quotes.rb
scss_beautifier-0.1.18 lib/scss_beautifier/formatters/string_quotes.rb
scss_beautifier-0.1.17 lib/scss_beautifier/formatters/string_quotes.rb
scss_beautifier-0.1.16 lib/scss_beautifier/formatters/string_quotes.rb
scss_beautifier-0.1.15 lib/scss_beautifier/formatters/string_quotes.rb
scss_beautifier-0.1.14 lib/scss_beautifier/formatters/string_quotes.rb
scss_beautifier-0.1.13 lib/scss_beautifier/formatters/string_quotes.rb
scss_beautifier-0.1.12 lib/scss_beautifier/formatters/string_quotes.rb