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

Parameters:

  • source (String)

    the source code to analyze

  • line (Integer)

    the line number the source starts at

  • mode (String)

    the type of directive, one of “disable” or “enable”

  • linters (Array<String>)

    the name of the linters to act upon



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.

Returns:

  • (String)


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

Returns:

  • (String)


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.

Parameters:

  • source (String)

    the source code to analyze

  • line (Integer)

    the line number the source starts at

Returns:



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.

Parameters:

Returns:

  • (true, false)


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.

Returns:

  • (true, false)


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.

Returns:

  • (true, false)


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.

Returns:

  • (String)


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

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