dev_scripts 0.1.9 → 0.2.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: 84e9684ea52274a30eebbd1dd2156bdf7a6e92ad
4
- data.tar.gz: 4689e7a90e727dcc6f04a21b6deff66b5ceeee8e
3
+ metadata.gz: 9e2d6231dc1af37f14563a2d5f8403e20b3d52c2
4
+ data.tar.gz: 6e046412172954212c6df5560dcee90c4c8342ab
5
5
  SHA512:
6
- metadata.gz: 502954069332523e028f5ff6c1f0d442e39984ad9253e5801d6861d3bb17eabeb96e7e204fb20f2af4dee0bd74c9ccbe2f85fd23e7fe3921289a72d743305005
7
- data.tar.gz: 6f18af2b0987e59b47e0a77ba816ef4b0f053011642bb8fa3dc00a935d5ef56675deb68280316e4b3d19ec0a1311ed2e804631256fb884c700d2a5466b254ad8
6
+ metadata.gz: 5bd41ce5bbe8db70ac8274226bf9301152674a9d441fc4aed3b424c4de75af773ee1253a616ddd8c8e8e68e6157d7ebfdb65d0097177c07f1a51aaa51ff21e9d
7
+ data.tar.gz: 146e7ac335608f6c0d37c37eb4cff2978a059610e186cd26e8ebbbf0b4119a40be1bd85d06643ae03339f2d011bcb4d9a1583e97eaddca12090f19947c03a5c9
data/.vscode/tasks.json CHANGED
@@ -12,6 +12,12 @@
12
12
  "type": "shell",
13
13
  "command": "dev_scripts expand_block ${relativeFile} ${lineNumber}",
14
14
  "problemMatcher": []
15
+ },
16
+ {
17
+ "label": "expand method arguments",
18
+ "type": "shell",
19
+ "command": "dev_scripts expand_method_args ${relativeFile} ${lineNumber}",
20
+ "problemMatcher": []
15
21
  }
16
22
  ]
