Class: Transpec::Rewriter
- Inherits:
-
Object
- Object
- Transpec::Rewriter
- Defined in:
- lib/transpec/rewriter.rb
Instance Method Summary collapse
- #create_source_buffer(source, name) ⇒ Object
- #dispatch_node(node, ancestor_nodes, in_example_group_context) ⇒ Object
-
#initialize(configuration = Configuration.new) ⇒ Rewriter
constructor
A new instance of Rewriter.
- #need_to_modify_expectation_syntax_configuration?(rspec_configure) ⇒ Boolean
- #need_to_modify_mock_syntax_configuration?(rspec_configure) ⇒ Boolean
- #parse(source_buffer) ⇒ Object
- #process_be_close(be_close) ⇒ Object
- #process_double(double) ⇒ Object
- #process_method_stub(method_stub) ⇒ Object
- #process_rspec_configure(rspec_configure) ⇒ Object
- #process_should(should) ⇒ Object
- #process_should_receive(should_receive) ⇒ Object
- #rewrite(source, name = '(string)') ⇒ Object
- #rewrite_file!(file_path) ⇒ Object
- #warn_not_in_example_group_context_error(error) ⇒ Object
Constructor Details
#initialize(configuration = Configuration.new) ⇒ Rewriter
Returns a new instance of Rewriter.
17 18 19 |
# File 'lib/transpec/rewriter.rb', line 17 def initialize(configuration = Configuration.new) @configuration = configuration end |
Instance Method Details
#create_source_buffer(source, name) ⇒ Object
49 50 51 52 53 |
# File 'lib/transpec/rewriter.rb', line 49 def create_source_buffer(source, name) source_buffer = Parser::Source::Buffer.new(name) source_buffer.source = source source_buffer end |
#dispatch_node(node, ancestor_nodes, in_example_group_context) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/transpec/rewriter.rb', line 61 def dispatch_node(node, ancestor_nodes, in_example_group_context) Syntax.all.each do |syntax_class| next unless syntax_class.target_node?(node) syntax = syntax_class.new( node, ancestor_nodes, in_example_group_context, @source_rewriter ) handler_name = "process_#{syntax_class.snake_case_name}" send(handler_name, syntax) break end rescue Syntax::NotInExampleGroupContextError => error warn_not_in_example_group_context_error(error) end |
#need_to_modify_expectation_syntax_configuration?(rspec_configure) ⇒ Boolean
140 141 142 143 |
# File 'lib/transpec/rewriter.rb', line 140 def need_to_modify_expectation_syntax_configuration?(rspec_configure) return false unless @configuration.convert_to_expect_to_matcher? rspec_configure.expectation_syntaxes == [:should] end |
#need_to_modify_mock_syntax_configuration?(rspec_configure) ⇒ Boolean
145 146 147 148 149 |
# File 'lib/transpec/rewriter.rb', line 145 def need_to_modify_mock_syntax_configuration?(rspec_configure) return false if !@configuration.convert_to_expect_to_receive? && !@configuration.convert_to_allow_to_receive? rspec_configure.mock_syntaxes == [:should] end |
#parse(source_buffer) ⇒ Object
55 56 57 58 59 |
# File 'lib/transpec/rewriter.rb', line 55 def parse(source_buffer) parser = Parser::CurrentRuby.new ast = parser.parse(source_buffer) ast end |
#process_be_close(be_close) ⇒ Object
126 127 128 |
# File 'lib/transpec/rewriter.rb', line 126 def process_be_close(be_close) be_close.convert_to_be_within! if @configuration.replace_deprecated_method? end |
#process_double(double) ⇒ Object
112 113 114 |
# File 'lib/transpec/rewriter.rb', line 112 def process_double(double) double.convert_to_double! if @configuration.replace_deprecated_method? end |
#process_method_stub(method_stub) ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/transpec/rewriter.rb', line 116 def process_method_stub(method_stub) if @configuration.convert_to_allow_to_receive? method_stub.allowize! elsif @configuration.replace_deprecated_method? method_stub.replace_deprecated_method! end method_stub.remove_any_number_of_times! if @configuration.replace_deprecated_method? end |
#process_rspec_configure(rspec_configure) ⇒ Object
130 131 132 133 134 135 136 137 138 |
# File 'lib/transpec/rewriter.rb', line 130 def process_rspec_configure(rspec_configure) if need_to_modify_expectation_syntax_configuration?(rspec_configure) rspec_configure.modify_expectation_syntaxes!(:expect) end if need_to_modify_mock_syntax_configuration?(rspec_configure) rspec_configure.modify_mock_syntaxes!(:expect) end end |
#process_should(should) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/transpec/rewriter.rb', line 91 def process_should(should) if @configuration.convert_to_expect_to_matcher? should.expectize!( @configuration.negative_form_of_to, @configuration.parenthesize_matcher_arg? ) end end |
#process_should_receive(should_receive) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/transpec/rewriter.rb', line 100 def process_should_receive(should_receive) if @configuration.convert_to_expect_to_receive? if should_receive.any_number_of_times? && @configuration.replace_deprecated_method? should_receive.allowize_any_number_of_times!(@configuration.negative_form_of_to) else should_receive.expectize!(@configuration.negative_form_of_to) end elsif should_receive.any_number_of_times? && @configuration.replace_deprecated_method? should_receive.stubize_any_number_of_times! end end |
#rewrite(source, name = '(string)') ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/transpec/rewriter.rb', line 27 def rewrite(source, name = '(string)') source_buffer = create_source_buffer(source, name) ast = parse(source_buffer) @source_rewriter = Parser::Source::Rewriter.new(source_buffer) failed_overlapping_rewrite = false @source_rewriter.diagnostics.consumer = proc { failed_overlapping_rewrite = true } AST::Scanner.scan(ast) do |node, ancestor_nodes, in_example_group_context| dispatch_node(node, ancestor_nodes, in_example_group_context) end rewritten_source = @source_rewriter.process if failed_overlapping_rewrite rewriter = self.class.new(@configuration) rewritten_source = rewriter.rewrite(rewritten_source, name) end rewritten_source end |
#rewrite_file!(file_path) ⇒ Object
21 22 23 24 25 |
# File 'lib/transpec/rewriter.rb', line 21 def rewrite_file!(file_path) source = File.read(file_path) rewritten_source = rewrite(source, file_path) File.write(file_path, rewritten_source) end |
#warn_not_in_example_group_context_error(error) ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/transpec/rewriter.rb', line 81 def warn_not_in_example_group_context_error(error) warn error. warn format( '%s:%d:%s', error.source_buffer.name, error.source_range.line, error.source_range.source_line ) end |