Class: Synvert::Core::Rewriter::Instance

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/synvert/core/rewriter/instance.rb

Overview

Instance is an execution unit, it finds specified ast nodes, checks if the nodes match some conditions, then add, replace or remove code.

One instance can contains one or many [Synvert::Core::Rewriter::Scope] and [Synvert::Rewriter::Condition].

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

#add_arguments_with_parenthesis_if_necessary, #add_receiver_if_necessary, #strip_brackets

Constructor Details

#initialize(rewriter, file_pattern, &block) ⇒ Synvert::Core::Rewriter::Instance

Initialize an instance.

Parameters:

  • rewriter (Synvert::Core::Rewriter)
  • file_pattern (String)

    pattern to find files, e.g. spec/*/_spec.rb

  • block (Block)

    block code to find nodes, match conditions and rewrite code.



73
74
75
76
77
78
79
# File 'lib/synvert/core/rewriter/instance.rb', line 73

def initialize(rewriter, file_pattern, &block)
  @rewriter = rewriter
  @actions = []
  @file_pattern = file_pattern
  @block = block
  rewriter.helpers.each { |helper| self.singleton_class.send(:define_method, helper[:name], &helper[:block]) }
end

Instance Attribute Details

#current_fileObject

Returns current filename.

Returns:

  • current filename



65
# File 'lib/synvert/core/rewriter/instance.rb', line 65

attr_accessor :current_node, :current_file

#current_nodeObject

Returns current parsing node.

Returns:

  • current parsing node



65
66
67
# File 'lib/synvert/core/rewriter/instance.rb', line 65

def current_node
  @current_node
end

Class Method Details

.file_ast(file_path) ⇒ String

Cached file ast.

Parameters:

  • file_path (String)

    file path

Returns:

  • (String)

    ast node for file



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/synvert/core/rewriter/instance.rb', line 30

def file_ast(file_path)
  @file_ast ||= {}
  @file_ast[file_path] ||=
    begin
      buffer = Parser::Source::Buffer.new file_path
      buffer.source = file_source(file_path)

      parser = Parser::CurrentRuby.new
      parser.reset
      parser.parse buffer
    end
end

.file_source(file_path) ⇒ String

Cached file source.

Parameters:

  • file_path (String)

    file path

Returns:

  • (String)

    file source



16
17
18
19
20
21
22
23
24
# File 'lib/synvert/core/rewriter/instance.rb', line 16

def file_source(file_path)
  @file_source ||= {}
  @file_source[file_path] ||=
    begin
      source = File.read(file_path)
      source = Engine::ERB.encode(source) if file_path =~ /\.erb$/
      source
    end
end

.resetObject

Reset cached file source and ast.



55
56
57
58
# File 'lib/synvert/core/rewriter/instance.rb', line 55

def reset
  @file_source = {}
  @file_ast = {}
end

.write_file(file_path, source) ⇒ Object

Write source to file and remove cached file source and ast.

Parameters:

  • file_path (String)

    file path

  • source (String)

    file source



47
48
49
50
51
52
# File 'lib/synvert/core/rewriter/instance.rb', line 47

def write_file(file_path, source)
  source = Engine::ERB.decode(source) if file_path =~ /\.erb/
  File.write file_path, source.gsub(/ +\n/, "\n")
  @file_source[file_path] = nil
  @file_ast[file_path] = nil
end

Instance Method Details

#append(code) ⇒ Object

Parse append dsl, it creates a [Synvert::Core::Rewriter::AppendAction] to append the code to the bottom of current node body.

Parameters:

  • code (String)

    code need to be appended.



207
208
209
# File 'lib/synvert/core/rewriter/instance.rb', line 207

def append(code)
  @actions << Rewriter::AppendAction.new(self, code)
end

#goto_node(child_node_name, &block) ⇒ Object

Parse goto_node dsl, it creates a [Synvert::Core::Rewriter::GotoScope] to go to a child node, then continue operating on the child node.

Parameters:

  • child_node_name (String)

    the name of the child node.

  • block (Block)

    block code to continue operating on the matching nodes.



169
170
171
# File 'lib/synvert/core/rewriter/instance.rb', line 169

def goto_node(child_node_name, &block)
  Rewriter::GotoScope.new(self, child_node_name, &block).process
end

#if_exist_node(rules, &block) ⇒ Object

Parse if_exist_node dsl, it creates a [Synvert::Core::Rewriter::IfExistCondition] to check if matching nodes exist in the child nodes, if so, then continue operating on each matching ast node.

Parameters:

  • rules (Hash)

    rules to check mathing ast nodes.

  • block (Block)

    block code to continue operating on the matching nodes.



180
181
182
# File 'lib/synvert/core/rewriter/instance.rb', line 180

def if_exist_node(rules, &block)
  Rewriter::IfExistCondition.new(self, rules, &block).process
end

#if_only_exist_node(rules, &block) ⇒ Object

Parse if_only_exist_node dsl, it creates a [Synvert::Core::Rewriter::IfOnlyExistCondition] to check if current node has only one child node and the child node matches rules, if so, then continue operating on each matching ast node.

Parameters:

  • rules (Hash)

    rules to check mathing ast nodes.

  • block (Block)

    block code to continue operating on the matching nodes.



199
200
201
# File 'lib/synvert/core/rewriter/instance.rb', line 199

def if_only_exist_node(rules, &block)
  Rewriter::IfOnlyExistCondition.new(self, rules, &block).process
end

#insert(code) ⇒ Object

Parse insert dsl, it creates a [Synvert::Core::Rewriter::InsertAction] to insert the code to the top of current node body.

Parameters:

  • code (String)

    code need to be inserted.



215
216
217
# File 'lib/synvert/core/rewriter/instance.rb', line 215

def insert(code)
  @actions << Rewriter::InsertAction.new(self, code)
end

#insert_after(node) ⇒ Object

Parse insert_after dsl, it creates a [Synvert::Core::Rewriter::InsertAfterAction] to insert the code next to the current node.

Parameters:

  • code (String)

    code need to be inserted.



223
224
225
# File 'lib/synvert/core/rewriter/instance.rb', line 223

def insert_after(node)
  @actions << Rewriter::InsertAfterAction.new(self, node)
end

#nodeParser::AST::Node

Gets current node, it allows to get current node in block code.

Returns:



126
127
128
# File 'lib/synvert/core/rewriter/instance.rb', line 126

def node
  @current_node
end

#processObject

Process the instance. It finds all files, for each file, it executes the block code, gets all rewrite actions, and rewrite source code back to original file.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/synvert/core/rewriter/instance.rb', line 84

def process
  file_pattern = File.join(Configuration.instance.get(:path), @file_pattern)
  Dir.glob(file_pattern).each do |file_path|
    unless Configuration.instance.get(:skip_files).include? file_path
      begin
        conflict_actions = []
        source = self.class.file_source(file_path)
        ast = self.class.file_ast(file_path)

        @current_file = file_path

        self.process_with_node ast do
          begin
            instance_eval &@block
          rescue NoMethodError
            puts @current_node.debug_info
            raise
          end
        end

        if @actions.length > 0
          @actions.sort!
          conflict_actions = get_conflict_actions
          @actions.reverse.each do |action|
            source[action.begin_pos...action.end_pos] = action.rewritten_code
            source = remove_code_or_whole_line(source, action.line)
          end
          @actions = []

          self.class.write_file(file_path, source)
        end
      rescue Parser::SyntaxError
        puts "[Warn] file #{file_path} was not parsed correctly."
        # do nothing, iterate next file
      end while !conflict_actions.empty?
    end
  end
end

#process_with_node(node) { ... } ⇒ Object

Set current_node to node and process.

Parameters:

Yields:

  • process



134
135
136
137
138
# File 'lib/synvert/core/rewriter/instance.rb', line 134

def process_with_node(node)
  self.current_node = node
  yield
  self.current_node = node
end

#process_with_other_node(node) { ... } ⇒ Object

Set current_node properly, process and set current_node back to original current_node.

Parameters:

Yields:

  • process



144
145
146
147
148
149
# File 'lib/synvert/core/rewriter/instance.rb', line 144

def process_with_other_node(node)
  original_node = self.current_node
  self.current_node = node
  yield
  self.current_node = original_node
end

#removeObject

Parse remove dsl, it creates a [Synvert::Core::Rewriter::RemoveAction] to current node.



242
243
244
# File 'lib/synvert/core/rewriter/instance.rb', line 242

def remove
  @actions << Rewriter::RemoveAction.new(self)
end

#replace_erb_stmt_with_exprObject

Parse replace_erb_stmt_with_expr dsl, it creates a [Synvert::Core::Rewriter::ReplaceErbStmtWithExprAction] to replace erb stmt code to expr code.



237
238
239
# File 'lib/synvert/core/rewriter/instance.rb', line 237

def replace_erb_stmt_with_expr
  @actions << Rewriter::ReplaceErbStmtWithExprAction.new(self)
end

#replace_with(code) ⇒ Object

Parse replace_with dsl, it creates a [Synvert::Core::Rewriter::ReplaceWithAction] to replace current node with code.

Parameters:

  • code (String)

    code need to be replaced with.



231
232
233
# File 'lib/synvert/core/rewriter/instance.rb', line 231

def replace_with(code)
  @actions << Rewriter::ReplaceWithAction.new(self, code)
end

#unless_exist_node(rules, &block) ⇒ Object

Parse unless_exist_node dsl, it creates a [Synvert::Core::Rewriter::UnlessExistCondition] to check if matching nodes doesn’t exist in the child nodes, if so, then continue operating on each matching ast node.

Parameters:

  • rules (Hash)

    rules to check mathing ast nodes.

  • block (Block)

    block code to continue operating on the matching nodes.



189
190
191
# File 'lib/synvert/core/rewriter/instance.rb', line 189

def unless_exist_node(rules, &block)
  Rewriter::UnlessExistCondition.new(self, rules, &block).process
end

#warn(message) ⇒ Object

Parse warn dsl, it creates a [Synvert::Core::Rewriter::Warning] to save warning message.

Parameters:

  • message (String)

    warning message.



249
250
251
# File 'lib/synvert/core/rewriter/instance.rb', line 249

def warn(message)
  @rewriter.add_warning Rewriter::Warning.new(self, message)
end

#within_node(rules, &block) ⇒ Object Also known as: with_node

Parse within_node dsl, it creates a [Synvert::Core::Rewriter::WithinScope] to find matching ast nodes, then continue operating on each matching ast node.

Parameters:

  • rules (Hash)

    rules to find mathing ast nodes.

  • block (Block)

    block code to continue operating on the matching nodes.



160
161
162
# File 'lib/synvert/core/rewriter/instance.rb', line 160

def within_node(rules, &block)
  Rewriter::WithinScope.new(self, rules, &block).process
end