rails5-spec-converter 1.0.11 → 1.0.12

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: 5358fe3940ec49c5084895afcffe7cf51a07d06a
4
- data.tar.gz: b0363014912a09bf02040da8f8b2f738358f4613
3
+ metadata.gz: dd33f82a367cba9cb53594ef8b6606e265267bf7
4
+ data.tar.gz: 369d01c3d4b27d19ea7de004054fc8376f5a5d6b
5
5
  SHA512:
6
- metadata.gz: 3d1f2a9f62a860a713aed9450112d3964aab94b626e09352d262082cb2a9a649ee473e2c017c5220b122a4a3df1106616bb3c26631911bc2594eced205f4c901
7
- data.tar.gz: 65f4b19f4086ec55cb1d24cadb0473e9277005d977e4ceddf6b53d0d10e07b529d52aa7350d3c5985ad4f8f957d54f4ce82db0c148eec8ae33fce99390a2f726
6
+ metadata.gz: 2694b5710bf049e67d199b643a07cf7fb456e8d83ee65c51e99f2ca4ef60a3174f564a732018a599191e728cf9e68d7ee36126828849d5bbc1bc9077d7b0e1a5
7
+ data.tar.gz: 0004df17a2b3a31d88ace9af26194c6281acd92da24fa58d707d00b3e83934b759d4b42bb453e4003aaa9df3d12d199fdf9cff1be6ec2ff729cfbdd2e44c6600
data/README.md CHANGED
@@ -39,6 +39,35 @@ If you want to specify a specific set of files instead, you can run `rails5-spec
39
39
 
40
40
  By default it will make some noise, run with `rails5-spec-converter --quiet` if you want it not to.
41
41
 
42
+ ### Strategy
43
+
44
+ `rails5-spec-converter` wants to partition your arguments into two sets, those that belong in `params` and those that don't.
45
+
46
+ But it doesn't do any runtime analysis, so it can only effectively sort out non-`params` keys if they're included in a hash literal on the test invocation site. Hence:
47
+
48
+ ```
49
+ all_the_params = {
50
+ search: 'bayleef',
51
+ format: :json
52
+ }
53
+
54
+ get :users, all_the_params
55
+ ```
56
+
57
+ will become
58
+
59
+ ```
60
+ get :users, params: all_the_params
61
+ ```
62
+
63
+ even though `format` should be **outside** the params hash.
64
+
65
+ * `--warn-if-ambiguous` will print a message every time `rails5-spec-converter` encounters this situation
66
+
67
+ * `--strategy optimistic` (default) will always wrap the unknowable args in `params`
68
+
69
+ * `--strategy skip` will never wrap the unknowable args in `params`
70
+
42
71
  ### Whitespace
43
72
 
44
73
  #### Indentation
@@ -85,32 +114,9 @@ To force hashes to be written without extra whitespace in all files regardless o
85
114
 
86
115
  To force hashes to be written WITH extra whitespace in all files regardless of context, use the argument `--hash-spacing`.
87
116
 
88
- ## Limitations
89
-
90
- `rails5-spec-converter` wants to partition your arguments into two sets, those that belong in `params` and those that don't.
91
-
92
- But it doesn't do any runtime analysis, so it can only effectively sort out non-`params` keys if they're included in a hash literal on the test invocation site. Hence:
93
-
94
- ```
95
- all_the_params = {
96
- search: 'bayleef',
97
- format: :json
98
- }
99
-
100
- get :users, all_the_params
101
- ```
102
-
103
- will become
104
-
105
- ```
106
- get :users, params: all_the_params
107
- ```
108
-
109
- even though `format` should be **outside** the params hash.
110
-
111
- #### Future Work
117
+ ### Future Work
112
118
 
113
- In cases where the hash keys are unknowable at parse time, future versions may optionally attempt to produce this sort of result:
119
+ Future versions may introduce an `uglify` argument for `--strategy`, which resolves the runtime hash heys problem the following way:
114
120
 
115
121
  ```
