rails5-spec-converter 1.0.17 → 1.0.18

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: b30c8ac0b4d815e5e31d439c98845c7d0b9ddf3c
4
- data.tar.gz: 2e5c2c892127cc0d03c05e8333a739bde933ff86
3
+ metadata.gz: bfd9be6c8436f667718042b22fb9c3c4391175f3
4
+ data.tar.gz: d5213028fc60af13ff39ea58167d55b038271416
5
5
  SHA512:
6
- metadata.gz: 9ab4fbf2947035aade8879caf9fa0673c7c038b3930385a450e1d612427389b6e03a40905c6c282c846bcb28b0b8007266a7234c5bf17ee93045d8d4d86f56d9
7
- data.tar.gz: 442ec1884fbbfa2d748281f7f07a0316892462a4d20e3c74688ac814bf1581f6de106daa80cb5fcc5f3a527f38fd53bbc40ebb6d86d7169cda3d9a427933d58f
6
+ metadata.gz: e0aa8e20f7b38a4702fd4729ea175b176fe9284a04f3367a5d01a16dd42379bb7d2742c7b16333a5500e0914f23ae79b5536525cec7f8e949041d4d42faf2f21
7
+ data.tar.gz: 718159ad161733c7b8384ea519560fae793caa430be899ef561a6b9b7c8cbb1224fcb73c649a143070bdb83c08c8db0e60744163628d248eeabbb38b0a0a910b
data/README.md CHANGED
@@ -68,6 +68,20 @@ even though `format` should be **outside** the params hash.
68
68
 
69
69
  * `--strategy skip` will never wrap the unknowable args in `params`
70
70
 
71
+ * `--strategy uglify` will attempt to split the hash into `params` and non-`params` hashes at runtime, like so:
72
+
73
+ ```
74
+ all_the_params = {
75
+ search: 'bayleef',
76
+ format: :json
77
+ }
78
+
79
+ _inner, _outer = all_the_params.partition { |k,v| %i{session flash method body xhr format}.include?(k) }.map { |a| Hash[a] }
80
+ get :users, _outer.merge(params: _inner)
81
+ ```
82
+
83
+ This should allow your tests to pass without deprecation warnings while introducing an enticing code cleanup oppurtunity.
84
+
71
85
  ### Whitespace
72
86
 
73
87
  #### Indentation
@@ -114,22 +128,6 @@ To force hashes to be written without extra whitespace in all files regardless o
114
128
 
115
129
  To force hashes to be written WITH extra whitespace in all files regardless of context, use the argument `--hash-spacing`.
116
130
 
117
- ### Future Work
118
-
119
- Future versions may introduce an `uglify` argument for `--strategy`, which resolves the runtime hash heys problem the following way:
120
-
121
- ```
122
- all_the_params = {
123
- search: 'bayleef',
124
- format: :json
125
- }
126
-
127
- r5sc_user_params, r5sc_other_params = all_the_params.partition { |k,v| %i{session flash method body xhr format}.include?(k) }.map { |a| Hash[a] }
128
- get :users, r5sc_other_params.merge(params: r5sc_user_params)
129
- ```
130
-
131
- This should allow your tests to pass without deprecation warnings while introducing an enticing code cleanup oppurtunity.
132
-
133
131
  ## Development
134
132
 
135
133
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rspec spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -11,6 +11,7 @@ module Rails5
11
11
  def initialize(content, options = TextTransformerOptions.new)
12
12
  @options = options
13
13
  @content = content
14
+ @textifier = NodeTextifier.new(@content)
14
15
 
15
16
  @source_buffer = Parser::Source::Buffer.new('(string)')
16
17
  @source_buffer.source = @content
@@ -48,7 +49,7 @@ module Rails5
48
49
  content: @content,
49
50
  options: @options,
50
51
  hash_node: args[0],
51
- original_indent: node.loc.expression.source_line.match(/^(\s*)/)[1]
52
+ original_indent: line_indent(node)
52
53
  )
53
54
 
