lib/scss_lint/linter.rb in scss-lint-0.22.0 vs lib/scss_lint/linter.rb in scss-lint-0.23.0

- old
+ new

@@ -21,17 +21,38 @@ # Helper for creating lint from a parse tree node # # @param node_or_line [Sass::Script::Tree::Node, Sass::Engine::Line] # @param message [String] def add_lint(node_or_line, message) - line = node_or_line.respond_to?(:line) ? node_or_line.line : node_or_line + location = if node_or_line.respond_to?(:source_range) && node_or_line.source_range + location_from_range(node_or_line.source_range) + elsif node_or_line.respond_to?(:line) + Location.new(node_or_line.line) + else + Location.new(node_or_line) + end @lints << Lint.new(engine.filename, - line, + location, message) end + # Extract {SCSSLint::Location} from a {Sass::Source::Range}. + # + # @param range [Sass::Source::Range] + # @return [SCSSLint::Location] + def location_from_range(range) + length = if range.start_pos.line == range.end_pos.line + range.end_pos.offset - range.start_pos.offset + else + line_source = engine.lines[range.start_pos.line] + line_source.length - range.start_pos.offset + 1 + end + + Location.new(range.start_pos.line, range.start_pos.offset, length) + end + # @param source_position [Sass::Source::Position] # @param offset [Integer] # @return [String] the character at the given [Sass::Source::Position] def character_at(source_position, offset = 0) actual_line = source_position.line - 1 @@ -70,10 +91,10 @@ # Returns whether a given node spans only a single line. # # @param node [Sass::Tree::Node] # @return [true,false] whether the node spans a single line - def node_on_single_line(node) + def node_on_single_line?(node) return if node.source_range.start_pos.line != node.source_range.end_pos.line # The Sass parser reports an incorrect source range if the trailing curly # brace is on the next line, e.g. #