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
60
61
62
63
64
|
# File 'lib/transpec/rewriter.rb', line 60
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/transpec/rewriter.rb', line 73
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
153
154
155
156
157
158
|
# 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]
rescue Syntax::RSpecConfigure::UnknownSyntaxError
false
end
|
#need_to_modify_mock_syntax_configuration?(rspec_configure) ⇒ Boolean
160
161
162
163
164
165
166
|
# File 'lib/transpec/rewriter.rb', line 160
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
66
67
68
69
70
71
|
# File 'lib/transpec/rewriter.rb', line 66
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.remove_allowance_for_no_message! 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
|
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/transpec/rewriter.rb', line 35
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
33
|
# File 'lib/transpec/rewriter.rb', line 28
def rewrite_file!(file_path)
source = File.read(file_path)
rewritten_source = rewrite(source, file_path)
return if source == rewritten_source
File.write(file_path, rewritten_source)
end
|