Class: SCSSLint::Linter
- Inherits:
-
Sass::Tree::Visitors::Base
- Object
- Sass::Tree::Visitors::Base
- SCSSLint::Linter
- Includes:
- SelectorVisitor, Utils
- Defined in:
- lib/scss_lint/linter.rb
Overview
Defines common functionality available to all linters.
Direct Known Subclasses
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
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
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
-
#lints ⇒ Object
readonly
Returns the value of attribute lints.
Instance Method Summary collapse
-
#add_lint(node_or_line, message = nil) ⇒ Object
Helper for creating lint from a parse tree node.
-
#character_at(source_position, offset = 0) ⇒ String
The character at the given [Sass::Source::Position].
-
#description ⇒ String?
Define if you want a default message for your linter.
-
#initialize ⇒ Linter
constructor
A new instance of Linter.
- #run(engine, config) ⇒ Object
-
#source_from_range(source_range) ⇒ String
Extracts the original source code given a range.
-
#visit(node) ⇒ Object
Modified so we can also visit selectors in linters.
-
#visit_children(parent) ⇒ Object
Redefine so we can set the ‘node_parent` of each node.
Methods included from Utils
#can_be_condensed?, #extract_string_selectors, #pluralize, #previous_node, #remove_quoted_strings, #shortest_hex_form
Methods included from SelectorVisitor
Constructor Details
#initialize ⇒ Linter
Returns a new instance of Linter.
9 10 11 |
# File 'lib/scss_lint/linter.rb', line 9 def initialize @lints = [] end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/scss_lint/linter.rb', line 7 def config @config end |
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
7 8 9 |
# File 'lib/scss_lint/linter.rb', line 7 def engine @engine end |
#lints ⇒ Object (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
31 32 33 34 35 36 37 |
# File 'lib/scss_lint/linter.rb', line 31 def add_lint(node_or_line, = nil) line = node_or_line.respond_to?(:line) ? node_or_line.line : node_or_line @lints << Lint.new(engine.filename, line, || description) end |
#character_at(source_position, offset = 0) ⇒ String
Returns 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 |
#description ⇒ String?
Define if you want a default message for your linter
23 24 25 |
# File 'lib/scss_lint/linter.rb', line 23 def description nil end |
#run(engine, config) ⇒ Object
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.
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
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
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 |