54
55
  @source_rewriter.replace(
@@ -58,7 +59,7 @@ module Rails5
58
59
  end
59
60
  else
60
61
  warn_about_ambiguous_params(node) if @options.warn_about_ambiguous_params?
61
- wrap_arg(args[0], 'params') if @options.wrap_ambiguous_params?
62
+ handle_ambiguous_method_call!(node)
62
63
  end
63
64
 
64
65
  wrap_arg(args[1], 'headers') if args[1]
@@ -69,6 +70,52 @@ module Rails5
69
70
 
70
71
  private
71
72
 
73
+ def handle_ambiguous_method_call!(node)
74
+ target, verb, action, *args = node.children
75
+
76
+ if @options.wrap_ambiguous_params?
77
+ wrap_arg(args[0], 'params') if @options.wrap_ambiguous_params?
78
+ end
79
+
80
+ if @options.uglify_ambiguous_params?
81
+ keys = (HashRewriter::ALLOWED_KWARG_KEYS - [:params]).join(' ')
82
+ partition_clause = [
83
+ @textifier.node_to_string(args[0]),
84
+ "partition { |k,v| %i{#{keys}}.include?(k) }",
85
+ 'map { |a| Hash[a] }'
86
+ ].join('.')
87
+
88
+ text_before_node = node.loc.expression.source_line[0...node.loc.expression.column]
89
+ first_line_content = "_inner, _outer = #{partition_clause}"
90
+ if text_before_node =~ /^\s+$/
91
+ @source_rewriter.insert_before(node.loc.expression, "#{first_line_content}\n#{line_indent(node)}")
92
+ @source_rewriter.replace(args[0].loc.expression, '_outer.merge(params: _inner)')
93
+ else
94
+ return unless in_a_block_with_only_whitespace?(node)
95
+
96
+ new_indent = line_indent(node) + @options.indent
97
+ @source_rewriter.insert_before(node.loc.expression, "\n" + new_indent + first_line_content + "\n" + new_indent)
98
+ @source_rewriter.replace(args[0].loc.expression, '_outer.merge(params: _inner)')
99
+ @source_rewriter.insert_after(node.loc.expression, "\n#{line_indent(node)}")
100
+ trim_enclosing_spaces!(node)
101
+ end
102
+ end
103
+ end
104
+
105
+ def in_a_block_with_only_whitespace?(node)
106
+ return false unless node.parent && node.parent.block_type?
107
+ content_before = @content[node.parent.loc.begin.end_pos...node.loc.expression.begin_pos]
108
+ content_after = @content[node.loc.expression.end_pos...node.parent.loc.end.begin_pos]
109
+ content_before =~ /^\s*$/ && content_after =~ /^\s*$/
110
+ end
111
+
112
+ def trim_enclosing_spaces!(node)
113
+ before_range = Parser::Source::Range.new(@source_buffer, node.parent.loc.begin.end_pos, node.loc.expression.begin_pos)
114
+ after_range = Parser::Source::Range.new(@source_buffer, node.loc.expression.end_pos, node.parent.loc.end.begin_pos)
115
+ @source_rewriter.remove(before_range)
116
+ @source_rewriter.remove(after_range)
117
+ end
118
+
72
119
  def looks_like_route_definition?(hash_node)
73
120
  keys = hash_node.children.map { |pair| pair.children[0].children[0] }
74
121
  route_definition_keys = [:to, :controller]
@@ -110,6 +157,10 @@ module Rails5
110
157
  log "```\n#{node.loc.expression.source}\n```\n\n"
111
158
  end
112
159
 
160
+ def line_indent(node)
161
+ node.loc.expression.source_line.match(/^(\s*)/)[1]
162
+ end
163
+
113
164
  def log(str)
114
165
  return if @options.quiet?
115
166
 
@@ -15,6 +15,10 @@ class TextTransformerOptions
15
15
  @strategy == :optimistic
16
16
  end
17
17
 
18
+ def uglify_ambiguous_params?
19
+ @strategy == :uglify
20
+ end
21
+
18
22
  def warn_about_ambiguous_params?
19
23
  @warn_if_ambiguous
20
24
  end
@@ -1,5 +1,5 @@
1
1
  module Rails5
2
2
  module SpecConverter
3
- VERSION = "1.0.17"
3
+ VERSION = "1.0.18"
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.17
4
+ version: 1.0.18
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-22 00:00:00.000000000 Z
11
+ date: 2016-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser