Class: HamlLint::Directive

Inherits:
Object
  • Object
show all
Defined in:
lib/haml_lint/directive.rb

Overview

Handles linter configuration transformation via Haml comments.

Direct Known Subclasses

Null

Defined Under Namespace

Classes: Null

Constant Summary collapse

LINTER_REGEXP =
/(?:[A-Z]\w+)/
DIRECTIVE_REGEXP =
/
  # "haml-lint:" with optional spacing
  \s*haml-lint\s*:\s*

  # The mode - either disable or enable
  (?<mode>(?:dis|en)able)\b\s*

  # "all" or a comma-separated list (with optional spaces) of linters
  (?<linters>all | (?:#{LINTER_REGEXP}\s*,\s*)* #{LINTER_REGEXP})
/x

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, line, mode, linters) ⇒ Directive

Instantiates a new HamlLint::Directive



39
40
41
42
43
44
# File 'lib/haml_lint/directive.rb', line 39

def initialize(source, line, mode, linters)
  @source = source
  @line = line
  @mode = mode
  @linters = linters
end

Instance Attribute Details

#lintersString (readonly)

The names of the linters to act upon.



49
50
51
# File 'lib/haml_lint/directive.rb', line 49

def linters
  @linters
end

#modeString (readonly)

The mode of the directive. One of “disable” or “enable”.



54
55
56
# File 'lib/haml_lint/directive.rb', line 54

def mode
  @mode
end

Class Method Details

.from_line(source, line) ⇒ HamlLint::Directive

Constructs a directive from source code as a given line.



22
23
24
25
26
27
28
29
30
# File 'lib/haml_lint/directive.rb', line 22

def self.from_line(source, line)
  match = DIRECTIVE_REGEXP.match(source)

  if match
    new(source, line, match[:mode], match[:linters].split(/\s*,\s*/))
  else
    Null.new(source, line)
  end
end

Instance Method Details

#==(other) ⇒ true, false

Checks whether a directive is equivalent to another.



61
62
63
64
65
# File 'lib/haml_lint/directive.rb', line 61

def ==(other)
  super unless other.is_a?(HamlLint::Directive)

  mode == other.mode && linters == other.linters
end

#disable?true, false

Checks whether this is a disable directive.



70
71
72
# File 'lib/haml_lint/directive.rb', line 70

def disable?
  mode == 'disable'
end

#enable?true, false

Checks whether this is an enable directive.



77
78
79
# File 'lib/haml_lint/directive.rb', line 77

def enable?
  mode == 'enable'
end

#inspectString

Formats the directive for display in a console.



84
85
86
# File 'lib/haml_lint/directive.rb', line 84

def inspect
  "#<HamlLint::Directive(mode=#{mode}, linters=#{linters})>"
end