Class: Synvert::Core::Rewriter

Inherits:
Object
  • Object
show all
Defined in:
lib/synvert/core/rewriter.rb

Overview

Rewriter is the top level namespace in a snippet.

One Rewriter checks if the depndency version matches, and it can contain one or many Instance, which define the behavior what files and what codes to detect and rewrite to what code.

Defined Under Namespace

Modules: Helper Classes: Action, AnyValue, AppendAction, Condition, DeleteAction, GemSpec, GotoScope, IfExistCondition, IfOnlyExistCondition, InsertAction, InsertAfterAction, Instance, PrependAction, QueryScope, RemoveAction, ReplaceAction, ReplaceErbStmtWithExprAction, ReplaceWithAction, RubyVersion, Scope, UnlessExistCondition, Warning, WithinScope, WrapAction

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group, name) { ... } ⇒ Rewriter

Initialize a Rewriter. When a rewriter is initialized, it is already registered.

Parameters:

  • group (String)

    group of the rewriter.

  • name (String)

    name of the rewriter.

Yields:

  • defines the behaviors of the rewriter, block code won't be called when initialization.


141
142
143
144
145
146
147
148
149
150
151
# File 'lib/synvert/core/rewriter.rb', line 141

def initialize(group, name, &block)
  @group = group
  @name = name
  @block = block
  @helpers = []
  @sub_snippets = []
  @warnings = []
  @affected_files = Set.new
  @redo_until_no_change = false
  self.class.register(@group, @name, self)
end

Instance Attribute Details

#affected_filesSet (readonly)

Returns affected fileds.

Returns:

  • (Set)

    affected fileds


133
# File 'lib/synvert/core/rewriter.rb', line 133

attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec

#gem_specObject (readonly)

Returns the value of attribute gem_spec.


133
# File 'lib/synvert/core/rewriter.rb', line 133

attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec

#groupString (readonly)

Returns the group of rewriter.

Returns:

  • (String)

    the group of rewriter


133
134
135
# File 'lib/synvert/core/rewriter.rb', line 133

def group
  @group
end

#helperArray (readonly)

Returns helper methods.

Returns:

  • (Array)

    helper methods.


133
# File 'lib/synvert/core/rewriter.rb', line 133

attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec

#helpersObject (readonly)

Returns the value of attribute helpers.


133
134
135
# File 'lib/synvert/core/rewriter.rb', line 133

def helpers
  @helpers
end

#nameString (readonly)

Returns the unique name of rewriter.

Returns:

  • (String)

    the unique name of rewriter


133
# File 'lib/synvert/core/rewriter.rb', line 133

attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec

#ruby_versionRewriter::RubyVersion (readonly)

Returns the ruby version.

Returns:


133
# File 'lib/synvert/core/rewriter.rb', line 133

attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec

#sub_snippetsArray<Synvert::Core::Rewriter> (readonly)

Returns all rewriters this rewiter calls.

Returns:


133
# File 'lib/synvert/core/rewriter.rb', line 133

attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec

#warningsArray<Synvert::Core::Rewriter::Warning> (readonly)

Returns warning messages.

Returns:


133
# File 'lib/synvert/core/rewriter.rb', line 133

attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec

Class Method Details

.availablesHash<String, Hash<String, Rewriter>>

Get all available rewriters

Returns:

  • (Hash<String, Hash<String, Rewriter>>)

101
102
103
# File 'lib/synvert/core/rewriter.rb', line 101

def availables
  rewriters
end

.call(group, name, sandbox = false) ⇒ Synvert::Core::Rewriter

Get a registered rewriter by group and name, then process that rewriter.

Parameters:

  • group (String)

    the rewriter group.

  • name (String)

    the rewriter name.

  • sandbox (Boolean) (defaults to: false)

    if run in sandbox mode.

Returns:

Raises:


88
89
90
91
92
93
94
95
96
# File 'lib/synvert/core/rewriter.rb', line 88

def call(group, name, sandbox = false)
  rewriter = fetch(group, name)
  if sandbox
    rewriter.process_with_sandbox
  else
    rewriter.process
  end
  rewriter
end

.clearObject

Clear all registered rewriters.


106
107
108
# File 'lib/synvert/core/rewriter.rb', line 106

def clear
  rewriters.clear
end

.execute { ... } ⇒ Object

Execute the temporary rewriter without group and name.

Yields:

  • defines the behaviors of the rewriter.


48
49
50
51
52
# File 'lib/synvert/core/rewriter.rb', line 48

def execute(&block)
  rewriter = Rewriter.new('', '', &block)
  rewriter.process
  rewriter
end

.fetch(group, name) ⇒ Synvert::Core::Rewriter

