Class: SCSSLint::Linter

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

Overview

Defines common functionality available to all linters.

Defined Under Namespace

Classes: BorderZero, CapitalizationInSelector, ColorKeyword, Comment, Compass, DebugStatement, DeclarationOrder, DeclaredName, DuplicateProperty, EmptyLineBetweenBlocks, EmptyRule, HexFormat, IdWithExtraneousSelector, Indentation, LeadingZero, PlaceholderInExtend, PropertySpelling, SelectorDepth, Shorthand, SingleLinePerSelector, SortedProperties, SpaceAfterComma, SpaceAfterPropertyColon, SpaceAfterPropertyName, SpaceBeforeBrace, SpaceBetweenParens, 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.



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

def initialize
  @lints = []
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#engineObject (readonly)

Returns the value of attribute engine.



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

def engine
  @engine
end

#lintsObject (readonly)

Returns the value of attribute lints.



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

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).



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

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



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

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]



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

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



20
21
22
# File 'lib/scss_lint/linter.rb', line 20

def description
  nil
end

#run(engine, config) ⇒ Object



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

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

#visit(node) ⇒ Object

Modified so we can also visit selectors in linters



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

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



67
68
69
70
71
72
# File 'lib/scss_lint/linter.rb', line 67

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