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_raise_error(raise_error) ⇒ 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.
19 20 21 |
# File 'lib/transpec/rewriter.rb', line 19 def initialize(configuration = Configuration.new) @configuration = configuration end |
Instance Method Details
#create_source_buffer(source, name) ⇒ Object
51 52 53 54 55 |
# File 'lib/transpec/rewriter.rb', line 51 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
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/transpec/rewriter.rb', line 64 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
153 154 155 156 |
# File 'lib/transpec/rewriter.rb', line 153 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
158 159 160 161 162 |
# File 'lib/transpec/rewriter.rb', line 158 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
57 58 59 60 61 62 |
# File 'lib/transpec/rewriter.rb', line 57 def parse(source_buffer) builder = AST::Builder.new parser = Parser::CurrentRuby.new(builder) ast = parser.parse(source_buffer) ast end |
#process_be_close(be_close) ⇒ Object
133 134 135 |
# File 'lib/transpec/rewriter.rb', line 133 def process_be_close(be_close) be_close.convert_to_be_within! if @configuration.replace_deprecated_method? end |
#process_double(double) ⇒ Object
119 120 121 |
# File 'lib/transpec/rewriter.rb', line 119 def process_double(double) double.convert_to_double! if @configuration.replace_deprecated_method? end |
#process_method_stub(method_stub) ⇒ Object
123 124 125 126 127 128 129 130 131 |
# File 'lib/transpec/rewriter.rb', line 123 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. if @configuration.replace_deprecated_method? end |
#process_raise_error(raise_error) ⇒ Object
137 138 139 140 141 |
# File 'lib/transpec/rewriter.rb', line 137 def process_raise_error(raise_error) if @configuration.replace_deprecated_method? raise_error.remove_error_specification_with_negative_expectation! end end |
#process_rspec_configure(rspec_configure) ⇒ Object
143 144 145 146 147 148 149 150 151 |
# File 'lib/transpec/rewriter.rb', line 143 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
94 95 96 97 98 99 100 101 |
# File 'lib/transpec/rewriter.rb', line 94 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
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/transpec/rewriter.rb', line 103 def process_should_receive(should_receive) if should_receive.useless_expectation? if @configuration.replace_deprecated_method? if @configuration.convert_to_allow_to_receive? should_receive.allowize_useless_expectation!(@configuration.negative_form_of_to) else should_receive.stubize_useless_expectation! end elsif @configuration.convert_to_expect_to_receive? should_receive.expectize!(@configuration.negative_form_of_to) end elsif @configuration.convert_to_expect_to_receive? should_receive.expectize!(@configuration.negative_form_of_to) end end |
#rewrite(source, name = '(string)') ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/transpec/rewriter.rb', line 29 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
23 24 25 26 27 |
# File 'lib/transpec/rewriter.rb', line 23 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
84 85 86 87 88 89 90 91 92 |
# File 'lib/transpec/rewriter.rb', line 84 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 |