Class: Synvert::Core::Rewriter
- Inherits:
-
Object
- Object
- Synvert::Core::Rewriter
- 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: Condition, GemSpec, GotoScope, IfExistCondition, IfOnlyExistCondition, Instance, ReplaceErbStmtWithExprAction, RubyVersion, Scope, UnlessExistCondition, Warning, WithinScope
Constant Summary collapse
- DEFAULT_OPTIONS =
{ run_instance: true, write_to_file: true, parser: 'parser' }.freeze
Instance Attribute Summary collapse
-
#affected_files ⇒ Set
readonly
Affected fileds.
-
#gem_spec ⇒ Rewriter::GemSpec
readonly
The gem spec.
-
#group ⇒ String
readonly
The group of rewriter.
-
#helper ⇒ Array
readonly
Helper methods.
-
#helpers ⇒ Object
readonly
Returns the value of attribute helpers.
-
#name ⇒ String
readonly
The unique name of rewriter.
-
#options ⇒ Hash
The rewriter options.
-
#ruby_version ⇒ Rewriter::RubyVersion
readonly
The ruby version.
-
#sub_snippets ⇒ Array<Synvert::Core::Rewriter>
readonly
All rewriters this rewiter calls.
-
#test_results ⇒ Object
readonly
Returns the value of attribute test_results.
-
#warnings ⇒ Array<Synvert::Core::Rewriter::Warning>
readonly
Warning messages.
Class Method Summary collapse
-
.availables ⇒ Hash<String, Hash<String, Synvert::Core::Rewriter>>
Get all available rewriters.
-
.clear ⇒ Object
Clear all registered rewriters.
-
.fetch(group, name) ⇒ Synvert::Core::Rewriter
Fetch a rewriter by group and name.
-
.register(group, name, rewriter) ⇒ Object
Register a rewriter with its group and name.
Instance Method Summary collapse
-
#add_affected_file(file_path) ⇒ Object
Add an affected file.
-
#add_file(filename, content) ⇒ Object
It adds a new file.
-
#add_snippet(group, name = nil) ⇒ Object
It calls anther rewriter.
-
#add_warning(warning) ⇒ Object
Add a warning.
-
#call_helper(name, options = {}) ⇒ Object
It calls a shared rewriter.
-
#configure(options) ⇒ Object
Configure the rewriter.
-
#description(description = nil) ⇒ Object
It sets description of the rewrite or get description.
-
#helper_method(name) { ... } ⇒ Object
It defines helper method for Instance.
-
#if_gem(name, version) ⇒ Object
It compares version of the specified gem.
-
#if_ruby(version) ⇒ Object
It checks if ruby version is greater than or equal to the specified ruby version.
-
#initialize(group, name) { ... } ⇒ Rewriter
constructor
Initialize a Rewriter.
-
#load_data(key) ⇒ Object
Loads data with the given key.
- #parser ⇒ Object
-
#process ⇒ Object
Process the rewriter.
-
#process_with_sandbox ⇒ Object
Process rewriter with sandbox mode.
-
#remove_file(filename) ⇒ Object
It removes a file.
-
#reset ⇒ Object
Reset @warnings, @affected_files, and @test_results.
-
#save_data(key, value) ⇒ Object
Saves data with a given key and value.
- #test ⇒ Object
-
#with_configurations(configurations) { ... } ⇒ Object
Executes a block of code with temporary configurations.
-
#within_files(file_patterns, &block) ⇒ Object
(also: #within_file)
It finds specified files, and for each file, it will delegate to Instance to rewrite code.
Constructor Details
#initialize(group, name) { ... } ⇒ Rewriter
Initialize a Rewriter. When a rewriter is initialized, it is already registered.
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/synvert/core/rewriter.rb', line 114 def initialize(group, name, &block) @group = group @name = name @block = block @helpers = [] @sub_snippets = [] @options = DEFAULT_OPTIONS.dup reset self.class.register(@group, @name, self) end |
Instance Attribute Details
#affected_files ⇒ Set (readonly)
Returns affected fileds.
97 98 99 100 101 102 103 104 105 |
# File 'lib/synvert/core/rewriter.rb', line 97 attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec, :test_results |
#gem_spec ⇒ Rewriter::GemSpec (readonly)
Returns the gem spec.
97 98 99 100 101 102 103 104 105 |
# File 'lib/synvert/core/rewriter.rb', line 97 attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec, :test_results |
#group ⇒ String (readonly)
Returns the group of rewriter.
97 98 99 |
# File 'lib/synvert/core/rewriter.rb', line 97 def group @group end |
#helper ⇒ Array (readonly)
Returns helper methods.
97 98 99 100 101 102 103 104 105 |
# File 'lib/synvert/core/rewriter.rb', line 97 attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec, :test_results |
#helpers ⇒ Object (readonly)
Returns the value of attribute helpers.
97 98 99 |
# File 'lib/synvert/core/rewriter.rb', line 97 def helpers @helpers end |
#name ⇒ String (readonly)
Returns the unique name of rewriter.
97 98 99 100 101 102 103 104 105 |
# File 'lib/synvert/core/rewriter.rb', line 97 attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec, :test_results |
#options ⇒ Hash
Returns the rewriter options.
97 98 99 100 101 102 103 104 105 |
# File 'lib/synvert/core/rewriter.rb', line 97 attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec, :test_results |
#ruby_version ⇒ Rewriter::RubyVersion (readonly)
Returns the ruby version.
97 98 99 100 101 102 103 104 105 |
# File 'lib/synvert/core/rewriter.rb', line 97 attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec, :test_results |
#sub_snippets ⇒ Array<Synvert::Core::Rewriter> (readonly)
Returns all rewriters this rewiter calls.
97 98 99 100 101 102 103 104 105 |
# File 'lib/synvert/core/rewriter.rb', line 97 attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec, :test_results |
#test_results ⇒ Object (readonly)
Returns the value of attribute test_results.
97 98 99 100 101 102 103 104 105 |
# File 'lib/synvert/core/rewriter.rb', line 97 attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec, :test_results |
#warnings ⇒ Array<Synvert::Core::Rewriter::Warning> (readonly)
Returns warning messages.
97 98 99 100 101 102 103 104 105 |
# File 'lib/synvert/core/rewriter.rb', line 97 attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec, :test_results |
Class Method Details
.availables ⇒ Hash<String, Hash<String, Synvert::Core::Rewriter>>
Get all available rewriters
61 62 63 |
# File 'lib/synvert/core/rewriter.rb', line 61 def availables rewriters end |
.clear ⇒ Object
Clear all registered rewriters.
66 67 68 |
# File 'lib/synvert/core/rewriter.rb', line 66 def clear rewriters.clear end |
.fetch(group, name) ⇒ Synvert::Core::Rewriter
Fetch a rewriter by group and name.
52 53 54 55 56 |
# File 'lib/synvert/core/rewriter.rb', line 52 def fetch(group, name) group = group.to_s name = name.to_s rewriters.dig(group, name) end |
.register(group, name, rewriter) ⇒ Object
Register a rewriter with its group and name.
40 41 42 43 44 45 |
# File 'lib/synvert/core/rewriter.rb', line 40 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.
177 178 179 |
# File 'lib/synvert/core/rewriter.rb', line 177 def add_affected_file(file_path) @affected_files.add(file_path) end |
#add_file(filename, content) ⇒ Object
It adds a new file.
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/synvert/core/rewriter.rb', line 290 def add_file(filename, content) return unless @options[:run_instance] unless @options[:write_to_file] result = NodeMutation::Result.new(affected: true, conflicted: false) if Configuration.test_result == 'new_source' result.new_source = content else result.actions = [NodeMutation::Struct::Action.new(:add_file, 0, 0, content)] end result.file_path = filename merge_test_result(result) return end filepath = File.join(Configuration.root_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 = nil) ⇒ Object
It calls anther rewriter.
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/synvert/core/rewriter.rb', line 354 def add_snippet(group, name = nil) rewriter = if name Rewriter.fetch(group, name) || Utils.eval_snippet([group, name].join('/')) else Utils.eval_snippet(group) end return unless rewriter && rewriter.is_a?(Rewriter) rewriter. = @options preserve_current_parser do if !rewriter.[:write_to_file] results = rewriter.test merge_test_results(results) elsif rewriter.[:run_instance] rewriter.process else rewriter.process_with_sandbox end end @sub_snippets << rewriter end |
#add_warning(warning) ⇒ Object
Add a warning.
170 171 172 |
# File 'lib/synvert/core/rewriter.rb', line 170 def add_warning(warning) @warnings << warning end |
#call_helper(name, options = {}) ⇒ Object
It calls a shared rewriter.
386 387 388 389 390 391 392 393 |
# File 'lib/synvert/core/rewriter.rb', line 386 def call_helper(name, = {}) helper = Synvert::Core::Helper.fetch(name) || Utils.eval_snippet(name) return unless helper && helper.is_a?(Synvert::Core::Helper) preserve_current_parser do instance_exec(, &helper.block) end end |
#configure(options) ⇒ Object
Configure the rewriter
203 204 205 206 207 208 |
# File 'lib/synvert/core/rewriter.rb', line 203 def configure() @options = @options.merge() if [:parser] && ![Synvert::PARSER_PARSER, Synvert::SYNTAX_TREE_PARSER, Synvert::PRISM_PARSER].include?([:parser]) raise Errors::ParserNotSupported.new("Parser #{[:parser]} not supported") end end |
#description(description = nil) ⇒ Object
It sets description of the rewrite or get description.
217 218 219 220 221 222 223 |
# File 'lib/synvert/core/rewriter.rb', line 217 def description(description = nil) if description @description = description else @description end end |
#helper_method(name) { ... } ⇒ Object
It defines helper method for Instance.
408 409 410 |
# File 'lib/synvert/core/rewriter.rb', line 408 def helper_method(name, &block) @helpers << { name: name, block: block } end |
#if_gem(name, version) ⇒ Object
It compares version of the specified gem.
242 243 244 |
# File 'lib/synvert/core/rewriter.rb', line 242 def if_gem(name, version) @gem_spec = Rewriter::GemSpec.new(name, version) end |
#if_ruby(version) ⇒ Object
It checks if ruby version is greater than or equal to the specified ruby version.
231 232 233 |
# File 'lib/synvert/core/rewriter.rb', line 231 def if_ruby(version) @ruby_version = Rewriter::RubyVersion.new(version) end |
#load_data(key) ⇒ Object
Loads data with the given key.
432 433 434 |
# File 'lib/synvert/core/rewriter.rb', line 432 def load_data(key) Synvert::Core.instance_variable_get("@#{key}") end |
#parser ⇒ Object
181 182 183 |
# File 'lib/synvert/core/rewriter.rb', line 181 def parser @options[:parser] end |
#process ⇒ Object
Process the rewriter. It will call the block.
127 128 129 130 |
# File 'lib/synvert/core/rewriter.rb', line 127 def process @affected_files = Set.new instance_eval(&@block) end |
#process_with_sandbox ⇒ Object
Process rewriter with sandbox mode. It will call the block but doesn't change any file.
134 135 136 137 |
# File 'lib/synvert/core/rewriter.rb', line 134 def process_with_sandbox @options[:run_instance] = false process end |
#remove_file(filename) ⇒ Object
It removes a file.
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/synvert/core/rewriter.rb', line 321 def remove_file(filename) return unless @options[:run_instance] unless @options[:write_to_file] result = NodeMutation::Result.new(affected: true, conflicted: false) if Configuration.test_result == 'new_source' result.new_source = nil else result.actions = [NodeMutation::Struct::Action.new(:remove_file, 0, -1)] end result.file_path = filename merge_test_result(result) return end file_path = File.join(Configuration.root_path, filename) File.delete(file_path) if File.exist?(file_path) end |
#reset ⇒ Object
Reset @warnings, @affected_files, and @test_results.
186 187 188 189 190 |
# File 'lib/synvert/core/rewriter.rb', line 186 def reset @warnings = [] @affected_files = Set.new @test_results = Hash.new { |h, k| h[k] = [] } end |
#save_data(key, value) ⇒ Object
Saves data with a given key and value.
424 425 426 |
# File 'lib/synvert/core/rewriter.rb', line 424 def save_data(key, value) Synvert::Core.instance_variable_set("@#{key}", value) end |
#test ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/synvert/core/rewriter.rb', line 139 def test @options[:write_to_file] = false @affected_files = Set.new instance_eval(&@block) if Configuration.test_result == 'new_source' @test_results.values.flatten else @test_results.map do |filename, test_results| new_actions = test_results.map(&:actions).flatten.sort_by(&:end) last_start = -1 conflicted = new_actions.any? do |action| if last_start > action.end true else last_start = action.start false end end result = NodeMutation::Result.new(affected: true, conflicted: conflicted) result.actions = new_actions result.file_path = filename result end end end |
#with_configurations(configurations) { ... } ⇒ Object
Executes a block of code with temporary configurations.
416 417 418 |
# File 'lib/synvert/core/rewriter.rb', line 416 def with_configurations(configurations, &block) Configuration.with_temporary_configurations(configurations, &block) end |
#within_files(file_patterns, &block) ⇒ Object Also known as: within_file
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/synvert/core/rewriter.rb', line 255 def within_files(file_patterns, &block) return unless @options[:run_instance] return if @ruby_version && !@ruby_version.match? return if @gem_spec && !@gem_spec.match? if @options[:write_to_file] handle_one_file(Array(file_patterns)) do |file_path| instance = Instance.new(self, file_path, &block) instance.process end else results = handle_one_file(Array(file_patterns)) do |file_path| instance = Instance.new(self, file_path, &block) instance.test end merge_test_results(results) end end |