Class: Documented::Rewriter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ Rewriter

Returns a new instance of Rewriter.



10
11
12
13
14
# File 'lib/rewriter.rb', line 10

def initialize(file_path)
  @file_path = file_path
  @source = RuboCop::ProcessedSource.new(File.read(file_path), 3)
  @tree = Parser::Source::TreeRewriter.new(@source.buffer)
end

Instance Attribute Details

#file_pathObject

Returns the value of attribute file_path.



6
7
8
# File 'lib/rewriter.rb', line 6

def file_path
  @file_path
end

#sourceObject

Returns the value of attribute source.



7
8
9
# File 'lib/rewriter.rb', line 7

def source
  @source
end

#treeObject

Returns the value of attribute tree.



8
9
10
# File 'lib/rewriter.rb', line 8

def tree
  @tree
end

Instance Method Details

#deleteObject



44
45
46
# File 'lib/rewriter.rb', line 44

def delete
  File.delete(temp_file_path)
end

#executeObject



36
37
38
# File 'lib/rewriter.rb', line 36

def execute
  system("#{command(file_path)} #{temp_file_path}")
end

#replace_commentsObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rewriter.rb', line 24

def replace_comments
  @source.comments.each do |comment|
    range = comment.location.expression
  
    if comment.text.start_with?('# documented:enable')
      @tree.replace(range, "Documented.enable")
    elsif comment.text.start_with?('# documented:disable')
      @tree.replace(range, "Documented.disable")
    end
  end
end

#require_library(lib_path) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/rewriter.rb', line 16

def require_library(lib_path)
  setup = "    \\n require \"\#{lib_path}\"\n    Documented.setup(\"\#{@file_path}\")\n  RUBY\n  @tree.insert_after(@source.ast.children.first.loc.expression, setup)\nend\n"

#writeObject



40
41
42
# File 'lib/rewriter.rb', line 40

def write
  File.write(temp_file_path, @tree.process)
end