Fetch a rewriter by group and name.

Parameters:

  • group (String)

    rewrtier group.

  • name (String)

    rewrtier name.

Returns:

Raises:


72
73
74
75
76
77
78
79
# File 'lib/synvert/core/rewriter.rb', line 72

def fetch(group, name)
  group = group.to_s
  name = name.to_s
  rewriter = rewriters.dig(group, name)
  raise RewriterNotFound, "Rewriter #{group} #{name} not found" unless rewriter

  rewriter
end

.register(group, name, rewriter) ⇒ Object

Register a rewriter with its group and name.

Parameters:

  • group (String)

    the rewriter group.

  • name (String)

    the unique rewriter name.

  • rewriter (Synvert::Core::Rewriter)

    the rewriter to register.


59
60
61
62
63
64
# File 'lib/synvert/core/rewriter.rb', line 59

def register(group, name, rewriter)
  group = group.to_s
  name = name.to_s
  rewriters[group] ||= {}
  rewriters[group][name] = rewriter
end

Instance Method Details

#add_affected_file(file_path) ⇒ Object

Add an affected file.

Parameters:

  • file_path (String)

183
184
185
# File 'lib/synvert/core/rewriter.rb', line 183

def add_affected_file(file_path)
  @affected_files.add(file_path)
end

#add_file(filename, content) ⇒ Object

Parses add_file dsl, it adds a new file.

Examples:

Synvert::Rewriter.new 'rails', 'add_application_record' do
  add_file 'app/models/application_record.rb', <<~EOS
    class ApplicationRecord < ActiveRecord::Base
      self.abstract_class = true
    end
  EOS
end

Parameters:

  • filename (String)

    file name of newly created file.

  • content (String)

    file body of newly created file.


260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/synvert/core/rewriter.rb', line 260

def add_file(filename, content)
  return if @sandbox

  filepath = File.join(Configuration.path, filename)
  if File.exist?(filepath)
    puts "File #{filepath} already exists."
    return
  end

  FileUtils.mkdir_p File.dirname(filepath)
  File.write(filepath, content)
end

#add_snippet(group, name) ⇒ Object

Parse add_snippet dsl, it calls anther rewriter.

Examples:

Synvert::Rewriter.new 'minitest', 'better_syntax' do
  add_snippet 'minitest', 'assert_empty'
  add_snippet 'minitest', 'assert_equal_arguments_order'
  add_snippet 'minitest', 'assert_includes'
  add_snippet 'minitest', 'assert_instance_of'
  add_snippet 'minitest', 'assert_kind_of'
  add_snippet 'minitest', 'assert_match'
  add_snippet 'minitest', 'assert_nil'
  add_snippet 'minitest', 'assert_operator'
  add_snippet 'minitest', 'assert_path_exists'
  add_snippet 'minitest', 'assert_predicate'
  add_snippet 'minitest', 'assert_respond_to'
  add_snippet 'minitest', 'assert_silent'
  add_snippet 'minitest', 'assert_truthy'
end

Parameters:

  • group (String)

    group of another rewriter.

  • name (String)

    name of another rewriter.


305
306
307
# File 'lib/synvert/core/rewriter.rb', line 305

def add_snippet(group, name)
  @sub_snippets << self.class.call(group.to_s, name.to_s, @sandbox)
end

#add_warning(warning) ⇒ Object

Add a warning.

Parameters:


176
177
178
# File 'lib/synvert/core/rewriter.rb', line 176

def add_warning(warning)
  @warnings << warning
end

#description(description = nil) ⇒ Object

Parse description dsl, it sets description of the rewrite. Or get description.

Examples:

Synvert::Rewriter.new 'rspec', 'use_new_syntax' do
  description 'It converts rspec code to new syntax, it calls all rspec sub snippets.'
end

Parameters:

  • description (String) (defaults to: nil)

    rewriter description.

Returns:

  • rewriter description.


199
200
201
202
203
204
205
# File 'lib/synvert/core/rewriter.rb', line 199

def description(description = nil)
  if description
    @description = description
  else
    @description
  end
end

#helper_method(name) { ... } ⇒ Object

Parse helper_method dsl, it defines helper method for Instance.

Examples:

Synvert::Rewriter.new 'rails', 'convert_active_record_dirty_5_0_to_5_1' do
  helper_method :find_callbacks_and_convert do |callback_names, callback_changes|
    # do anything, method find_callbacks_and_convert can be reused later.
  end
  within_files Synvert::RAILS_MODEL_FILES + Synvert::RAILS_OBSERVER_FILES do
    find_callbacks_and_convert(before_callback_names, before_callback_changes)
    find_callbacks_and_convert(after_callback_names, after_callback_changes)
  end
end

