synvert 0.0.12 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 904cb80d27d9364b9521a6002db88319440d6cd0
4
- data.tar.gz: b065e2a1aa186550d5051a1805aed0b0b36770bd
3
+ metadata.gz: c9b185412549d5579efb12b82033786c586cc6f7
4
+ data.tar.gz: cce0f663f05dc9a481edb2f46c41d4e3e0ee2ed2
5
5
  SHA512:
6
- metadata.gz: 8470c708ef1bd0d97a360c3cfa7c49576311ae8a0f7197b3da69fe49a742c54d3b49e3b79784a9543c4cf5b7f2d649339bc24d885fb7f50f1ced7bdab628112d
7
- data.tar.gz: 30f9727f898c103b693d46754f6e8c9317d35a98e3fed440ab7e5a8f4077b21fee2313b5670a999e9c7152333398f7e1b34e37009cdacd2a38a58a57dff1ff94
6
+ metadata.gz: 796bf4d58f0a30083a1522b70ff8d6cc746d5c982e0081df157a073278dc0e99c45e977390211adaea88979e12019b5da69972eac7fe667ff7df076ab4c2b5fe
7
+ data.tar.gz: 05d8b0d8c84a2141fa93c245f4d5a7be3095b86c1782bee74fa2656db3386f4e20e33c9d5e34d9d14e3853fd440869f8619fbbf5cce9c396a3edac4fe98705dd
@@ -1,4 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 1.9.3
3
4
  - 2.0.0
4
5
  - 2.1.0
@@ -1,5 +1,12 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.0.13
4
+
5
+ * Add keys and values rules for hash node
6
+ * Add key and value rules for pair node
7
+ * Add ruby new hash syntax snippet
8
+ * Add ruby new lambda syntax snippet
9
+
3
10
  ## 0.0.12
4
11
 
5
12
  * Allow define multiple scopes and conditions in one instance
data/README.md CHANGED
@@ -9,6 +9,8 @@ automatically.
9
9
 
10
10
  **synvert is still in alpha stage**
11
11
 
12
+ synvert supports ruby >= 1.9.3
13
+
12
14
  ## Installation
13
15
 
14
16
  Install it using rubygems
@@ -21,11 +23,10 @@ $ gem install synvert
21
23
 
22
24
  ```
23
25
  $ synvert -h
24
- Usage: synvert [options] [project_path]
25
- --load-snippets SNIPPET_PATHS
26
- load additional snippets, snippet paths can be local file path or remote http url
27
- --list-snippets list all available snippets
28
- --run-snippets SNIPPET_NAMES run specified snippets
26
+ Usage: synvert [project_path]
27
+ -d, --load SNIPPET_PATHS load additional snippets, snippet paths can be local file path or remote http url
28
+ -l, --list list all available snippets
29
+ -r, --run SNIPPET_NAMES run specified snippets
29
30
  ```
30
31
 
31
32
  e.g.
@@ -60,6 +61,8 @@ rspec_new_syntax | Use RSpec new syntax, it contains all
60
61
  convert_rspec_one_liner_expectation | RSpec converts one liner expectation
61
62
  convert_rspec_should_to_expect | RSpec converts should to expect
62
63
  convert_rspec_stub_and_mock_to_double | RSpec converts stub and mock to double
64
+ ruby_new_hash_syntax | Ruby uses new hash syntax
65
+ ruby_new_lambda_syntax | Ruby uses new lambda syntax
63
66
 
64
67
  ## Documentation
65
68
 
@@ -16,13 +16,13 @@ module Synvert
16
16
  command = :run
17
17
  optparse = OptionParser.new do |opts|
18
18
  opts.banner = "Usage: synvert [project_path]"
19
- opts.on '--load-snippets SNIPPET_PATHS', 'load additional snippets, snippet paths can be local file path or remote http url' do |snippet_paths|
19
+ opts.on '-d', '--load SNIPPET_PATHS', 'load additional snippets, snippet paths can be local file path or remote http url' do |snippet_paths|
20
20
  Configuration.instance.set 'snippet_paths', snippet_paths.split(',')
