Class: Documented::Rewriter
- Inherits:
-
Object
- Object
- Documented::Rewriter
- Defined in:
- lib/rewriter.rb
Instance Attribute Summary collapse
-
#file_path ⇒ Object
Returns the value of attribute file_path.
-
#source ⇒ Object
Returns the value of attribute source.
-
#tree ⇒ Object
Returns the value of attribute tree.
Instance Method Summary collapse
- #delete ⇒ Object
- #execute ⇒ Object
-
#initialize(file_path) ⇒ Rewriter
constructor
A new instance of Rewriter.
- #replace_comments ⇒ Object
- #require_library(lib_path) ⇒ Object
- #write ⇒ Object
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_path ⇒ Object
Returns the value of attribute file_path.
6 7 8 |
# File 'lib/rewriter.rb', line 6 def file_path @file_path end |
#source ⇒ Object
Returns the value of attribute source.
7 8 9 |
# File 'lib/rewriter.rb', line 7 def source @source end |
#tree ⇒ Object
Returns the value of attribute tree.
8 9 10 |
# File 'lib/rewriter.rb', line 8 def tree @tree end |
Instance Method Details
#delete ⇒ Object
44 45 46 |
# File 'lib/rewriter.rb', line 44 def delete File.delete(temp_file_path) end |
#execute ⇒ Object
36 37 38 |
# File 'lib/rewriter.rb', line 36 def execute system("#{command(file_path)} #{temp_file_path}") end |
#replace_comments ⇒ Object
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" |
#write ⇒ Object
40 41 42 |
# File 'lib/rewriter.rb', line 40 def write File.write(temp_file_path, @tree.process) end |