Class: Gauge::CodeParser Private
- Inherits:
-
Object
- Object
- Gauge::CodeParser
- Defined in:
- lib/code_parser.rb
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Class Method Summary collapse
- .code_to_ast(code) ⇒ Object private
- .include_args(code_string, params) ⇒ Object private
- .refactor(code, param_positions, new_param_values, new_step_text) ⇒ Object private
- .refactor_args(code, param_positions, new_param_values, new_step_text) ⇒ Object private
- .step_args_from_code(code) ⇒ Object private
Class Method Details
.code_to_ast(code) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
72 73 74 75 76 77 78 |
# File 'lib/code_parser.rb', line 72 def self.code_to_ast(code) buffer = Parser::Source::Buffer.new '(string)' buffer.source = code parser = Parser::CurrentRuby.new parser.parse(buffer) end |
.include_args(code_string, params) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
80 81 82 |
# File 'lib/code_parser.rb', line 80 def self.include_args(code_string, params) code_string.gsub("do", "do #{params}") end |
.refactor(code, param_positions, new_param_values, new_step_text) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/code_parser.rb', line 59 def self.refactor(code, param_positions, new_param_values, new_step_text) source_code=code.source ast = code_to_ast source_code file, line = code.source_location refactored_code=refactor_args(source_code, param_positions, new_param_values, new_step_text) tmp_file = Tempfile.new File.basename(file, ".rb") tmp_file.write(File.open(file, "r") { |f| f.read.gsub(source_code, refactored_code)}) tmp_file.close FileUtils.mv tmp_file.path, file end |
.refactor_args(code, param_positions, new_param_values, new_step_text) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/code_parser.rb', line 32 def self.refactor_args(code, param_positions, new_param_values, new_step_text) new_params = [] args = step_args_from_code code param_positions.sort_by!(&:newPosition).each { |e| if e.oldPosition == -1 new_params[e.newPosition] = new_param_values[e.newPosition] else new_params[e.newPosition] = args[e.oldPosition].children[0] end } buffer = Parser::Source::Buffer.new '(rewriter)' buffer.source=code ast = code_to_ast(code) new_params_string = "|#{new_params.join(', ')}|".gsub("||", "") # no params = empty string rewriter = Parser::Source::Rewriter.new(buffer) .replace(ast.children[0].location.expression, "step '#{new_step_text}'") # hack, could not find an easy way to manipulate the ast to include arguments, when none existed originally. # it's just easy to add arguments via string substitution. return include_args(rewriter.process, new_params_string) if ast.children[1].location.expression.nil? #insert new arguments rewriter.replace(ast.children[1].location.expression, new_params_string).process end |
.step_args_from_code(code) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
26 27 28 29 30 |
# File 'lib/code_parser.rb', line 26 def self.step_args_from_code(code) ast=code_to_ast(code) arg_node = ast.children[1] arg_node.children end |