21
21
  end
22
- opts.on '--list-snippets', 'list all available snippets' do
22
+ opts.on '-l', '--list', 'list all available snippets' do
23
23
  command = :list
24
24
  end
25
- opts.on '--run-snippets SNIPPET_NAMES', 'run specified snippets' do |snippet_names|
25
+ opts.on '-r', '--run SNIPPET_NAMES', 'run specified snippets' do |snippet_names|
26
26
  Configuration.instance.set 'snippet_names', snippet_names.split(',')
27
27
  end
28
28
  end
@@ -66,6 +66,38 @@ class Parser::AST::Node
66
66
  end
67
67
  end
68
68
 
69
+ def keys
70
+ if :hash == self.type
71
+ self.children.map { |child| child.children[0] }
72
+ else
73
+ raise NotImplementedError.new "keys is not handled for #{self.inspect}"
74
+ end
75
+ end
76
+
77
+ def values
78
+ if :hash == self.type
79
+ self.children.map { |child| child.children[1] }
80
+ else
81
+ raise NotImplementedError.new "keys is not handled for #{self.inspect}"
82
+ end
83
+ end
84
+
85
+ def key
86
+ if :pair == self.type
87
+ self.children.first
88
+ else
89
+ raise NotImplementedError.new "key is not handled for #{self.inspect}"
90
+ end
91
+ end
92
+
93
+ def value
94
+ if :pair == self.type
95
+ self.children.last
96
+ else
97
+ raise NotImplementedError.new "value is not handled for #{self.inspect}"
98
+ end
99
+ end
100
+
69
101
  def source(instance)
70
102
  if self.loc.expression
71
103
  instance.current_source[self.loc.expression.begin_pos...self.loc.expression.end_pos]
@@ -1,50 +1,82 @@
1
1
  Synvert::Rewriter.new "convert_rails_dynamic_finders", "Convert rails dynamic finders" do
2
2
  helper_method 'dynamic_finder_to_hash' do |prefix|
3
3
  fields = node.message.to_s[prefix.length..-1].split("_and_")
4
- fields.length.times.map { |i|
5
- fields[i] + ": " + node.arguments[i].source(self)
6
- }.join(", ")
4
+ if fields.length == node.arguments.length && :hash != node.arguments.first.type
5
+ fields.length.times.map { |i|
6
+ fields[i] + ": " + node.arguments[i].source(self)
7
+ }.join(", ")
8
+ else
9
+ "{{arguments}}"
10
+ end
7
11
  end
8
12
 
9
13
  within_files '**/*.rb' do
10
14
  # find_all_by_... => where(...)
11
15
  with_node type: 'send', message: /find_all_by_/ do
12
16
  hash_params = dynamic_finder_to_hash("find_all_by_")
13
- replace_with "{{receiver}}.where(#{hash_params})"
17
+ if node.receiver
18
+ replace_with "{{receiver}}.where(#{hash_params})"
19
+ else
20
+ replace_with "where(#{hash_params})"
21
+ end
14
22
  end
15
23
 
16
24
  # find_by_... => where(...).first
17
25
  with_node type: 'send', message: /find_by_/ do
18
26
  if :find_by_id == node.message
19
- replace_with "{{receiver}}.find({{arguments}})"
27
+ if node.receiver
28
+ replace_with "{{receiver}}.find({{arguments}})"
29
+ else
30
+ replace_with "find({{arguments}}"
31
+ end
20
32
  elsif :find_by_sql != node.message
21
33
  hash_params = dynamic_finder_to_hash("find_by_")
22
- replace_with "{{receiver}}.where(#{hash_params}).first"
34
+ if node.receiver
35
+ replace_with "{{receiver}}.where(#{hash_params}).first"
36
+ else
37
+ replace_with "where(#{hash_params}).first"
38
+ end
23
39
  end
24
40
  end
25
41
 
26
42
  # find_last_by_... => where(...).last
27
43
  with_node type: 'send', message: /find_last_by_/ do
28
44
  hash_params = dynamic_finder_to_hash("find_last_by_")
29
- replace_with "{{receiver}}.where(#{hash_params}).last"
45
+ if node.receiver
46
+ replace_with "{{receiver}}.where(#{hash_params}).last"
47
+ else
48
+ replace_with "where(#{hash_params}).last"
49
+ end
30
50
  end
31
51
 
32
52
  # scoped_by_... => where(...)
33
53
  with_node type: 'send', message: /scoped_by_/ do
34
54
  hash_params = dynamic_finder_to_hash("scoped_by_")
35
- replace_with "{{receiver}}.where(#{hash_params})"
55
+ if node.receiver
56
+ replace_with "{{receiver}}.where(#{hash_params})"
57
+ else
58
+ replace_with "where(#{hash_params})"
59
+ end
36
60
  end
37
61
 
38
62
  # find_or_initialize_by_... => find_or_initialize_by(...)
39
63
  with_node type: 'send', message: /find_or_initialize_by_/ do
40
64
  hash_params = dynamic_finder_to_hash("find_or_initialize_by_")
41
- replace_with "{{receiver}}.find_or_initialize_by(#{hash_params})"
65
+ if node.receiver
66
+ replace_with "{{receiver}}.find_or_initialize_by(#{hash_params})"
67
+ else
68
+ replace_with "find_or_initialize_by(#{hash_params})"
69
+ end
42
70
  end
43
71
 
44
72
  # find_or_create_by_... => find_or_create_by(...)
45
73
  with_node type: 'send', message: /find_or_create_by_/ do
46
74
  hash_params = dynamic_finder_to_hash("find_or_create_by_")
47
- replace_with "{{receiver}}.find_or_create_by(#{hash_params})"
75
+ if node.receiver
76
+ replace_with "{{receiver}}.find_or_create_by(#{hash_params})"
77
+ else
78
+ replace_with "find_or_create_by(#{hash_params})"
79
+ end
48
80
  end
49
81
  end
50
82
  end
@@ -0,0 +1,15 @@
1
+ Synvert::Rewriter.new "ruby_new_hash_syntax", "Ruby uses new hash syntax" do
2
+ if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("1.9.0")
3
+ within_files '**/*.rb' do
4
+ # {:foo => 'bar'} => {foo: 'bar'}
5
+ within_node type: 'hash' do
6
+ with_node type: 'pair' do
7
+ if :sym == node.key.type
8
+ new_key = node.key.source(self)[1..-1]
9
+ replace_with "#{new_key}: {{value}}"
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ Synvert::Rewriter.new "ruby_new_lambda_syntax", "Ruby uses new lambda syntax" do
2
+ if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("1.9.0")
3
+ within_files '**/*.rb' do
4
+ # lambda { |a, b, c| a + b + c } => ->(a, b, c) { a + b + c }
5
+ within_node type: 'block', caller: {type: 'send', message: 'lambda'} do
6
+ if node.arguments.empty?
7
+ replace_with "-> { {{body}} }"
8
+ else
9
+ replace_with "->({{arguments}}) { {{body}} }"
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
2
 
3
3
  module Synvert
4
- VERSION = "0.0.12"
4
+ VERSION = "0.0.13"
5
5
  end
@@ -83,6 +83,34 @@ describe Parser::AST::Node do
83
83
  end
84
84
  end
85
85
 