116
122
  all_the_params = {
@@ -122,7 +128,7 @@ r5sc_user_params, r5sc_other_params = all_the_params.partition { |k,v| %i{sessio
122
128
  get :users, r5sc_other_params.merge(params: r5sc_user_params)
123
129
  ```
124
130
 
125
- ...which should allow your tests to pass without deprecation warnings while introducing an enticing code cleanup oppurtunity.
131
+ This should allow your tests to pass without deprecation warnings while introducing an enticing code cleanup oppurtunity.
126
132
 
127
133
  ## Development
128
134
 
@@ -1,4 +1,5 @@
1
1
  require 'rails5/spec_converter/text_transformer'
2
+ require 'rails5/spec_converter/text_transformer_options'
2
3
  require 'rails5/spec_converter/version'
3
4
  require 'optparse'
4
5
 
@@ -6,7 +7,7 @@ module Rails5
6
7
  module SpecConverter
7
8
  class CLI
8
9
  def initialize
9
- @options = {}
10
+ @options = TextTransformerOptions.new
10
11
  OptionParser.new do |opts|
11
12
  opts.banner = "Usage: rails5-spec-converter [options] [files]"
12
13
 
@@ -16,15 +17,23 @@ module Rails5
16
17
  end
17
18
 
18
19
  opts.on("-q", "--quiet", "Run quietly") do |q|
19
- @options[:quiet] = q
20
+ @options.quiet = q
20
21
  end
21
22
 
22
23
  opts.on("-i", "--indent INDENT", "Use specified string for indentation (default is two spaces)") do |indent|
23
- @options[:indent] = indent.gsub("\\t", "\t")
24
+ @options.indent = indent.gsub("\\t", "\t")
24
25
  end
25
26
 
26
27
  opts.on("--[no-]hash-spacing", "Always/never add space around hashes ({foo: 'bar'} vs { foo: 'bar' })") do |hash_spacing|
27
- @options[:hash_spacing] = hash_spacing
28
+ @options.hash_spacing = hash_spacing
29
+ end
30
+
31
+ opts.on("-s", "--strategy STRATEGY", "Set unknown hash parameters strategy (see README)") do |strategy|
32
+ @options.strategy = strategy.to_sym
33
+ end
34
+
35
+ opts.on("--warn-if-ambiguous", "Emit warnings when hash parameters are unknowable (see README)") do |warn_if_ambigous|
36
+ @options.warn_if_ambiguous = warn_if_ambigous
28
37
  end
29
38
  end.parse!
30
39
 
@@ -39,6 +48,7 @@ module Rails5
39
48
  log "Processing: #{file_path}"
40
49
 
41
50
  original_content = File.read(file_path)
51
+ @options.file_path = file_path
42
52
  transformed_content = Rails5::SpecConverter::TextTransformer.new(original_content, @options).transform
43
53
  File.write(file_path, transformed_content)
44
54
  end
@@ -46,7 +56,7 @@ module Rails5
46
56
  end
47
57
 
48
58
  def log(str)
49
- return if @options[:quiet]
59
+ return if @options.quiet?
50
60
 
51
61
  puts str
52
62
  end
@@ -1,5 +1,6 @@
1
1
  require 'parser/current'
2
2
  require 'astrolabe/builder'
3
+ require 'rails5/spec_converter/text_transformer_options'
3
4
 
4
5
  module Rails5
5
6
  module SpecConverter
@@ -8,10 +9,8 @@ module Rails5
8
9
  ALLOWED_KWARG_KEYS = %i(params session flash method body xhr format)
9
10
 
10
11
  class TextTransformer
11
- def initialize(content, options = {})
12
- @indent = options[:indent] || ' '
13
- @hash_spacing = options[:hash_spacing]
14
- @quiet = options[:quiet]
12
+ def initialize(content, options = TextTransformerOptions.new)
13
+ @options = options
15
14
  @content = content
16
15
 
17
16
  @source_buffer = Parser::Source::Buffer.new('(string)')
@@ -30,7 +29,9 @@ module Rails5
30
29
  next unless args.length > 0
31
30
  next unless target.nil? && HTTP_VERBS.include?(verb)
32
31
 
33
- if args[0].hash_type? && args[0].children.length > 0
32
+ if args[0].hash_type? && args[0].children.length == 0
33
+ wrap_arg(args[0], 'params')
34
+ elsif args[0].hash_type?
34
35
  next if looks_like_route_definition?(args[0])
35
36
  next if has_kwsplat?(args[0])
36
37
  next if has_key?(args[0], :params)
@@ -40,7 +41,8 @@ module Rails5
40
41
  original_indent: node.loc.expression.source_line.match(/^(\s*)/)[1]
41
42
  )
42
43
  else
43
- wrap_arg(args[0], 'params')
44
+ warn_about_ambiguous_params(node) if @options.warn_about_ambiguous_params?
45
+ wrap_arg(args[0], 'params') if @options.wrap_ambiguous_params?
44
46
  end
45
47
 
46
48
  wrap_arg(args[1], 'headers') if args[1]
@@ -144,7 +146,7 @@ module Rails5
144
146
  return nil if indent_before_first_pair(hash_node)
145
147
 
146
148
  joiner = joiner_between_pairs(hash_node)
147
- joiner && joiner.include?("\n") ? @indent : nil
149
+ joiner && joiner.include?("\n") ? @options.indent : nil
148
150
  end
149
151
 
150
152
  def existing_indent(hash_node)
@@ -206,8 +208,8 @@ module Rails5
206
208
  end
207
209
 
208
210
  def determine_curly_sep(hash_node)
209
- return ' ' if @hash_spacing == true
210
- return '' if @hash_spacing == false
211
+ return ' ' if @options.hash_spacing == true
212
+ return '' if @options.hash_spacing == false
211
213
 
212
214
  no_space_after_curly?(hash_node) ? '' : ' '
213
215
  end
@@ -247,8 +249,14 @@ module Rails5
247
249
  @content[node.loc.expression.begin_pos...node.loc.expression.end_pos]
248
250
  end
249
251
 
252
+ def warn_about_ambiguous_params(node)
253
+ log "Ambiguous params found"
254
+ log "#{@options.file_path}:#{node.loc.line}" if @options.file_path
255
+ log "```\n#{node.loc.expression.source}\n```\n\n"
256
+ end
257
+
250
258
  def log(str)
251
- return if @quiet
259
+ return if @options.quiet?
252
260
 
253
261
  puts str
254
262
  end
@@ -0,0 +1,25 @@
1
+ class TextTransformerOptions
2
+ attr_accessor :strategy, :hash_spacing, :indent, :file_path
3
+ attr_writer :quiet, :warn_if_ambiguous
4
+
5
+ def initialize
6
+ @file_path = nil
7
+ @strategy = :optimistic
8
+ @quiet = false
9
+ @indent = ' '
10
+ @hash_spacing = nil
11
+ @warn_if_ambiguous = false
12
+ end
13
+
14
+ def wrap_ambiguous_params?
15
+ @strategy == :optimistic
16
+ end
17
+
18
+ def warn_about_ambiguous_params?
19
+ @warn_if_ambiguous
20
+ end
21
+
22
+ def quiet?
23
+ @quiet
24
+ end
25
+ end
@@ -1,5 +1,5 @@
1
1
  module Rails5
2
2
  module SpecConverter
3
- VERSION = "1.0.11"
3
+ VERSION = "1.0.12"
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.11
4
+ version: 1.0.12
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-01 00:00:00.000000000 Z
11
+ date: 2016-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -117,6 +117,7 @@ files:
117
117
  - lib/rails5/spec_converter.rb
118
118
  - lib/rails5/spec_converter/cli.rb
119
119
  - lib/rails5/spec_converter/text_transformer.rb
120
+ - lib/rails5/spec_converter/text_transformer_options.rb
120
121
  - lib/rails5/spec_converter/version.rb
121
122
  - rails5-spec-converter.gemspec
122
123
  homepage: https://github.com/tjgrathwell/rails5-spec-converter