Module: RspecSprint::Fixers::LetItBe::Rewriter
- Defined in:
- lib/rspec_sprint/fixers/let_it_be/rewriter.rb
Class Method Summary collapse
-
.rewrite(source, candidates) ⇒ Object
source(String)と candidates(Array)を受け取り、 各候補行の let(/let!( を let_it_be( に書き換えた source を返す。 ファイル I/O を持たない純粋関数。パース不能時は source をそのまま返す。.
Class Method Details
.rewrite(source, candidates) ⇒ Object
source(String)と candidates(Array)を受け取り、各候補行の let(/let!( を let_it_be( に書き換えた source を返す。ファイル I/O を持たない純粋関数。パース不能時は source をそのまま返す。
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rspec_sprint/fixers/let_it_be/rewriter.rb', line 15 def rewrite(source, candidates) return source if candidates.empty? processed = ::RuboCop::AST::ProcessedSource.new(source.to_s, RUBY_VERSION.to_f) return source if processed.ast.nil? target_lines = candidates.map(&:line).to_set tree_rewriter = ::Parser::Source::TreeRewriter.new(processed.buffer) processed.ast.each_node(:block) do |node| next unless target_lines.include?(node.loc.line) send_node = node.children[0] next unless send_node&.send_type? next unless %i[let let!].include?(send_node.method_name) tree_rewriter.replace(send_node.loc.selector, "let_it_be") end tree_rewriter.process end |