86
+ describe "#keys" do
87
+ it 'gets for hash node' do
88
+ node = parse("{:foo => :bar, 'foo' => 'bar'}")
89
+ expect(node.keys).to eq [Parser::CurrentRuby.parse(':foo'), Parser::CurrentRuby.parse("'foo'")]
90
+ end
91
+ end
92
+
93
+ describe "#values" do
94
+ it 'gets for hash node' do
95
+ node = parse("{:foo => :bar, 'foo' => 'bar'}")
96
+ expect(node.values).to eq [Parser::CurrentRuby.parse(':bar'), Parser::CurrentRuby.parse("'bar'")]
97
+ end
98
+ end
99
+
100
+ describe "#key" do
101
+ it 'gets for pair node' do
102
+ node = parse("{:foo => 'bar'}").children[0]
103
+ expect(node.key).to eq Parser::CurrentRuby.parse(':foo')
104
+ end
105
+ end
106
+
107
+ describe "#value" do
108
+ it 'gets for hash node' do
109
+ node = parse("{:foo => 'bar'}").children[0]
110
+ expect(node.value).to eq Parser::CurrentRuby.parse("'bar'")
111
+ end
112
+ end
113
+
86
114
  describe "#condition" do
87
115
  it 'gets for if node' do
88
116
  node = parse('if defined?(Bundler); end')
@@ -11,63 +11,39 @@ describe 'Convert dynamic finders' do
11
11
  describe 'with fakefs', fakefs: true do
12
12
  let(:post_model_content) {'''
13
13
  class Post < ActiveRecord::Base
14
- def active_users_by_email(email)
14
+ def active_users
15
15
  User.find_all_by_email_and_active(email, true)
16
- end
17
-
18
- def first_active_user_by_email(email)
19
16
  User.find_by_email_and_active(email, true)
20
- end
21
-
22
- def last_active_user_by_email(email)
23
17
  User.find_last_by_email_and_active(email, true)
24
- end
25
-
26
- def scoped_active_user_by_email(email)
27
18
  User.scoped_by_email_and_active(email, true)
28
- end
29
-
30
- def active_users_by_sql(email)
31
19
  User.find_by_sql(["select * from users where email = ?", email])
32
- end
33
-
34
- def active_user_by_id(id)
35
20
  User.find_by_id(id)
21
+ User.find_by_account_id(Account.find_by_email(account_email).id)
22
+ User.find_or_create_by_email_and_login(parmas)
23
+ User.find_or_initialize_by_account_id(:account_id => account_id)
36
24
  end
37
25
 
38
- def active_user_by_account_email(account_email)
39
- User.find_by_account_id(Account.find_by_email(account_email).id)
26
+ def self.active_admins
27
+ find_all_by_role_and_active("admin", true)
40
28
  end
41
29
  end
42
30
  '''}
43
31
  let(:post_model_rewritten_content) {'''
44
32
  class Post < ActiveRecord::Base
45
- def active_users_by_email(email)
33
+ def active_users
46
34
  User.where(email: email, active: true)
47
- end
48
-
49
- def first_active_user_by_email(email)
50
35
  User.where(email: email, active: true).first
51
- end
52
-
53
- def last_active_user_by_email(email)
54
36
  User.where(email: email, active: true).last
55
- end
56
-
57
- def scoped_active_user_by_email(email)
58
37
  User.where(email: email, active: true)
59
- end
60
-
61
- def active_users_by_sql(email)
62
38
  User.find_by_sql(["select * from users where email = ?", email])
63
- end
64
-
65
- def active_user_by_id(id)
66
39
  User.find(id)
40
+ User.where(account_id: Account.where(email: account_email).first.id).first
41
+ User.find_or_create_by(parmas)
42
+ User.find_or_initialize_by(:account_id => account_id)
67
43
  end
68
44
 
69
- def active_user_by_account_email(account_email)
70
- User.where(account_id: Account.where(email: account_email).first.id).first
45
+ def self.active_admins
46
+ where(role: "admin", active: true)
71
47
  end
72
48
  end
73
49
  '''}
@@ -13,7 +13,7 @@ describe 'Use RSpec new syntax' do
13
13
  end
14
14
  end
15
15
 
