rails5-spec-converter 1.0.8 → 1.0.9

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: 09acdaf5aac98f957adf479e15e41fdcb50c15b8
4
- data.tar.gz: 944323eb7a81f0968b71add26521ef9c05a056c7
3
+ metadata.gz: d5bf0eb89b52036356b4302e4ac69ce3b55b7736
4
+ data.tar.gz: 5a1af9450f8b860194ef3fbf3a81248a1747219f
5
5
  SHA512:
6
- metadata.gz: 0cdfe7db58fdef963722fabe00c9cf97ba976ad4ec9f5b6501f6c469fdcab2832aaf6a50d68f2dfdafc328923eb538712ba1316c002ced809ae50eea3a0b7504
7
- data.tar.gz: 0f717273ca4a3c6767852cf248ceb9a137325f093de2ce1fee6475207dc24b3b1cf9abd1c90a127e7d4373662b14b085eff5d6a5a4106d3e613b05cfe534ce46
6
+ metadata.gz: 4eb378462e4741677c8f55eeaad228c4b8b028f7087bddc7240e47fa851c37c0fe7e1a4a868d64d6660fa8c7149934783a2194b939dbb3cce02c425a212181e0
7
+ data.tar.gz: 6e1715037451e0cba49b3d3c4c618f29382df4b0a45e296bebc929f46d2d437a69bf1359272f149d1acdbe058b1ba71f2eb20ef19f024343109f6c997502dc82
@@ -12,18 +12,18 @@ module Rails5
12
12
  @indent = options[:indent] || ' '
13
13
  @quiet = options[:quiet]
14
14
  @content = content
15
- end
16
15
 
17
- def transform
18
- source_buffer = Parser::Source::Buffer.new('(string)')
19
- source_buffer.source = @content
16
+ @source_buffer = Parser::Source::Buffer.new('(string)')
17
+ @source_buffer.source = @content
20
18
 
21
19
  ast_builder = Astrolabe::Builder.new
22
- parser = Parser::CurrentRuby.new(ast_builder)
20
+ @parser = Parser::CurrentRuby.new(ast_builder)
23
21
 
24
- source_rewriter = Parser::Source::Rewriter.new(source_buffer)
22
+ @source_rewriter = Parser::Source::Rewriter.new(@source_buffer)
23
+ end
25
24
 
26
- root_node = parser.parse(source_buffer)
25
+ def transform
26
+ root_node = @parser.parse(@source_buffer)
27
27
  root_node.each_node(:send) do |node|
28
28
  target, verb, action, *args = node.children
29
29
  next unless args.length > 0
@@ -33,15 +33,18 @@ module Rails5
33
33
  next if looks_like_route_definition?(args[0])
34
34
  next if has_key?(args[0], :params)
35
35
 
36
- write_params_hash(source_rewriter, args[0])
36
+ write_params_hash(
37
+ hash_node: args[0],
38
+ original_indent: node.loc.expression.source_line.match(/^(\s*)/)[1]
39
+ )
37
40
  else
38
- wrap_arg(source_rewriter, args[0], 'params')
41
+ wrap_arg(args[0], 'params')
39
42
  end
40
43
 
41
- wrap_arg(source_rewriter, args[1], 'headers') if args[1]
44
+ wrap_arg(args[1], 'headers') if args[1]
42
45
  end
43
46
 
44
- source_rewriter.process
47
+ @source_rewriter.process
45
48
  end
46
49
 
47
50
  private
@@ -68,38 +71,44 @@ module Rails5
68
71
  hash_node.children.any? { |pair| pair.children[0].children[0] == key }
69
72
  end
70
73
 
71
- def write_params_hash(source_rewriter, hash_node)
72
- pairs_that_belong_in_params = []
73
- pairs_that_belong_outside_params = []
74
-
75
- hash_node.children.each do |pair|
76
- key = pair.children[0].children[0]
77
-
78
- if ALLOWED_KWARG_KEYS.include?(key)
79
- pairs_that_belong_outside_params << pair
80
- else
81
- pairs_that_belong_in_params << pair
82
- end
83
- end
74
+ def write_params_hash(hash_node:, original_indent: )
75
+ pairs_that_belong_in_params, pairs_that_belong_outside_params = partition_params(hash_node)
84
76
 
85
77
  if pairs_that_belong_in_params.length > 0
86
78
  joiner = joiner_between_pairs(hash_node)
87
79
  params_hash = appropriately_spaced_params_hash(
88
80
  hash_node: hash_node,
89
- pairs: pairs_that_belong_in_params
81
+ pairs: pairs_that_belong_in_params,
82
+ original_indent: original_indent
90
83
  )
91
84
 
92
85
  rewritten_hashes = ["params: #{params_hash}"]
93
86
  if pairs_that_belong_outside_params.length > 0
94
87
  rewritten_hashes << restring_hash(pairs_that_belong_outside_params, joiner: joiner)
95
88
  end
