synvert-core 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b4282ee9f0e489971000bf8cedfc613d9fc44c11
4
- data.tar.gz: 0f27f2a20c1f7a111692436d86c4b10f2d9790f9
3
+ metadata.gz: 0f24ec1fdcc29492b0b5e7747f7541c3eaac7e40
4
+ data.tar.gz: 3ff09347d10f30e79cd03a53c00d57dd0cb6a299
5
5
  SHA512:
6
- metadata.gz: 564a1c66e905b84e830a8f8a782336ba0b4640962ebbd7dea6d3fd04c0817d87d90061a5e596d953ff3c5bc3973563ab06e65666fca95146450b4871e81de3f4
7
- data.tar.gz: b701564b0eff45f5d9bc439f524946929e31d5b9da10547c24593861f46c0c88cf3ea98ac61af66071a0f6be9daf4537ac5178fcbddb2930d6f60fe3f879652a
6
+ metadata.gz: 8fc6bf37a059cb3d04bdcdc1bb8d464250eab3fb81c8ac724f2e6ce2eb88908c166ca258b831b1c6569fcb8006aba3be4da57ee0602955aaa657395edc2691a4
7
+ data.tar.gz: 879e33c2e928a1607bbabf7d4746242510e655e7fd7aea4d0f72b66efd84183d818347c609fa85afa2bf3c2339c688c266a24ee692cf93df2242702ffcc4c416
data/CHANGELOG.md CHANGED
@@ -1,10 +1,15 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.11.0 (2016-07-31)
4
+
5
+ * Add options to Rewriter::Instance
6
+ * Add sort_by option to Rewriter::Instance
7
+
3
8
  ## 0.10.0 (2016-07-31)
4
9
 
5
10
  * Use parser 2.3.1.2
6
11
  * Add options to Rewriter::Action
7
- * Add autoindent option
12
+ * Add autoindent option to Rewriter::Action
8
13
 
9
14
  ## 0.9.0 (2015-12-23)
10
15
 
@@ -3,6 +3,8 @@
3
3
  module Synvert::Core
4
4
  # Action defines rewriter action, add, replace or remove code.
5
5
  class Rewriter::Action
6
+ DEFAULT_OPTIONS = { autoindent: true }
7
+
6
8
  # Initialize an action.
7
9
  #
8
10
  # @param instance [Synvert::Core::Rewriter::Instance]
@@ -11,7 +13,7 @@ module Synvert::Core
11
13
  def initialize(instance, code, options={})
12
14
  @instance = instance
13
15
  @code = code
14
- @options = default_options.merge(options)
16
+ @options = DEFAULT_OPTIONS.merge(options)
15
17
  @node = @instance.current_node
16
18
  end
17
19
 
@@ -41,20 +43,6 @@ module Synvert::Core
41
43
  def rewritten_source
42
44
  @rewritten_source ||= @node.rewritten_source(@code)
43
45
  end
44
-
45
- # Compare actions by begin position.
46
- #
47
- # @param action [Synvert::Core::Rewriter::Action]
48
- # @return [Integer] -1, 0 or 1
49
- def <=>(action)
50
- self.begin_pos <=> action.begin_pos
51
- end
52
-
53
- private
54
-
55
- def default_options
56
- { autoindent: true }
57
- end
58
46
  end
59
47
  end
60
48
 
@@ -64,16 +64,20 @@ module Synvert::Core
64
64
  # @return current filename
65
65
  attr_accessor :current_node, :current_file
66
66
 
67
+ DEFAULT_OPTIONS = { sort_by: 'begin_pos' }
68
+
67
69
  # Initialize an instance.
68
70
  #
69
71
  # @param rewriter [Synvert::Core::Rewriter]
70
72
  # @param file_pattern [String] pattern to find files, e.g. spec/**/*_spec.rb
73
+ # @param options [Hash] instance options, it includes :sort_by.
71
74
  # @param block [Block] block code to find nodes, match conditions and rewrite code.
72
75
  # @return [Synvert::Core::Rewriter::Instance]
73
- def initialize(rewriter, file_pattern, &block)
76
+ def initialize(rewriter, file_pattern, options={}, &block)
74
77
  @rewriter = rewriter
75
78
  @actions = []
76
79
  @file_pattern = file_pattern
80
+ @options = DEFAULT_OPTIONS.merge(options)
77
81
  @block = block
78
82
  rewriter.helpers.each { |helper| self.singleton_class.send(:define_method, helper[:name], &helper[:block]) }
79
83
  end
@@ -102,7 +106,7 @@ module Synvert::Core
102
106
  end
103
107
 
104
108
  if @actions.length > 0
105
- @actions.sort!
109
+ @actions.sort_by! { |action| action.send(@options[:sort_by]) }
106
110
  conflict_actions = get_conflict_actions
107
111
  @actions.reverse.each do |action|
108
112
  source[action.begin_pos...action.end_pos] = action.rewritten_code
@@ -206,13 +206,14 @@ module Synvert::Core
206
206
  # It creates a [Synvert::Core::Rewriter::Instance] to rewrite code.
207
207
  #
208
208
  # @param file_pattern [String] pattern to find files, e.g. spec/**/*_spec.rb
209
+ # @param options [Hash] instance options.
209
210
  # @param block [Block] the block to rewrite code in the matching files.
210
- def within_files(file_pattern, &block)
211
+ def within_files(file_pattern, options={}, &block)
211
212
  return if @sandbox
212
213
 
213
214
  if (!@ruby_version || @ruby_version.match?) &&
214
215
  (!@gem_spec || @gem_spec.match?)
215
- Rewriter::Instance.new(self, file_pattern, &block).process
216
+ Rewriter::Instance.new(self, file_pattern, options, &block).process
216
217
  end
217
218
  end
218
219
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = "0.10.0"
5
+ VERSION = "0.11.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synvert-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang