class SCSSLint::Linter::SpaceBeforeBrace

Checks for the presence of a single space before an opening brace.

Public Instance Methods

check_node(node) { || ... } click to toggle source
# File lib/scss_lint/linter/space_before_brace.rb, line 6
def check_node(node)
  source = source_from_range(node.source_range).strip

  # Only lint `@include`s which have curly braces
  if source[-1] == '{'
    check_for_space(node, source)
  end

  yield
end
visit_each(node)
Alias for: check_node
visit_for(node)
Alias for: check_node
visit_function(node)
Alias for: check_node
visit_mixin(node)
Alias for: check_node
visit_mixindef(node)
Alias for: check_node
visit_rule(node)
Alias for: check_node
visit_while(node)
Alias for: check_node

Private Instance Methods

chars_before_incorrect(string) click to toggle source

Check if the characters before the end of the string are not what they should be

# File lib/scss_lint/linter/space_before_brace.rb, line 45
def chars_before_incorrect(string)
  if config['style'] != 'new_line'
    return !single_space_before(string)
  end
  !newline_before_nonwhitespace(string)
end
check_for_space(node, string) click to toggle source
# File lib/scss_lint/linter/space_before_brace.rb, line 28
def check_for_space(node, string)
  line = node.source_range.end_pos.line

  if config['allow_single_line_padding'] && node_on_single_line?(node)
    return unless string[-2] != ' '
    add_lint(line, 'Opening curly brace in a single line rule set '\
                   '`{` should be preceded by at least one space')
  else
    return unless chars_before_incorrect(string)
    style_message = (config['style'] == 'new_line') ? 'a new line' : 'one space'
    add_lint(line, 'Opening curly brace `{` should be ' \
                   "preceded by #{style_message}")
  end
end
newline_before_nonwhitespace(string) click to toggle source

Check if, starting from the end of a string and moving backwards, towards the beginning, we find a new line before any non-whitespace characters

# File lib/scss_lint/linter/space_before_brace.rb, line 63
def newline_before_nonwhitespace(string)
  offset = -2
  while /\S/.match(string[offset]).nil?
    return true if string[offset] == "\n"
    offset -= 1
  end
  false
end
single_space_before(string) click to toggle source

Check if there is one space and only one space before the end of the string

# File lib/scss_lint/linter/space_before_brace.rb, line 54
def single_space_before(string)
  return false if string[-2] != ' '
  return false if string[-3] == ' '
  true
end