Class: HamlLint::Document

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

Overview

Represents a parsed Haml document and its associated metadata.

Constant Summary collapse

STRING_SOURCE =

File name given to source code parsed from just a string.

'(string)'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, options) ⇒ Document

Parses the specified Haml code into a HamlLint::Document.

Parameters:

  • source (String)

    Haml code to parse

  • options (Hash)

Options Hash (options):

  • :file (String)

    file name of document that was parsed

Raises:

  • (Haml::Parser::Error)

    if there was a problem parsing the document



40
41
42
43
44
45
# File 'lib/haml_lint/document.rb', line 40

def initialize(source, options)
  @config = options[:config]
  @file = options.fetch(:file, STRING_SOURCE)
  @source_was_changed = false
  process_source(source)
end

Instance Attribute Details

#configHamlLint::Configuration (readonly)

Returns Configuration used to parse template.

Returns:



12
13
14
# File 'lib/haml_lint/document.rb', line 12

def config
  @config
end

#fileString (readonly)

Returns Haml template file path.

Returns:

  • (String)

    Haml template file path



15
16
17
# File 'lib/haml_lint/document.rb', line 15

def file
  @file
end

#indentationString (readonly)

Returns the indentation used in the file.

Returns:

  • (String)

    the indentation used in the file



30
31
32
# File 'lib/haml_lint/document.rb', line 30

def indentation
  @indentation
end

#sourceString (readonly)

Returns original source code.

Returns:

  • (String)

    original source code



21
22
23
# File 'lib/haml_lint/document.rb', line 21

def source
  @source
end

#source_linesArray<String> (readonly)

Returns original source code as an array of lines.

Returns:

  • (Array<String>)

    original source code as an array of lines



24
25
26
# File 'lib/haml_lint/document.rb', line 24

def source_lines
  @source_lines
end

#source_was_changedBoolean (readonly)

Returns true if the source was changed (by autocorrect).

Returns:

  • (Boolean)

    true if the source was changed (by autocorrect)



27
28
29
# File 'lib/haml_lint/document.rb', line 27

def source_was_changed
  @source_was_changed
end

#treeHamlLint::Tree::Node (readonly)

Returns Root of the parse tree.

Returns:



18
19
20
# File 'lib/haml_lint/document.rb', line 18

def tree
  @tree
end

#unescape_interpolation_to_original_cacheObject (readonly)

Returns the value of attribute unescape_interpolation_to_original_cache.



32
33
34
# File 'lib/haml_lint/document.rb', line 32

def unescape_interpolation_to_original_cache
  @unescape_interpolation_to_original_cache
end

Instance Method Details

#change_source(new_source) ⇒ Object

Reparses the new source and remember that the document was changed Used when auto-correct does changes to the file. If the source hasn’t changed, then the document will not be marked as changed.

If the new_source fails to parse, automatically reparses the previous source to bring the document back to how it should be before re-raising the parse exception

Parameters:

  • source (String)

    Haml code to parse



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/haml_lint/document.rb', line 63

def change_source(new_source)
  return if new_source == @source
  check_new_source_compatible(new_source)

  old_source = @source
  begin
    process_source(new_source)
    @source_was_changed = true
  rescue HamlLint::Exceptions::ParseError
    # Reprocess the previous_source so that other linters can work on this document
    # object from a clean slate
    process_source(old_source)
    raise
  end
  nil
end

#last_non_empty_lineInteger

Returns the last non empty line of the document or 1 if all lines are empty

Returns:

  • (Integer)

    last non empty line of the document or 1 if all lines are empty



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

def last_non_empty_line
  index = source_lines.rindex { |l| !l.empty? }
  (index || 0) + 1
end

#write_to_disk!Object



80
81
82
83
84
85
86
87
# File 'lib/haml_lint/document.rb', line 80

def write_to_disk!
  return unless @source_was_changed
  if file == STRING_SOURCE
    raise HamlLint::Exceptions::InvalidFilePath, 'Cannot write without :file option'
  end
  File.write(file, unstrip_frontmatter(source))
  @source_was_changed = false
end