17
23
  }
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dev_scripts (0.1.9)
4
+ dev_scripts (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/lib/dev_scripts.rb CHANGED
@@ -7,6 +7,7 @@ end
7
7
  require 'dev_scripts/scripts/open_spec_file'
8
8
  require 'dev_scripts/scripts/rubocop_metrics_method_length'
9
9
  require 'dev_scripts/scripts/expand_block'
10
+ require 'dev_scripts/scripts/expand_method_args'
10
11
 
11
12
  if File.exist? './dev_scripts'
12
13
  Dir['./dev_scripts/**/*.rb'].each do |file|
@@ -0,0 +1,23 @@
1
+ require 'dev_scripts/script'
2
+ require 'dev_scripts/support/expanded_method'
3
+ require 'parser/current'
4
+
5
+ DevScripts::Script.define_script :expand_method_args do
6
+ args :file_path, :line_number
7
+
8
+ execute do
9
+ lines = []
10
+
11
+ File.foreach(file_path).with_index do |file_line, index|
12
+ if index + 1 == line_number.to_i
13
+ lines << DevScripts::Support::ExpandedMethod.new(
14
+ ast_node: Parser::CurrentRuby.parse(file_line)
15
+ )
16
+ else
17
+ lines << file_line
18
+ end
19
+ end
20
+
21
+ File.open(file_path, 'w') { |file| file.write(lines.join('')) }
22
+ end
23
+ end
@@ -0,0 +1,121 @@
1
+ module DevScripts
2
+ module Support
3
+ class ExpandedMethod < String
4
+ INDENT = ' '.freeze
5
+ class NotAMethodError < StandardError; end
6
+
7
+ def initialize(ast_node:)
8
+ @type = ast_node.type
9
+ @children = ast_node.children
10
+
11
+ raise NotAMethodError unless [:send, :block].include?(type)
12
+
13
+ get_components
14
+
15
+ self << string_receiver
16
+
17
+ args.each_with_index do |arg, index|
18
+ if index == 0
19
+ self << '('
20
+ self << "\n"
21
+ end
22
+
23
+ self << INDENT + string_arg(arg)
24
+ self << "," unless index == args.length - 1
25
+ self << "\n"
26
+ self << ")" if index == args.length - 1
27
+ end
28
+
29
+ case type
30
+ when :block
31
+ self << ' do'
32
+ self << "\n"
33
+ self << INDENT + string_block_body
34
+ self << "\n"
35
+ self << 'end'
36
+ end
37
+
38
+ self << "\n"
39
+ end
40
+
41
+ private
42
+
43
+ attr_reader :receiver, :method_name, :args, :type, :children
44
+
45
+ def string_receiver
46
+ return method_name.to_s if receiver.nil?
47
+ end
48
+
49
+ def string_arg(arg)
50
+ case arg.type
51
+ when :send
52
+ FlattenedMethod.new(ast_node: arg)
53
+ when :sym
54
+ ":#{arg.children[-1]}"
55
+ when :str
56
+ "'#{arg.children[-1]}'"
57
+ when :block
58
+ FlattenedMethod.new(ast_node: arg)
59
+ when :hash
60
+ arg.children.each_with_object('').with_index do |(arg_child, string), index|
61
+ string << INDENT if index > 0
62
+
63
+ string << string_arg(arg_child)
64
+ string << "," if index < arg.children.size - 1
65
+ string << "\n" if index < arg.children.size - 1
66
+ end
67
+ when :pair
68
+ "#{arg.children[0].children[0]}: #{string_arg(arg.children[1])}"
69
+ when :nil
70
+ "nil"
71
+ end
72
+ end
73
+
74
+ def string_block_body
75
+ case children[-1].type
76
+ when :str
77
+ "'#{children[-1].children[-1]}'"
78
+ end
79
+ end
80
+
81
+ def get_components
82
+ case type
83
+ when :send
84
+ @receiver, @method_name, *@args = children
85
+ when :block
86
+ @receiver, @method_name, *@args = children.first.children
87
+ end
88
+ end
89
+ end
90
+
91
+ class FlattenedMethod < ExpandedMethod
92
+ def initialize(ast_node:)
93
+ @type = ast_node.type
94
+ @children = ast_node.children
95
+
96
+ raise NotAMethodError unless [:send, :block].include?(type)
97
+
98
+ get_components
99
+
100
+ self << string_receiver
101
+
102
+ args.each_with_index do |arg, index|
103
+ if index == 0
104
+ self << '('
105
+ end
106
+ self << string_arg(arg)
107
+ self << "," unless index == args.length - 1
108
+ self << ")" if index == args.length - 1
109
+ end
110
+
111
+ case type
112
+ when :block
113
+ self << ' { '
114
+ self << string_block_body
115
+ self << ' }'
116
+ end
117
+ end
118
+ end
119
+
120
+ end
121
+ end
@@ -1,3 +1,3 @@
1
1
  module DevScripts
2
- VERSION = "0.1.9"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dev_scripts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Quinones
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-04 00:00:00.000000000 Z
11
+ date: 2019-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -160,10 +160,12 @@ files:
160
160
  - lib/dev_scripts.rb
161
161
  - lib/dev_scripts/script.rb
162
162
  - lib/dev_scripts/scripts/expand_block.rb
163
+ - lib/dev_scripts/scripts/expand_method_args.rb
163
164
  - lib/dev_scripts/scripts/open_spec_file.rb
164
165
  - lib/dev_scripts/scripts/rubocop_metrics_method_length.rb
165
166
  - lib/dev_scripts/support/block.rb
166
167
  - lib/dev_scripts/support/expanded_block.rb
168
+ - lib/dev_scripts/support/expanded_method.rb
167
169
  - lib/dev_scripts/support/method_call.rb
168
170
  - lib/dev_scripts/version.rb
169
171
  homepage: