rails5-spec-converter 1.0.14 → 1.0.15

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: e9a3ed6b378206c21ca92e7fd621dd2e414cbfbc
4
- data.tar.gz: 963503b2f6e547cabb3bf11a147d055e7d04c53a
3
+ metadata.gz: 8f809b54f88c799e62a3da375d7e1c6386a24e71
4
+ data.tar.gz: 853ab71eb42ad2ff5f6834f25596e6f92a45a1d9
5
5
  SHA512:
6
- metadata.gz: c3fdfd22196475dc10ff315347fb4057903b7ec98fa5a8a906b579abc3c2ffb4b0ebe7c073175ee2e296756a7494f4fa6b29647bf326212fc9255e99c9b4acfe
7
- data.tar.gz: f2706c33bd3f869a9030f9296325666b61b0183d5085591991b73c7174687b33110cd85de0dc83a43452c940255af686315b7fc838bbf74743bd267eea9a5231
6
+ metadata.gz: 169d693d60ed16a8f81b30cf0e2e77d1cb019451b0ab64b5224bfaf2b3ed9c3a610fde5723a8d322b87c14aa8d70b8f9fb0943e1d979dbf1f1a5d633decd3f70
7
+ data.tar.gz: b2683718f15845812229c662c977302d58bcfa18a1d47b1efe95cf679194b5678fae3b9236d66f7fd962366c251d738f8787c85f6e1c8efe2421a3c02b9f3d73
@@ -0,0 +1,16 @@
1
+ class HashNodeTextAnalyzer
2
+ attr_reader :content, :hash_node
3
+
4
+ def initialize(content, hash_node)
5
+ @content = content
6
+ @hash_node = hash_node
7
+ end
8
+
9
+ def text_before_first_pair
10
+ content[hash_node.loc.expression.begin_pos...hash_node.children.first.loc.expression.begin_pos]
11
+ end
12
+
13
+ def text_after_last_pair
14
+ content[hash_node.children.last.loc.expression.end_pos...hash_node.loc.expression.end_pos]
15
+ end
16
+ end
@@ -1,6 +1,7 @@
1
1
  require 'parser/current'
2
2
  require 'astrolabe/builder'
3
3
  require 'rails5/spec_converter/text_transformer_options'
4
+ require 'rails5/spec_converter/hash_node_text_analyzer'
4
5
 
5
6
  module Rails5
6
7
  module SpecConverter
@@ -80,24 +81,34 @@ module Rails5
80
81
  hash_node.children.any? { |node| node.kwsplat_type? }
81
82
  end
82
83
 
84
+ def has_trailing_comma?(hash_node)
85
+ HashNodeTextAnalyzer.new(@content, hash_node).text_after_last_pair =~ /,/
86
+ end
87
+
83
88
  def has_key?(hash_node, key)
84
89
  hash_node.children.any? { |pair| pair.children[0].children[0] == key }
85
90
  end
86
91
 
87
92
  def write_params_hash(hash_node:, original_indent: )
88
93
  pairs_that_belong_in_params, pairs_that_belong_outside_params = partition_params(hash_node)
94
+ use_trailing_comma = has_trailing_comma?(hash_node)
89
95
 
90
96
  if pairs_that_belong_in_params.length > 0
91
97
  joiner = joiner_between_pairs(hash_node)
92
98
  params_hash = appropriately_spaced_params_hash(
93
99
  hash_node: hash_node,
94
100
  pairs: pairs_that_belong_in_params,
95
- original_indent: original_indent
101
+ original_indent: original_indent,
102
+ use_trailing_comma: use_trailing_comma
96
103
  )
97
104
 
98
105
  rewritten_hashes = ["params: #{params_hash}"]
99
106
  if pairs_that_belong_outside_params.length > 0
100
- rewritten_hashes << restring_hash(pairs_that_belong_outside_params, joiner: joiner)
107
+ rewritten_hashes << restring_hash(
108
+ pairs_that_belong_outside_params,
109
+ joiner: joiner,
110
+ use_trailing_comma: use_trailing_comma
111
+ )
101
112
  end
