rails5-spec-converter 1.0.22 → 2.0.0
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7eb8605294b0f3e4029d4a37a66b92cfb59af830
|
4
|
+
data.tar.gz: 9cf52cc55cdd1d225469ad8d8995ad7cea52a4ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3ad5d20ee93db7c0ae5ecec047b647aab9f3661765b7c2d6c8dd1f22a88c9a0b4b5e1055fe47d9d7a962dbdf9a88bc34fb5ee2329769b827642910ae2f524a34
|
7
|
+
data.tar.gz: 88b4aa44487e0a6772e84949f3f41cebbc0dc98f06e10e25af31217d5317a3bb40501a8b7939bf24b6723ea7822fe391cb0b51b1c7951cf5401143dd1db4d753
|
data/README.md
CHANGED
@@ -39,49 +39,6 @@ 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
|
-
|
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
|
-
_outer, _inner = all_the_params.partition { |k,v| %i{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
|
-
|
85
42
|
### Whitespace
|
86
43
|
|
87
44
|
#### Indentation
|
@@ -27,14 +27,6 @@ module Rails5
|
|
27
27
|
opts.on("--[no-]hash-spacing", "Always/never add space around hashes ({foo: 'bar'} vs { foo: 'bar' })") do |hash_spacing|
|
28
28
|
@options.hash_spacing = hash_spacing
|
29
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_ambiguous|
|
36
|
-
@options.warn_if_ambiguous = warn_if_ambiguous
|
37
|
-
end
|
38
30
|
end.parse!
|
39
31
|
|
40
32
|
@files = ARGV
|
@@ -41,10 +41,6 @@ module Rails5
|
|
41
41
|
else
|
42
42
|
next if looks_like_route_definition?(args[0])
|
43
43
|
next if has_key?(args[0], :params)
|
44
|
-
if has_kwsplat?(args[0])
|
45
|
-
warn_about_ambiguous_params(node) if @options.warn_about_ambiguous_params?
|
46
|
-
next unless @options.wrap_ambiguous_params?
|
47
|
-
end
|
48
44
|
|
49
45
|
hash_rewriter = HashRewriter.new(
|
50
46
|
content: @content,
|
@@ -66,8 +62,7 @@ module Rails5
|
|
66
62
|
)
|
67
63
|
@source_rewriter.remove(nil_arg_range)
|
68
64
|
else
|
69
|
-
|
70
|
-
handle_ambiguous_method_call!(node)
|
65
|
+
wrap_arg(args[0], 'params')
|
71
66
|
end
|
72
67
|
|
73
68
|
wrap_extra_positional_args!(args) if args.length > 1
|
@@ -78,62 +73,6 @@ module Rails5
|
|
78
73
|
|
79
74
|
private
|
80
75
|
|
81
|
-
def handle_ambiguous_method_call!(node)
|
82
|
-
target, verb, action, *args = node.children
|
83
|
-
|
84
|
-
if @options.wrap_ambiguous_params?
|
85
|
-
wrap_arg(args[0], 'params') if @options.wrap_ambiguous_params?
|
86
|
-
end
|
87
|
-
|
88
|
-
if @options.uglify_ambiguous_params?
|
89
|
-
keys = HashRewriter::OUTSIDE_PARAMS_KEYS.join(' ')
|
90
|
-
partition_clause = [
|
91
|
-
@textifier.node_to_string(args[0]),
|
92
|
-
"partition { |k,v| %i{#{keys}}.include?(k) }",
|
93
|
-
'map { |a| Hash[a] }'
|
94
|
-
].join('.')
|
95
|
-
|
96
|
-
text_before_node = node.loc.expression.source_line[0...node.loc.expression.column]
|
97
|
-
first_line_content = "_outer, _inner = #{partition_clause}"
|
98
|
-
if text_before_node =~ /^\s+$/
|
99
|
-
@source_rewriter.insert_before(node.loc.expression, "#{first_line_content}\n#{line_indent(node)}")
|
100
|
-
if args.length == 1
|
101
|
-
@source_rewriter.replace(args[0].loc.expression, '_outer.merge(params: _inner)')
|
102
|
-
else
|
103
|
-
replacement_range = Parser::Source::Range.new(
|
104
|
-
@source_buffer,
|
105
|
-
args[0].loc.expression.begin_pos,
|
106
|
-
args[1].loc.expression.begin_pos
|
107
|
-
)
|
108
|
-
@source_rewriter.replace(replacement_range, '_outer.merge(params: _inner).merge(')
|
109
|
-
@source_rewriter.insert_after(args.last.loc.expression, ')')
|
110
|
-
end
|
111
|
-
else
|
112
|
-
return unless in_a_block_with_only_whitespace?(node)
|
113
|
-
|
114
|
-
new_indent = line_indent(node) + @options.indent
|
115
|
-
@source_rewriter.insert_before(node.loc.expression, "\n" + new_indent + first_line_content + "\n" + new_indent)
|
116
|
-
@source_rewriter.replace(args[0].loc.expression, '_outer.merge(params: _inner)')
|
117
|
-
@source_rewriter.insert_after(node.loc.expression, "\n#{line_indent(node)}")
|
118
|
-
trim_enclosing_spaces!(node)
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
def in_a_block_with_only_whitespace?(node)
|
124
|
-
return false unless node.parent && node.parent.block_type?
|
125
|
-
content_before = @content[node.parent.loc.begin.end_pos...node.loc.expression.begin_pos]
|
126
|
-
content_after = @content[node.loc.expression.end_pos...node.parent.loc.end.begin_pos]
|
127
|
-
content_before =~ /^\s*$/ && content_after =~ /^\s*$/
|
128
|
-
end
|
129
|
-
|
130
|
-
def trim_enclosing_spaces!(node)
|
131
|
-
before_range = Parser::Source::Range.new(@source_buffer, node.parent.loc.begin.end_pos, node.loc.expression.begin_pos)
|
132
|
-
after_range = Parser::Source::Range.new(@source_buffer, node.loc.expression.end_pos, node.parent.loc.end.begin_pos)
|
133
|
-
@source_rewriter.remove(before_range)
|
134
|
-
@source_rewriter.remove(after_range)
|
135
|
-
end
|
136
|
-
|
137
76
|
def looks_like_route_definition?(hash_node)
|
138
77
|
keys = hash_node.children.map { |pair| pair.children[0].children[0] }
|
139
78
|
route_definition_keys = [:to, :controller]
|
@@ -179,12 +118,6 @@ module Rails5
|
|
179
118
|
@source_rewriter.replace(node_loc, "#{key}: #{node_source}")
|
180
119
|
end
|
181
120
|
|
182
|
-
def warn_about_ambiguous_params(node)
|
183
|
-
log "Ambiguous params found"
|
184
|
-
log "#{@options.file_path}:#{node.loc.line}" if @options.file_path
|
185
|
-
log "```\n#{node.loc.expression.source}\n```\n\n"
|
186
|
-
end
|
187
|
-
|
188
121
|
def line_indent(node)
|
189
122
|
node.loc.expression.source_line.match(/^(\s*)/)[1]
|
190
123
|
end
|
@@ -1,26 +1,12 @@
|
|
1
1
|
class TextTransformerOptions
|
2
|
-
attr_accessor :
|
3
|
-
attr_writer :quiet
|
2
|
+
attr_accessor :hash_spacing, :indent, :file_path
|
3
|
+
attr_writer :quiet
|
4
4
|
|
5
5
|
def initialize
|
6
6
|
@file_path = nil
|
7
|
-
@strategy = :optimistic
|
8
7
|
@quiet = false
|
9
8
|
@indent = ' '
|
10
9
|
@hash_spacing = nil
|
11
|
-
@warn_if_ambiguous = false
|
12
|
-
end
|
13
|
-
|
14
|
-
def wrap_ambiguous_params?
|
15
|
-
@strategy == :optimistic
|
16
|
-
end
|
17
|
-
|
18
|
-
def uglify_ambiguous_params?
|
19
|
-
@strategy == :uglify
|
20
|
-
end
|
21
|
-
|
22
|
-
def warn_about_ambiguous_params?
|
23
|
-
@warn_if_ambiguous
|
24
10
|
end
|
25
11
|
|
26
12
|
def quiet?
|
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:
|
4
|
+
version: 2.0.0
|
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-10-
|
11
|
+
date: 2016-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parser
|