Class: HamlLint::Linter::SpaceInsideHashAttributes

Inherits:
HamlLint::Linter show all
Includes:
HamlLint::LinterRegistry
Defined in:
lib/haml_lint/linter/space_inside_hash_attributes.rb

Overview

Checks for spaces inside the braces of hash attributes (e.g. ‘%tag{ lang: en }` vs `%tagen`).

Constant Summary collapse

STYLE =
{
  'no_space' => {
    start_regex: /\A\{[^ ]/,
    end_regex: /(?:^\s*\}|[^ ]\})\z/,
    start_message: 'Hash attribute should start with no space after the opening brace',
    end_message: 'Hash attribute should end with no space before the closing brace or be on its own line'
  },
  'space' => {
    start_regex: /\A\{(?: [^ ]|$)/,
    end_regex: /(?:^\s*\}|[^ ] \})\z/,
    start_message: 'Hash attribute should start with one space after the opening brace',
    end_message: 'Hash attribute should end with one space before the closing brace or be on its own line'
  }
}.freeze

Instance Attribute Summary

Attributes inherited from HamlLint::Linter

#lints

Instance Method Summary collapse

Methods included from HamlLint::LinterRegistry

extract_linters_from, included

Methods inherited from HamlLint::Linter

autocorrect_priority, autocorrect_safe?, #initialize, #name, ruby_parser, #run, #run_or_raise, supports_autocorrect?, #supports_autocorrect?

Methods included from HamlVisitor

#visit, #visit_children

Constructor Details

This class inherits a constructor from HamlLint::Linter

Instance Method Details

#visit_tag(node) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/haml_lint/linter/space_inside_hash_attributes.rb', line 26

def visit_tag(node)
  return unless node.hash_attributes?

  style_name = config['style'] == 'no_space' ? 'no_space' : 'space'
  style = STYLE[style_name]
  source = node.hash_attributes_source

  start_ok = source.match?(style[:start_regex])
  end_ok = source.match?(style[:end_regex])
  return if start_ok && end_ok

  corrected = correct_hash_spacing(node, source, style_name)
  record_lint(node, style[:start_message], corrected: corrected) unless start_ok
  record_lint(node, style[:end_message], corrected: corrected) unless end_ok
end