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

#initialize, #name, #run

Methods included from HamlVisitor

#visit, #visit_children

Constructor Details

This class inherits a constructor from HamlLint::Linter

Instance Method Details

#visit_tag(node) ⇒ Object



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

def visit_tag(node)
  return unless node.hash_attributes?

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

  record_lint(node, style[:start_message]) unless source =~ style[:start_regex]
  record_lint(node, style[:end_message]) unless source =~ style[:end_regex]
end