class SCSSLint::Linter::SpaceAroundOperator

Checks for space around operators on values.

Public Instance Methods

source_fm_range(range) click to toggle source

Making a public version of source_from_range, a private method.

# File lib/scss_lint/linter/space_around_operator.rb, line 27
def source_fm_range(range)
  source_from_range(range)
end
visit_script_operation(node) { || ... } click to toggle source
# File lib/scss_lint/linter/space_around_operator.rb, line 8
def visit_script_operation(node)
  operation_sources = OperationSources.new(node, self)
  operation_sources.adjust_sources

  # When an operation is found interpolated within something not a String
  # (only selectors?), the source ranges are offset by two (probably not
  # accounting for the `#{`. Slide everything to the left by 2, and maybe
  # things will look sane this time.
  unless operation_sources.operator_source.match?(Sass::Script::Lexer::REGULAR_EXPRESSIONS[:op])
    operation_sources.adjust_for_interpolation
    operation_sources.adjust_sources
  end

  check(node, operation_sources)

  yield
end

Private Instance Methods

check(node, operation_sources) click to toggle source
# File lib/scss_lint/linter/space_around_operator.rb, line 33
def check(node, operation_sources) # rubocop:disable Metrics/AbcSize, Metrics/LineLength, Metrics/MethodLength
  match = operation_sources.operator_source.match(/
    (?<left_space>\s*)
    (?<operator>\S+)
    (?<right_space>\s*)
  /x)

  # We forgive spacing with newlines. In the case of a newline occurring on
  # one side or another, we don't care about indentation, and the
  # TrailingWhitespace linter will worry about trailing whitespace, so we
  # just don't worry about space with a newline.
  left_newline = match[:left_space].include?("\n")
  right_newline = match[:right_space].include?("\n")

  case config['style']
  when 'one_space'
    if one_space_exists?(match, left_newline, right_newline)
      add_lint(node, operation_sources.space_msg(match[:operator]))
    end
  when 'at_least_one_space'
    unless spaces_exist?(match, left_newline, right_newline)
      add_lint(node, operation_sources.space_msg(match[:operator]))
    end
  else
    if spaces_exist?(match, left_newline, right_newline)
      add_lint(node, operation_sources.no_space_msg(match[:operator]))
    end
  end
end
one_space_exists?(match, left_newline, right_newline) click to toggle source
# File lib/scss_lint/linter/space_around_operator.rb, line 63
def one_space_exists?(match, left_newline, right_newline)
  (match[:left_space] != ' ' && !left_newline) ||
    (match[:right_space] != ' ' && !right_newline)
end
spaces_exist?(match, left_newline, right_newline) click to toggle source
# File lib/scss_lint/linter/space_around_operator.rb, line 68
def spaces_exist?(match, left_newline, right_newline)
  (match[:left_space] != '' && !left_newline) ||
    (match[:right_space] != '' && !right_newline)
end