102
113
  @source_rewriter.replace(
103
114
  hash_node.loc.expression,
@@ -125,15 +136,13 @@ module Rails5
125
136
  def indent_before_first_pair(hash_node)
126
137
  return nil unless hash_node.children.length > 0
127
138
 
128
- text_before_first_pair = @content[hash_node.loc.expression.begin_pos...hash_node.children.first.loc.expression.begin_pos]
129
- extract_indent(text_before_first_pair)
139
+ extract_indent(HashNodeTextAnalyzer.new(@content, hash_node).text_before_first_pair)
130
140
  end
131
141
 
132
142
  def indent_after_last_pair(hash_node)
133
143
  return nil unless hash_node.children.length > 0
134
144
 
135
- text_after_last_pair = @content[hash_node.children.last.loc.expression.end_pos...hash_node.loc.expression.end_pos]
136
- extract_indent(text_after_last_pair)
145
+ extract_indent(HashNodeTextAnalyzer.new(@content, hash_node).text_after_last_pair)
137
146
  end
138
147
 
139
148
  def indent_of_first_value_if_multiline(hash_node, original_indent)
@@ -188,7 +197,7 @@ module Rails5
188
197
  @content[node1.loc.expression.end_pos...node2.loc.expression.begin_pos]
189
198
  end
190
199
 
191
- def appropriately_spaced_params_hash(hash_node:, pairs:, original_indent: )
200
+ def appropriately_spaced_params_hash(hash_node:, pairs:, original_indent:, use_trailing_comma:)
192
201
  outer_indent = existing_indent(hash_node)
193
202
  middle_indent = indent_of_first_value_if_multiline(hash_node, original_indent)
194
203
  inner_indent = additional_indent(hash_node)
@@ -197,7 +206,8 @@ module Rails5
197
206
  restrung_hash = restring_hash(
198
207
  pairs,
199
208
  indent: outer_indent + (inner_indent || ''),
200
- joiner: ",\n"
209
+ joiner: ",\n",
210
+ use_trailing_comma: use_trailing_comma
201
211
  )
202
212
  if middle_indent
203
213
  restrung_hash = original_indent + add_indent(restrung_hash, middle_indent)
@@ -210,7 +220,7 @@ module Rails5
210
220
  "{\n#{restrung_hash}\n#{final_brace_indent}}"
211
221
  else
212
222
  curly_sep = determine_curly_sep(hash_node)
213
- "{#{curly_sep}#{restring_hash(pairs)}#{curly_sep}}"
223
+ "{#{curly_sep}#{restring_hash(pairs, use_trailing_comma: use_trailing_comma)}#{curly_sep}}"
214
224
  end
215
225
  end
216
226
 
@@ -230,8 +240,13 @@ module Rails5
230
240
  @source_rewriter.replace(node_loc, "#{key}: #{node_source}")
231
241
  end
232
242
 
233
- def restring_hash(pairs, joiner: ", ", indent: '')
234
- pairs.map { |pair| "#{indent}#{pair.loc.expression.source}" }.join(joiner)
243
+ def restring_hash(pairs, joiner: ", ", indent: '', use_trailing_comma: false)
244
+ hash_string = pairs.map { |pair| "#{indent}#{pair.loc.expression.source}" }.join(joiner)
245
+ if use_trailing_comma
246
+ hash_string + ','
247
+ else
248
+ hash_string
249
+ end
235
250
  end
236
251
 
237
252
  def add_indent(str, indent)
@@ -1,5 +1,5 @@
1
1
  module Rails5
2
2
  module SpecConverter
3
- VERSION = "1.0.14"
3
+ VERSION = "1.0.15"
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.14
4
+ version: 1.0.15
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-09-11 00:00:00.000000000 Z
11
+ date: 2016-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -116,6 +116,7 @@ files:
116
116
  - exe/rails5-spec-converter
117
117
  - lib/rails5/spec_converter.rb
118
118
  - lib/rails5/spec_converter/cli.rb
119
+ - lib/rails5/spec_converter/hash_node_text_analyzer.rb
119
120
  - lib/rails5/spec_converter/text_transformer.rb
120
121
  - lib/rails5/spec_converter/text_transformer_options.rb
121
122
  - lib/rails5/spec_converter/version.rb