Parameters:

  • name (String)

    helper method name.

Yields:

  • helper method block.


322
323
324
# File 'lib/synvert/core/rewriter.rb', line 322

def helper_method(name, &block)
  @helpers << { name: name, block: block }
end

#if_gem(name, version) ⇒ Object

Parse if_gem dsl, it compares version of the specified gem.

Examples:

Synvert::Rewriter.new 'rails', 'upgrade_5_2_to_6_0' do
  if_gem 'rails', '>= 6.0'
end

Parameters:

  • name (String)

    gem name.

  • version (String)

    equal, less than or greater than specified version, e.g. '>= 2.0.0',


224
225
226
# File 'lib/synvert/core/rewriter.rb', line 224

def if_gem(name, version)
  @gem_spec = Rewriter::GemSpec.new(name, version)
end

#if_ruby(version) ⇒ Object

Parse if_ruby dsl, it checks if ruby version is greater than or equal to the specified ruby version.

Examples:

Synvert::Rewriter.new 'ruby', 'new_safe_navigation_operator' do
  if_ruby '2.3.0'
end

Parameters:

  • version (String)

    specified ruby version.


213
214
215
# File 'lib/synvert/core/rewriter.rb', line 213

def if_ruby(version)
  @ruby_version = Rewriter::RubyVersion.new(version)
end

#processObject

Process the rewriter. It will call the block.


155
156
157
158
159
160
# File 'lib/synvert/core/rewriter.rb', line 155

def process
  @affected_files = Set.new
  instance_eval(&@block)

  process if !@affected_files.empty? && @redo_until_no_change # redo
end

#process_with_sandboxObject

Process rewriter with sandbox mode. It will call the block but doesn't change any file.


164
165
166
167
168
169
170
171
# File 'lib/synvert/core/rewriter.rb', line 164

def process_with_sandbox
  @sandbox = true
  begin
    process
  ensure
    @sandbox = false
  end
end

#redo_until_no_changeObject

Rerun the snippet until no change.

Examples:

Synvert::Rewriter.new 'ruby', 'nested_class_definition' do
  redo_until_no_change
end

353
354
355
# File 'lib/synvert/core/rewriter.rb', line 353

def redo_until_no_change
  @redo_until_no_change = true
end

#remove_file(filename) ⇒ Object

Parses remove_file dsl, it removes a file.

Examples:

Synvert::Rewriter.new 'rails', 'upgrade_4_0_to_4_1' do
  remove_file 'config/initializers/secret_token.rb'
end

Parameters:

  • filename (String)

    file name.


279
280
281
282
283
284
# File 'lib/synvert/core/rewriter.rb', line 279

def remove_file(filename)
  return if @sandbox

  file_path = File.join(Configuration.path, filename)
  File.delete(file_path) if File.exist?(file_path)
end

#todo(todo = nil) ⇒ String

Parse todo dsl, it sets todo of the rewriter. Or get todo.

Examples:

Synvert::Rewriter.new 'rails', 'upgrade_3_2_to_4_0' do
  todo <<~EOS
    1. Rails 4.0 no longer supports loading plugins from vendor/plugins. You must replace any plugins by extracting them to gems and adding them to your Gemfile. If you choose not to make them gems, you can move them into, say, lib/my_plugin/* and add an appropriate initializer in config/initializers/my_plugin.rb.
    2.  Make the following changes to your Gemfile.
        gem 'sass-rails', '~> 4.0.0'
        gem 'coffee-rails', '~> 4.0.0'
        gem 'uglifier', '>= 1.3.0'
  EOS
end

Parameters:

  • todo (String) (defaults to: nil)

    rewriter todo.

Returns:

  • (String)

    rewriter todo.


340
341
342
343
344
345
346
# File 'lib/synvert/core/rewriter.rb', line 340

def todo(todo = nil)
  if todo
    @todo = todo
  else
    @todo
  end
end

#within_files(file_patterns, &block) ⇒ Object Also known as: within_file

Parse within_files dsl, it finds specified files. It creates a Instance to rewrite code.

Examples:

Synvert::Rewriter.new 'rspec', 'be_close_to_be_within' do
  within_files '**/*.rb' do
  end
end

Parameters:

  • file_patterns (String|Array<String>)

    string pattern or list of string pattern to find files, e.g. ['spec/*/_spec.rb']

  • block (Block)

    the block to rewrite code in the matching files.


237
238
239
240
241
242
243
244
# File 'lib/synvert/core/rewriter.rb', line 237

def within_files(file_patterns, &block)
  return if @sandbox

  return if @ruby_version && !@ruby_version.match?
  return if @gem_spec && !@gem_spec.match?

  Rewriter::Instance.new(self, Array(file_patterns), &block).process
end