16
- describe 'rspec', fakefs: true do
16
+ describe 'with fakefs', fakefs: true do
17
17
  let(:post_spec_content) {"""
18
18
  it 'case' do
19
19
  obj.should matcher
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Ruby uses new hash synax' do
4
+ before do
5
+ Synvert::Configuration.instance.set :path, '.'
6
+ allow_any_instance_of(Synvert::Rewriter::GemSpec).to receive(:match?).and_return(true)
7
+ rewriter_path = File.join(File.dirname(__FILE__), '../../../../lib/synvert/snippets/ruby/new_hash_syntax.rb')
8
+ @rewriter = eval(File.read(rewriter_path))
9
+ end
10
+
11
+ describe 'with fakefs', fakefs: true do
12
+ let(:test_content) {"""
13
+ {:foo => 'bar', 'foo' => 'bar'}
14
+ {:key1 => 'value1', :key2 => 'value2'}
15
+ """}
16
+ let(:test_rewritten_content) {"""
17
+ {foo: 'bar', 'foo' => 'bar'}
18
+ {key1: 'value1', key2: 'value2'}
19
+ """}
20
+
21
+ it 'process' do
22
+ File.write 'test.rb', test_content
23
+ @rewriter.process
24
+ expect(File.read 'test.rb').to eq test_rewritten_content
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Ruby uses new -> synax' do
4
+ before do
5
+ Synvert::Configuration.instance.set :path, '.'
6
+ allow_any_instance_of(Synvert::Rewriter::GemSpec).to receive(:match?).and_return(true)
7
+ rewriter_path = File.join(File.dirname(__FILE__), '../../../../lib/synvert/snippets/ruby/new_lambda_syntax.rb')
8
+ @rewriter = eval(File.read(rewriter_path))
9
+ end
10
+
11
+ describe 'with fakefs', fakefs: true do
12
+ let(:test_content) {"""
13
+ lambda { test }
14
+ lambda { |a, b, c| a + b + c }
15
+ """}
16
+ let(:test_rewritten_content) {"""
17
+ -> { test }
18
+ ->(a, b, c) { a + b + c }
19
+ """}
20
+
21
+ it 'process' do
22
+ File.write 'test.rb', test_content
23
+ @rewriter.process
24
+ expect(File.read 'test.rb').to eq test_rewritten_content
25
+ end
26
+ end
27
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synvert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-31 00:00:00.000000000 Z
11
+ date: 2014-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -140,6 +140,8 @@ files:
140
140
  - lib/synvert/snippets/rspec/one_liner_expectation.rb
141
141
  - lib/synvert/snippets/rspec/should_to_expect.rb
142
142
  - lib/synvert/snippets/rspec/stub_and_mock_to_double.rb
143
+ - lib/synvert/snippets/ruby/new_hash_syntax.rb
144
+ - lib/synvert/snippets/ruby/new_lambda_syntax.rb
143
145
  - lib/synvert/version.rb
144
146
  - spec/spec_helper.rb
145
147
  - spec/support/parser_helper.rb
@@ -157,6 +159,8 @@ files:
157
159
  - spec/synvert/snippets/rails/upgrade_3_1_to_3_2_spec.rb
158
160
  - spec/synvert/snippets/rails/upgrade_3_2_to_4_0_spec.rb
159
161
  - spec/synvert/snippets/rspec/new_syntax_spec.rb
162
+ - spec/synvert/snippets/ruby/new_hash_syntax_spec.rb
163
+ - spec/synvert/snippets/ruby/new_lambda_syntax_spec.rb
160
164
  - synvert.gemspec
161
165
  homepage: ''
162
166
  licenses:
@@ -199,3 +203,5 @@ test_files:
199
203
  - spec/synvert/snippets/rails/upgrade_3_1_to_3_2_spec.rb
200
204
  - spec/synvert/snippets/rails/upgrade_3_2_to_4_0_spec.rb
201
205
  - spec/synvert/snippets/rspec/new_syntax_spec.rb
206
+ - spec/synvert/snippets/ruby/new_hash_syntax_spec.rb
207
+ - spec/synvert/snippets/ruby/new_lambda_syntax_spec.rb