lib/scss_lint/linter.rb in scss-lint-0.18.0 vs lib/scss_lint/linter.rb in scss-lint-0.19.0

- old
+ new

@@ -8,47 +8,63 @@ def initialize @lints = [] end + # @param engine [Engine] + # @param config [Config] def run(engine, config) @config = config @engine = engine visit(engine.tree) end # Define if you want a default message for your linter + # @return [String, nil] def description nil end # Helper for creating lint from a parse tree node + # + # @param node_or_line [Sass::Script::Tree::Node, Sass::Engine::Line] + # @param message [String, nil] def add_lint(node_or_line, message = nil) line = node_or_line.respond_to?(:line) ? node_or_line.line : node_or_line @lints << Lint.new(engine.filename, line, message || description) end - # Returns the character at the given [Sass::Source::Position] + # @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 + actual_line = source_position.line - 1 actual_offset = source_position.offset + offset - 1 # Return a newline if offset points at the very end of the line return "\n" if actual_offset == engine.lines[actual_line].length engine.lines[actual_line][actual_offset] end # Extracts the original source code given a range. + # + # @param source_range [Sass::Source::Range] + # @return [String] the original source code def source_from_range(source_range) current_line = source_range.start_pos.line - 1 - last_line = source_range.end_pos.line - 1 + last_line = source_range.end_pos.line - 1 + start_pos = source_range.start_pos.offset - 1 - source = engine.lines[current_line][(source_range.start_pos.offset - 1)..-1] + if current_line == last_line + source = engine.lines[current_line][start_pos..(source_range.end_pos.offset - 1)] + else + source = engine.lines[current_line][start_pos..-1] + end current_line += 1 while current_line < last_line source += "#{engine.lines[current_line]}\n" current_line += 1 @@ -62,32 +78,26 @@ end source end - # Monkey-patched implementation that adds support for traversing - # Sass::Script::Nodes (original implementation only supports - # Sass::Tree::Nodes). - def self.node_name(node) - case node - when Sass::Script::Tree::Node, Sass::Script::Value::Base - "script_#{node.class.name.gsub(/.*::(.*?)$/, '\\1').downcase}" - else - super - end - end - # Modified so we can also visit selectors in linters + # + # @param node [Sass::Tree::Node, Sass::Script::Tree::Node, + # Sass::Script::Value::Base] def visit(node) # Visit the selector of a rule if parsed rules are available if node.is_a?(Sass::Tree::RuleNode) && node.parsed_rules visit_selector(node.parsed_rules) end super end # Redefine so we can set the `node_parent` of each node + # + # @param parent [Sass::Tree::Node, Sass::Script::Tree::Node, + # Sass::Script::Value::Base] def visit_children(parent) parent.children.each do |child| child.node_parent = parent visit(child) end