Class: Transpec::Rewriter
- Inherits:
-
Object
- Object
- Transpec::Rewriter
show all
- Defined in:
- lib/transpec/rewriter.rb
Defined Under Namespace
Classes: OverlappedRewriteError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(configuration = Configuration.new, report = Report.new) ⇒ Rewriter
Returns a new instance of Rewriter.
22
23
24
25
26
|
# File 'lib/transpec/rewriter.rb', line 22
def initialize(configuration = Configuration.new, report = Report.new)
@configuration = configuration
@report = report
@invalid_context_errors = []
end
|
Instance Attribute Details
#invalid_context_errors ⇒ Object
Returns the value of attribute invalid_context_errors.
20
21
22
|
# File 'lib/transpec/rewriter.rb', line 20
def invalid_context_errors
@invalid_context_errors
end
|
#report ⇒ Object
Returns the value of attribute report.
20
21
22
|
# File 'lib/transpec/rewriter.rb', line 20
def report
@report
end
|
Instance Method Details
#create_source_buffer(source, name) ⇒ Object
59
60
61
62
63
|
# File 'lib/transpec/rewriter.rb', line 59
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) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/transpec/rewriter.rb', line 72
def dispatch_node(node, ancestor_nodes)
Syntax.all.each do |syntax_class|
next unless syntax_class.target_node?(node)
syntax = syntax_class.new(
node,
ancestor_nodes,
@source_rewriter,
@report
)
handler_name = "process_#{syntax_class.snake_case_name}"
send(handler_name, syntax)
break
end
rescue OverlappedRewriteError
rescue Syntax::InvalidContextError => error
@invalid_context_errors << error
end
|
#need_to_modify_expectation_syntax_configuration?(rspec_configure) ⇒ Boolean
152
153
154
155
156
157
|
# File 'lib/transpec/rewriter.rb', line 152
def need_to_modify_expectation_syntax_configuration?(rspec_configure)
return false unless @configuration.convert_to_expect_to_matcher?
rspec_configure.expectation_syntaxes == [:should]
rescue Syntax::RSpecConfigure::UnknownSyntaxError
false
end
|
#need_to_modify_mock_syntax_configuration?(rspec_configure) ⇒ Boolean
159
160
161
162
163
164
165
|
# File 'lib/transpec/rewriter.rb', line 159
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]
rescue Syntax::RSpecConfigure::UnknownSyntaxError
false
end
|
#parse(source_buffer) ⇒ Object
65
66
67
68
69
70
|
# File 'lib/transpec/rewriter.rb', line 65
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
132
133
134
|
# File 'lib/transpec/rewriter.rb', line 132
def process_be_close(be_close)
be_close.convert_to_be_within! if @configuration.replace_deprecated_method?
end
|
#process_double(double) ⇒ Object
118
119
120
|
# File 'lib/transpec/rewriter.rb', line 118
def process_double(double)
double.convert_to_double! if @configuration.replace_deprecated_method?
end
|
#process_method_stub(method_stub) ⇒ Object
122
123
124
125
126
127
128
129
130
|
# File 'lib/transpec/rewriter.rb', line 122
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_allowance_for_no_message! if @configuration.replace_deprecated_method?
end
|
#process_raise_error(raise_error) ⇒ Object
136
137
138
139
140
|
# File 'lib/transpec/rewriter.rb', line 136
def process_raise_error(raise_error)
if @configuration.replace_deprecated_method?
raise_error.remove_error_specification_with_negative_expectation!
end
end
|
142
143
144
145
146
147
148
149
150
|
# File 'lib/transpec/rewriter.rb', line 142
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
93
94
95
96
97
98
99
100
|
# File 'lib/transpec/rewriter.rb', line 93
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/transpec/rewriter.rb', line 102
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
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/transpec/rewriter.rb', line 34
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 do
failed_overlapping_rewrite = true
fail OverlappedRewriteError
end
AST::Scanner.scan(ast) do |node, ancestor_nodes|
dispatch_node(node, ancestor_nodes)
end
rewritten_source = @source_rewriter.process
if failed_overlapping_rewrite
rewriter = self.class.new(@configuration, @report)
rewritten_source = rewriter.rewrite(rewritten_source, name)
end
rewritten_source
end
|
#rewrite_file!(file_path) ⇒ Object
28
29
30
31
32
|
# File 'lib/transpec/rewriter.rb', line 28
def rewrite_file!(file_path)
source = File.read(file_path)
rewritten_source = rewrite(source, file_path)
File.write(file_path, rewritten_source)
end
|