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+)/.freeze
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.freeze

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



41
42
43
44
45
46
# File 'lib/haml_lint/directive.rb', line 41

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)


51
52
53
# File 'lib/haml_lint/directive.rb', line 51

def linters
  @linters
end

#modeString (readonly)

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

Returns:

  • (String)


56
57
58
# File 'lib/haml_lint/directive.rb', line 56

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:



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

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)


63
64
65
66
67
# File 'lib/haml_lint/directive.rb', line 63

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)


72
73
74
# File 'lib/haml_lint/directive.rb', line 72

def disable?
  mode == 'disable'
end

#enable?true, false

Checks whether this is an enable directive.

Returns:

  • (true, false)


79
80
81
# File 'lib/haml_lint/directive.rb', line 79

def enable?
  mode == 'enable'
end

#inspectString

Formats the directive for display in a console.

Returns:

  • (String)


86
87
88
# File 'lib/haml_lint/directive.rb', line 86

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