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

  • :write_to_stdout (Boolean)

    true if source changes should be written to stdout

Raises:

  • (Haml::Parser::Error)

    if there was a problem parsing the document



44
45
46
47
48
49
50
# File 'lib/haml_lint/document.rb', line 44

def initialize(source, options)
  @config = options[:config]
  @file = options.fetch(:file, STRING_SOURCE)
  @write_to_stdout = options[:write_to_stdout]
  @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



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

def indentation
  @indentation
end

#sourceString (readonly)

Returns original source code.

Returns:

  • (String)

    original source code



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

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



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

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)



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

def source_was_changed
  @source_was_changed
end

#treeHamlLint::Tree::Node (readonly)

Returns Root of the parse tree.

Returns:



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

def tree
  @tree
end

#unescape_interpolation_to_original_cacheObject (readonly)

Returns the value of attribute unescape_interpolation_to_original_cache.



35
36
37
# File 'lib/haml_lint/document.rb', line 35

def unescape_interpolation_to_original_cache
  @unescape_interpolation_to_original_cache
end

#write_to_stdoutBoolean (readonly)

Returns true if source changes (from autocorrect) should be written to stdout instead of disk.

Returns:

  • (Boolean)

    true if source changes (from autocorrect) should be written to stdout instead of disk



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

def write_to_stdout
  @write_to_stdout
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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/haml_lint/document.rb', line 68

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



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

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

#write_to_disk!Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/haml_lint/document.rb', line 85

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