Class: SCSSLint::Linter

Inherits:
Sass::Tree::Visitors::Base
  • Object
show all
Includes:
SelectorVisitor, Utils
Defined in:
lib/scss_lint/linter.rb

Defined Under Namespace

Classes: BorderZero, CapitalizationInSelector, ColorKeyword, Comment, DebugStatement, DeclarationOrder, DeclaredName, DuplicateProperty, EmptyRule, HexFormat, IdWithExtraneousSelector, Indentation, LeadingZero, PlaceholderInExtend, Shorthand, SingleLinePerSelector, SortedProperties, SpaceAfterComma, SpaceAfterPropertyColon, SpaceAfterPropertyName, SpaceBeforeBrace, TrailingSemicolonAfterPropertyValue, UsageName, ZeroUnit

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#can_be_condensed?, #extract_string_selectors, #node_has_bad_name?, #pluralize, #previous_node, #remove_quoted_strings, #shortest_hex_form

Methods included from SelectorVisitor

#visit_selector

Constructor Details

#initializeLinter

Returns a new instance of Linter.



8
9
10
# File 'lib/scss_lint/linter.rb', line 8

def initialize
  @lints = []
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



6
7
8
# File 'lib/scss_lint/linter.rb', line 6

def engine
  @engine
end

#lintsObject (readonly)

Returns the value of attribute lints.



6
7
8
# File 'lib/scss_lint/linter.rb', line 6

def lints
  @lints
end

Class Method Details

.node_name(node) ⇒ Object

Monkey-patched implementation that adds support for traversing Sass::Script::Nodes (original implementation only supports Sass::Tree::Nodes).



45
46
47
48
49
50
51
52
# File 'lib/scss_lint/linter.rb', line 45

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

Instance Method Details

#add_lint(node_or_line, message = nil) ⇒ Object

Helper for creating lint from a parse tree node



23
24
25
26
27
28
29
# File 'lib/scss_lint/linter.rb', line 23

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

#character_at(source_position, offset = 0) ⇒ Object

Returns the character at the given [Sass::Source::Position]



32
33
34
35
36
37
38
39
40
# File 'lib/scss_lint/linter.rb', line 32

def character_at(source_position, offset = 0)
  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

#descriptionObject

Define if you want a default message for your linter



18
19
20
# File 'lib/scss_lint/linter.rb', line 18

def description
  nil
end

#run(engine) ⇒ Object



12
13
14
15
# File 'lib/scss_lint/linter.rb', line 12

def run(engine)
  @engine = engine
  visit(engine.tree)
end

#visit(node) ⇒ Object

Modified so we can also visit selectors in linters



55
56
57
58
59
60
61
62
# File 'lib/scss_lint/linter.rb', line 55

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

#visit_children(parent) ⇒ Object

Redefine so we can set the ‘node_parent` of each node



65
66
67
68
69
70
# File 'lib/scss_lint/linter.rb', line 65

def visit_children(parent)
  parent.children.each do |child|
    child.node_parent = parent
    visit(child)
  end
end