class SCSSLint::Linter::SpaceAroundOperator::OperationSources

A helper class for storing and adjusting the sources of the different components of an Operation node.

Constants

NO_SPACE_MSG
SPACE_MSG

Attributes

operator_source[R]

Public Class Methods

new(node, linter) click to toggle source
# File lib/scss_lint/linter/space_around_operator.rb, line 78
def initialize(node, linter)
  @node = node
  @linter = linter
  @source = normalize_source(@linter.source_fm_range(@node.source_range))
  @left_range = @node.operand1.source_range
  @right_range = @node.operand2.source_range
end

Public Instance Methods

adjust_for_interpolation() click to toggle source
# File lib/scss_lint/linter/space_around_operator.rb, line 96
def adjust_for_interpolation
  @source = normalize_source(@linter.source_fm_range(slide_to_the_left(@node.source_range)))
  @left_range = slide_to_the_left(@node.operand1.source_range)
  @right_range = slide_to_the_left(@node.operand2.source_range)
end
adjust_sources() click to toggle source
# File lib/scss_lint/linter/space_around_operator.rb, line 86
def adjust_sources
  # We need to #chop at the end because an operation's operand1 _always_
  # includes one character past the actual operand (which is either a
  # whitespace character, or the first character of the operation).
  @left_source = normalize_source(@linter.source_fm_range(@left_range))
  @right_source = normalize_source(@linter.source_fm_range(@right_range))
  @operator_source = calculate_operator_source
  adjust_left_boundary
end
no_space_msg(operator) click to toggle source
# File lib/scss_lint/linter/space_around_operator.rb, line 106
def no_space_msg(operator)
  NO_SPACE_MSG % [@source, @left_source, operator, @right_source]
end
space_msg(operator) click to toggle source
# File lib/scss_lint/linter/space_around_operator.rb, line 102
def space_msg(operator)
  SPACE_MSG % [@source, @left_source, operator, @right_source]
end

Private Instance Methods

adjust_left_boundary() click to toggle source
# File lib/scss_lint/linter/space_around_operator.rb, line 136
def adjust_left_boundary
  # If the left operand is wrapped in parentheses, any right parens end up
  # in the operator source. Here, we move them into the left operand
  # source, which is awkward in any messaging, but it works.
  if match = @operator_source.match(/^(\s*\))+/)
    @left_source += match[0]
    @operator_source = @operator_source[match.end(0)..-1]
  end

  # If the left operand is a nested operation, Sass includes any whitespace
  # before the (outer) operator in the left operator's source_range's
  # end_pos, which is not the case with simple, non-operation operands.
  if match = @left_source.match(/\s+$/)
    @left_source = @left_source[0..match.begin(0)]
    @operator_source = match[0] + @operator_source
  end

  [@left_source, @operator_source]
end
calculate_operator_source() click to toggle source
# File lib/scss_lint/linter/space_around_operator.rb, line 118
def calculate_operator_source
  # We don't want to add 1 to range1.end_pos.offset for the same reason as
  # the #chop comment above.
  between_start = Sass::Source::Position.new(
    @left_range.end_pos.line,
    @left_range.end_pos.offset,
  )
  between_end = Sass::Source::Position.new(
    @right_range.start_pos.line,
    @right_range.start_pos.offset - 1,
  )

  @linter.source_fm_range(Sass::Source::Range.new(between_start,
                                                  between_end,
                                                  @left_range.file,
                                                  @left_range.importer))
end
normalize_source(source) click to toggle source

Removes trailing parentheses and compacts newlines into a single space

# File lib/scss_lint/linter/space_around_operator.rb, line 157
def normalize_source(source)
  source.chop.gsub(/\s*\n\s*/, ' ')
end
slide_to_the_left(range) click to toggle source
# File lib/scss_lint/linter/space_around_operator.rb, line 161
def slide_to_the_left(range)
  start_pos = Sass::Source::Position.new(range.start_pos.line, range.start_pos.offset - 2)
  end_pos = Sass::Source::Position.new(range.end_pos.line, range.end_pos.offset - 2)
  Sass::Source::Range.new(start_pos, end_pos, range.file, range.importer)
end