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, DuplicateProperty, EmptyLineBetweenBlocks, EmptyRule, HexFormat, IdWithExtraneousSelector, Indentation, LeadingZero, NameFormat, PlaceholderInExtend, PropertySortOrder, PropertySpelling, SelectorDepth, Shorthand, SingleLinePerSelector, SpaceAfterComma, SpaceAfterPropertyColon, SpaceAfterPropertyName, SpaceBeforeBrace, SpaceBetweenParens, StringQuotes, TrailingSemicolonAfterPropertyValue, UrlQuotes, ZeroUnit

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#can_be_condensed?, #extract_string_selectors, #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

Instance Method Details

#add_lint(node_or_line, message = nil) ⇒ Object

Helper for creating lint from a parse tree node

Parameters:



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

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) ⇒ String

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

Parameters:

  • source_position (Sass::Source::Position)
  • offset (Integer) (defaults to: 0)

Returns:

  • (String)

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



42
43
44
45
46
47
48
49
50
# File 'lib/scss_lint/linter.rb', line 42

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

#descriptionString?

Define if you want a default message for your linter

Returns:

  • (String, nil)


23
24
25
# File 'lib/scss_lint/linter.rb', line 23

def description
  nil
end

#run(engine, config) ⇒ Object

Parameters:



15
16
17
18
19
# File 'lib/scss_lint/linter.rb', line 15

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

#source_from_range(source_range) ⇒ String

Extracts the original source code given a range.

Parameters:

  • source_range (Sass::Source::Range)

Returns:

  • (String)

    the original source code



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/scss_lint/linter.rb', line 56

def source_from_range(source_range)
  current_line = source_range.start_pos.line - 1
  last_line    = source_range.end_pos.line - 1
  start_pos    = source_range.start_pos.offset - 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
  end

  if source_range.start_pos.line != source_range.end_pos.line &&
     # Sometimes the parser reports ranges ending on the first column of the
     # line after the last line; don't include the last line in this case.
     engine.lines.count == current_line - 1
    source += "#{engine.lines[current_line][0...source_range.end_pos.offset]}\n"
  end

  source
end

#visit(node) ⇒ Object

Modified so we can also visit selectors in linters

Parameters:



87
88
89
90
91
92
93
94
# File 'lib/scss_lint/linter.rb', line 87

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

Parameters:



100
101
102
103
104
105
# File 'lib/scss_lint/linter.rb', line 100

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