96
- source_rewriter.replace(
89
+ @source_rewriter.replace(
97
90
  hash_node.loc.expression,
98
91
  rewritten_hashes.join(joiner)
99
92
  )
100
93
  end
101
94
  end
102
95
 
96
+ def partition_params(hash_node)
97
+ pairs_that_belong_in_params = []
98
+ pairs_that_belong_outside_params = []
99
+
100
+ hash_node.children.each do |pair|
101
+ key = pair.children[0].children[0]
102
+
103
+ if ALLOWED_KWARG_KEYS.include?(key)
104
+ pairs_that_belong_outside_params << pair
105
+ else
106
+ pairs_that_belong_in_params << pair
107
+ end
108
+ end
109
+ return pairs_that_belong_in_params, pairs_that_belong_outside_params
110
+ end
111
+
103
112
  def indent_before_first_pair(hash_node)
104
113
  return nil unless hash_node.children.length > 0
105
114
 
@@ -114,6 +123,17 @@ module Rails5
114
123
  extract_indent(text_after_last_pair)
115
124
  end
116
125
 
126
+ def indent_of_first_value_if_multiline(hash_node, original_indent)
127
+ return nil if hash_node.children.length == 0
128
+ first_value = hash_node.children[0].children[1]
129
+ return nil unless first_value.hash_type? || first_value.array_type?
130
+ value_str_lines = node_to_string(first_value).split("\n")
131
+ return nil if value_str_lines.length == 1
132
+ return nil unless value_str_lines[0].match(/[\s\[{]/)
133
+
134
+ value_str_lines[1].match(/^(\s*)/)[1].sub(original_indent, '')
135
+ end
136
+
117
137
  def additional_indent(hash_node)
118
138
  return nil if indent_before_first_pair(hash_node)
119
139
 
@@ -122,15 +142,14 @@ module Rails5
122
142
  end
123
143
 
124
144
  def existing_indent(hash_node)
125
- previous_sibling = hash_node.parent.children[hash_node.sibling_index - 1]
126
- text_before_hash = text_between_siblings(previous_sibling, hash_node)
145
+ text_before_hash = text_before_node(hash_node)
127
146
  whitespace_indent = extract_indent(text_before_hash)
128
147
  return whitespace_indent if whitespace_indent
129
148
 
130
149
  return indent_before_first_pair(hash_node) if indent_before_first_pair(hash_node)
131
150
 
132
151
  joiner = joiner_between_pairs(hash_node)
133
- extract_indent(joiner)
152
+ extract_indent(joiner) || ''
134
153
  end
135
154
 
136
155
  def has_space_after_curly?(hash_node)
@@ -154,36 +173,49 @@ module Rails5
154
173
  @content[node1.loc.expression.end_pos...node2.loc.expression.begin_pos]
155
174
  end
156
175
 
157
- def appropriately_spaced_params_hash(hash_node:, pairs:)
176
+ def appropriately_spaced_params_hash(hash_node:, pairs:, original_indent: )
177
+ outer_indent = existing_indent(hash_node)
178
+ middle_indent = indent_of_first_value_if_multiline(hash_node, original_indent)
158
179
  inner_indent = additional_indent(hash_node)
159
180
 
160
- if inner_indent || indent_before_first_pair(hash_node)
161
- outer_indent = existing_indent(hash_node)
181
+ if inner_indent || middle_indent || indent_before_first_pair(hash_node)
162
182
  restrung_hash = restring_hash(
163
183
  pairs,
164
184
  indent: outer_indent + (inner_indent || ''),
165
185
  joiner: ",\n"
166
186
  )
167
- "{\n#{restrung_hash}\n#{indent_after_last_pair(hash_node) || outer_indent}}"
187
+ if middle_indent
188
+ restrung_hash = original_indent + add_indent(restrung_hash, middle_indent)
189
+ end
190
+ final_brace_indent = if middle_indent
191
+ original_indent
192
+ else
193
+ indent_after_last_pair(hash_node) || outer_indent
194
+ end
195
+ "{\n#{restrung_hash}\n#{final_brace_indent}}"
168
196
  else
169
197
  curly_sep = has_space_after_curly?(hash_node) ? '' : ' '
170
198
  "{#{curly_sep}#{restring_hash(pairs)}#{curly_sep}}"
171
199
  end
172
200
  end
173
201
 
174
- def wrap_arg(source_rewriter, node, key)
202
+ def wrap_arg(node, key)
175
203
  node_loc = node.loc.expression
176
204
  node_source = node_loc.source
177
205
  if node.hash_type? && !node_source.match(/^\s*\{.*\}$/m)
178
206
  node_source = "{ #{node_source} }"
179
207
  end
180
- source_rewriter.replace(node_loc, "#{key}: #{node_source}")
208
+ @source_rewriter.replace(node_loc, "#{key}: #{node_source}")
181
209
  end
182
210
 
183
211
  def restring_hash(pairs, joiner: ", ", indent: '')
184
212
  pairs.map { |pair| "#{indent}#{pair.loc.expression.source}" }.join(joiner)
185
213
  end
186
214
 
215
+ def add_indent(str, indent)
216
+ str.split("\n").map { |line| indent + line }.join("\n")
217
+ end
218
+
187
219
  def extract_indent(str)
188
220
  return unless str
189
221
 
@@ -191,6 +223,17 @@ module Rails5
191
223
  match[1] if match
192
224
  end
193
225
 
226
+ def text_before_node(node)
227
+ previous_sibling = node.parent.children[node.sibling_index - 1]
228
+ return nil unless previous_sibling.loc.expression
229
+
230
+ text_between_siblings(previous_sibling, node)
231
+ end
232
+
233
+ def node_to_string(node)
234
+ @content[node.loc.expression.begin_pos...node.loc.expression.end_pos]
235
+ end
236
+
194
237
  def log(str)
195
238
  return if @quiet
196
239
 
@@ -1,5 +1,5 @@
1
1
  module Rails5
2
2
  module SpecConverter
3
- VERSION = "1.0.8"
3
+ VERSION = "1.0.9"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails5-spec-converter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 1.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Grathwell
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-07-23 00:00:00.000000000 Z
11
+ date: 2016-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -139,8 +139,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
139
  version: '0'
140
140
  requirements: []
141
141
  rubyforge_project:
142
- rubygems_version: 2.5.1
142
+ rubygems_version: 2.4.5
143
143
  signing_key:
144
144
  specification_version: 4
145
145
  summary: A tool to upgrade Rails 4-style specs to Rails 5-style
146
146
  test_files: []
147
+ has_rdoc: