ffast 0.2.0 → 0.2.3

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.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/release.yml +27 -0
  3. data/.github/workflows/ruby.yml +34 -0
  4. data/.gitignore +2 -0
  5. data/Fastfile +146 -3
  6. data/README.md +244 -132
  7. data/bin/console +6 -1
  8. data/bin/fast-experiment +3 -0
  9. data/bin/fast-mcp +7 -0
  10. data/fast.gemspec +24 -7
  11. data/lib/fast/cli.rb +129 -38
  12. data/lib/fast/experiment.rb +19 -2
  13. data/lib/fast/git.rb +1 -1
  14. data/lib/fast/mcp_server.rb +317 -0
  15. data/lib/fast/node.rb +258 -0
  16. data/lib/fast/prism_adapter.rb +310 -0
  17. data/lib/fast/rewriter.rb +64 -10
  18. data/lib/fast/scan.rb +203 -0
  19. data/lib/fast/shortcut.rb +23 -6
  20. data/lib/fast/source.rb +116 -0
  21. data/lib/fast/source_rewriter.rb +153 -0
  22. data/lib/fast/sql/rewriter.rb +98 -0
  23. data/lib/fast/sql.rb +165 -0
  24. data/lib/fast/summary.rb +435 -0
  25. data/lib/fast/version.rb +1 -1
  26. data/lib/fast.rb +165 -79
  27. data/mkdocs.yml +27 -3
  28. data/requirements-docs.txt +3 -0
  29. metadata +48 -62
  30. data/docs/command_line.md +0 -238
  31. data/docs/editors-integration.md +0 -46
  32. data/docs/experiments.md +0 -153
  33. data/docs/ideas.md +0 -80
  34. data/docs/index.md +0 -402
  35. data/docs/pry-integration.md +0 -27
  36. data/docs/research.md +0 -93
  37. data/docs/shortcuts.md +0 -323
  38. data/docs/similarity_tutorial.md +0 -176
  39. data/docs/syntax.md +0 -395
  40. data/docs/videos.md +0 -16
  41. data/examples/build_stubbed_and_let_it_be_experiment.rb +0 -51
  42. data/examples/experimental_replacement.rb +0 -46
  43. data/examples/find_usage.rb +0 -26
  44. data/examples/let_it_be_experiment.rb +0 -11
  45. data/examples/method_complexity.rb +0 -37
  46. data/examples/search_duplicated.rb +0 -15
  47. data/examples/similarity_research.rb +0 -58
  48. data/examples/simple_rewriter.rb +0 -6
  49. data/experiments/let_it_be_experiment.rb +0 -9
  50. data/experiments/remove_useless_hook.rb +0 -9
  51. data/experiments/replace_create_with_build_stubbed.rb +0 -10
data/docs/index.md DELETED
@@ -1,402 +0,0 @@
1
- # Fast
2
-
3
- Fast is a "Find AST" tool to help you search in the code abstract syntax tree.
4
-
5
- Ruby allow us to do the same thing in a few ways then it's hard to check
6
- how the code is written.
7
-
8
- Using the AST will be easier than try to cover the multiple ways we can write
9
- the same code.
10
-
11
- You can define a string like `%||` or `''` or `""` but they will have the same
12
- AST representation.
13
-
14
- ## AST representation
15
-
16
- Each detail of the ruby syntax have a equivalent identifier and some
17
- content. The content can be another expression or a final value.
18
-
19
- Fast uses parser gem behind the scenes to parse the code into nodes.
20
-
21
- First get familiar with parser gem and understand how ruby code is represented.
22
-
23
- When you install parser gem, you will have access to `ruby-parse` and you can
24
- use it with `-e` to parse an expression directly from the command line.
25
-
26
- Example:
27
-
28
- ```
29
- ruby-parse -e 1
30
- ```
31
-
32
- It will print the following output:
33
-
34
- ```
35
- (int 1)
36
- ```
37
-
38
- And trying a number with decimals:
39
-
40
- ```
41
- ruby-parse -e 1.1
42
- (float 1)
43
- ```
44
-
45
- Building a regex that will match decimals and integer looks like something easy
46
- and with fast you use a node pattern that reminds the syntax of regular
47
- expressions.
48
-
49
- ## Syntax for find in AST
50
-
51
- The current version cover the following elements:
52
-
53
- - `()` to represent a **node** search
54
- - `{}` is for **any** matches like **union** conditions with **or** operator
55
- - `[]` is for **all** matches like **intersect** conditions with **and** operator
56
- - `$` is for **capture** current expression
57
- - `_` is **something** not nil
58
- - `nil` matches exactly **nil**
59
- - `...` is a **node** with children
60
- - `^` is to get the **parent node** of an expression
61
- - `?` is for **maybe**
62
- - `\1` to use the first **previous captured** element
63
- - `""` surround the value with double quotes to match literal strings
64
-
65
- Jump to [Syntax](syntax.md).
66
-
67
- ## ast
68
-
69
- Use `Fast.ast` to convert simple code to AST objects. You can use it as
70
- `ruby-parse` but directly from the console.
71
-
72
- ```ruby
73
- Fast.ast("1") # => s(:int, 1)
74
- Fast.ast("method") # => s(:send, nil, :method)
75
- Fast.ast("a.b") # => s(:send, s(:send, nil, :a), :b)
76
- Fast.ast("1 + 1") # => s(:send, s(:int, 1), :+, s(:int, 1))
77
- Fast.ast("a = 2") # => s(:lvasgn, :a, s(:int, 2))
78
- Fast.ast("b += 2") # => s(:op_asgn, s(:lvasgn, :b), :+, s(:int, 2))
79
- ```
80
-
81
- It uses [astrolable](https://github.com/yujinakayama/astrolabe) gem behind the scenes:
82
-
83
- ```ruby
84
- Fast.ast(Fast.ast("1")).class
85
- => Astrolabe::Node
86
- Fast.ast(Fast.ast("1")).type
87
- => :int
88
- Fast.ast(Fast.ast("1")).children
89
- => [1]
90
- ```
91
-
92
- See also [ast_from_file](#ast_from_file).
93
-
94
- ## match?
95
-
96
- `Fast.match?` is the most granular function that tries to compare a node with an
97
- expression. It returns true or false and some node captures case it find
98
- something.
99
-
100
- Let's start with a simple integer in Ruby:
101
-
102
- ```ruby
103
- 1
104
- ```
105
-
106
- The AST can be represented with the following expression:
107
-
108
- ```
109
- (int 1)
110
- ```
111
-
112
- The ast representation holds node `type` and `children`.
113
-
114
- Let's build a method `s` to represent `Parser::AST::Node` with a `#type` and `#children`.
115
-
116
- ```ruby
117
- def s(type, *children)
118
- Parser::AST::Node.new(type, children)
119
- end
120
- ```
121
-
122
- A local variable assignment:
123
-
124
- ```ruby
125
- value = 42
126
- ```
127
-
128
- Can be represented with:
129
-
130
- ```ruby
131
- ast = s(:lvasgn, :value, s(:int, 42))
132
- ```
133
-
134
- Now, lets find local variable named `value` with an value `42`:
135
-
136
- ```ruby
137
- Fast.match?('(lvasgn value (int 42))', ast) # true
138
- ```
139
-
140
- Lets abstract a bit and allow some integer value using `_` as a shortcut:
141
-
142
- ```ruby
143
- Fast.match?('(lvasgn value (int _))', ast) # true
144
- ```
145
-
146
- Lets abstract more and allow float or integer:
147
-
148
- ```ruby
149
- Fast.match?('(lvasgn value ({float int} _))', ast) # true
150
- ```
151
-
152
- Or combine multiple assertions using `[]` to join conditions:
153
-
154
- ```ruby
155
- Fast.match?('(lvasgn value ([!str !hash !array] _))', ast) # true
156
- ```
157
-
158
- Matches all local variables not string **and** not hash **and** not array.
159
-
160
- We can match "a node with children" using `...`:
161
-
162
- ```ruby
163
- Fast.match?('(lvasgn value ...)', ast) # true
164
- ```
165
-
166
- You can use `$` to capture a node:
167
-
168
- ```ruby
169
- Fast.match?('(lvasgn value $...)', ast) # => [s(:int), 42]
170
- ```
171
-
172
- Or match whatever local variable assignment combining both `_` and `...`:
173
-
174
- ```ruby
175
- Fast.match?('(lvasgn _ ...)', ast) # true
176
- ```
177
-
178
- You can also use captures in any levels you want:
179
-
180
- ```ruby
181
- Fast.match?('(lvasgn $_ $...)', ast) # [:value, s(:int), 42]
182
- ```
183
-
184
- Keep in mind that `_` means something not nil and `...` means a node with
185
- children.
186
-
187
- Then, if do you get a method declared:
188
-
189
- ```ruby
190
- def my_method
191
- call_other_method
192
- end
193
- ```
194
- It will be represented with the following structure:
195
-
196
- ```ruby
197
- ast =
198
- s(:def, :my_method,
199
- s(:args),
200
- s(:send, nil, :call_other_method))
201
- ```
202
-
203
- Keep an eye on the node `(args)`.
204
-
205
- Then you know you can't use `...` but you can match with `(_)` to match with
206
- such case.
207
-
208
- Let's test a few other examples. You can go deeply with the arrays. Let's suppose we have a hardcore call to
209
- `a.b.c.d` and the following AST represents it:
210
-
211
- ```ruby
212
- ast =
213
- s(:send,
214
- s(:send,
215
- s(:send,
216
- s(:send, nil, :a),
217
- :b),
218
- :c),
219
- :d)
220
- ```
221
-
222
- You can search using sub-arrays with **pure values**, or **shortcuts** or
223
- **procs**:
224
-
225
- ```ruby
226
- Fast.match?([:send, [:send, '...'], :d], ast) # => true
227
- Fast.match?([:send, [:send, '...'], :c], ast) # => false
228
- Fast.match?([:send, [:send, [:send, '...'], :c], :d], ast) # => true
229
- ```
230
-
231
- Shortcuts like `...` and `_` are just literals for procs. Then you can use
232
- procs directly too:
233
-
234
- ```ruby
235
- Fast.match?([:send, [ -> (node) { node.type == :send }, [:send, '...'], :c], :d], ast) # => true
236
- ```
237
-
238
- And also work with expressions:
239
-
240
- ```ruby
241
- Fast.match?('(send (send (send (send nil $_) $_) $_) $_)', ast) # => [:a, :b, :c, :d]
242
- ```
243
-
244
- If something does not work you can debug with a block:
245
-
246
- ```ruby
247
- Fast.debug { Fast.match?([:int, 1], s(:int, 1)) }
248
- ```
249
-
250
- It will output each comparison to stdout:
251
-
252
- ```
253
- int == (int 1) # => true
254
- 1 == 1 # => true
255
- ```
256
-
257
- ## search
258
-
259
- Search allows you to go deeply in the AST, collecting nodes that matches with
260
- the expression. It also returns captures if they exist.
261
-
262
- ```ruby
263
- Fast.search('(int _)', Fast.ast('a = 1')) # => s(:int, 1)
264
- ```
265
-
266
- If you use captures, it returns the node and the captures respectively:
267
-
268
- ```ruby
269
- Fast.search('(int $_)', Fast.ast('a = 1')) # => [s(:int, 1), 1]
270
- ```
271
-
272
- You can also bind external parameters in the search using extra arguments:
273
- ```ruby
274
- Fast.search('(int %1)', Fast.ast('a = 1'), 1) # => [s(:int, 1)]
275
- ```
276
-
277
- ## capture
278
-
279
- To pick just the captures and ignore the nodes, use `Fast.capture`:
280
-
281
- ```ruby
282
- Fast.capture('(int $_)', Fast.ast('a = 1')) # => 1
283
- ```
284
- ## replace
285
-
286
- And if I want to refactor a code and use `delegate <attribute>, to: <object>`, try with replace:
287
-
288
- ```ruby
289
- Fast.replace '(def $_ ... (send (send nil $_) \1))', ast do |node, captures|
290
- attribute, object = captures
291
- replace(node.location.expression, "delegate :#{attribute}, to: :#{object}")
292
- end
293
- ```
294
-
295
- ## replace_file
296
-
297
- Now let's imagine we have real files like `sample.rb` with the following code:
298
-
299
- ```ruby
300
- def good_bye
301
- message = ["good", "bye"]
302
- puts message.join(' ')
303
- end
304
- ```
305
-
306
- And we decide to remove the `message` variable and put it inline with the `puts`.
307
-
308
- Basically, we need to find the local variable assignment, store the value in
309
- memory. Remove the assignment expression and use the value where the variable
310
- is being called.
311
-
312
- ```ruby
313
- assignment = nil
314
- Fast.replace_file('({ lvasgn lvar } message )','sample.rb') do |node, _|
315
- if node.type == :lvasgn
316
- assignment = node.children.last
317
- remove(node.location.expression)
318
- elsif node.type == :lvar
319
- replace(node.location.expression, assignment.location.expression.source)
320
- end
321
- end
322
- ```
323
-
324
- It will return an output of the new source code with the changes but not save
325
- the file. You can use ()[#rewrite_file] if you're confident about the changes.
326
-
327
- ## capture_file
328
-
329
- `Fast.capture_file` can be used to combine [capture](#capture) and file system.
330
-
331
- ```ruby
332
- Fast.capture_file("$(casgn)", "lib/fast/version.rb") # => s(:casgn, nil, :VERSION, s(:str, "0.1.3"))
333
- Fast.capture_file("(casgn nil _ (str $_))", "lib/fast/version.rb") # => "0.1.3"
334
- ```
335
-
336
- ## capture_all
337
-
338
- `Fast.capture_all` can be used to combine [capture_file](#capture_file) from multiple sources:
339
-
340
- ```ruby
341
- Fast.capture_all("(casgn nil $_)") # => { "./lib/fast/version.rb"=>:VERSION, "./lib/fast.rb"=>[:LITERAL, :TOKENIZER], ...}
342
- ```
343
-
344
- The second parameter can also be passed with to filter specific folders:
345
-
346
- ```ruby
347
- Fast.capture_all("(casgn nil $_)", "lib/fast") # => {"lib/fast/shortcut.rb"=>:LOOKUP_FAST_FILES_DIRECTORIES, "lib/fast/version.rb"=>:VERSION}
348
- ```
349
-
350
-
351
- ## rewrite_file
352
-
353
- `Fast.rewrite_file` works exactly as the `replace` but it will override the file
354
- from the input.
355
-
356
- ## ast_from_file
357
-
358
- This method parses the code and load into a AST representation.
359
-
360
- ```ruby
361
- Fast.ast_from_file('sample.rb')
362
- ```
363
-
364
- ## search_file
365
-
366
- You can use `search_file` and pass the path for search for expressions inside
367
- files.
368
-
369
- ```ruby
370
- Fast.search_file(expression, 'file.rb')
371
- ```
372
-
373
- It's simple combination of `Fast.ast_from_file` with `Fast.search`.
374
-
375
- ## ruby_files_from
376
-
377
- You'll be probably looking for multiple ruby files, then this method fetches
378
- all internal `.rb` files
379
-
380
- ```ruby
381
- Fast.ruby_files_from(['lib']) # => ["lib/fast.rb"]
382
- ```
383
-
384
- ## search_all
385
-
386
- Combines the [search_file](#search_file) with [ruby_files_from](#ruby_files_from)
387
- multiple locations and returns tuples with files and results.
388
-
389
- ```ruby
390
- Fast.search_all("(def ast_from_file)")
391
- => {"./lib/fast.rb"=>[s(:def, :ast_from_file,
392
- s(:args,
393
- s(:arg, :file)),
394
- s(:begin,
395
- ```
396
-
397
- You can also override the second param and pass target files or folders:
398
-
399
- ```ruby
400
- Fast.search_all("(def _)", '../other-folder')
401
- ```
402
-
@@ -1,27 +0,0 @@
1
-
2
- You can create a custom command in pry to reuse fast in any session.
3
-
4
- Start simply dropping it on your `.pryrc`:
5
-
6
- ```ruby
7
- Pry::Commands.block_command "fast", "Fast search" do |expression, file|
8
- require "fast"
9
- files = Fast.ruby_files_from(file || '.')
10
- files.each do |f|
11
- results = Fast.search_file(expression, f)
12
- next if results.nil? || results.empty?
13
- output.puts Fast.highlight("# #{f}")
14
-
15
- results.each do |result|
16
- output.puts Fast.highlight(result)
17
- end
18
- end
19
- end
20
- ```
21
-
22
- And use it in the console:
23
-
24
- ```pry
25
- fast '(def match?)' lib/fast.rb
26
- ```
27
-
data/docs/research.md DELETED
@@ -1,93 +0,0 @@
1
-
2
- # Research
3
-
4
- I love to research about codebase as data and prototyping ideas several times
5
- doesn't fit in simple [shortcuts](/shortcuts).
6
-
7
- Here is my first research that worth sharing:
8
-
9
- ## Combining Runtime metadata with AST complex searches
10
-
11
- This example covers how to find RSpec `allow` combined with `and_return` missing
12
- the `with` clause specifying the nested parameters.
13
-
14
- Here is the [gist](https://gist.github.com/jonatas/c1e580dcb74e20d4f2df4632ceb084ef)
15
- if you want to go straight and run it.
16
-
17
- Scenario for simple example:
18
-
19
- Given I have the following class:
20
-
21
- ```ruby
22
- class Account
23
- def withdraw(value)
24
- if @total >= value
25
- @total -= value
26
- :ok
27
- else
28
- :not_allowed
29
- end
30
- end
31
- end
32
- ```
33
-
34
- And I'm testing it with `allow` and some possibilities:
35
-
36
- ```ruby
37
- # bad
38
- allow(Account).to receive(:withdraw).and_return(:ok)
39
- # good
40
- allow(Account).to receive(:withdraw).with(100).and_return(:ok)
41
- ```
42
-
43
- **Objective:** find all bad cases of **any** class that does not respect the method
44
- parameters signature.
45
-
46
- First, let's understand the method signature of a method:
47
-
48
- ```ruby
49
- Account.instance_method(:withdraw).parameters
50
- # => [[:req, :value]]
51
- ```
52
-
53
- Now, we can build a small script to use the node pattern to match the proper
54
- specs that are using such pattern and later visit their method signatures.
55
-
56
-
57
- ```ruby
58
- Fast.class_eval do
59
- # Captures class and method name when find syntax like:
60
- # `allow(...).to receive(...)` that does not end with `.with(...)`
61
- pattern_with_captures = <<~FAST
62
- (send (send nil allow (const nil $_)) to
63
- (send (send nil receive (sym $_)) !with))
64
- FAST
65
-
66
- pattern = expression(pattern_with_captures.tr('$',''))
67
-
68
- ruby_files_from('spec').each do |file|
69
- results = search_file(pattern, file) || [] rescue next
70
- results.each do |n|
71
- clazz, method = capture(n, pattern_with_captures)
72
- if klazz = Object.const_get(clazz.to_s) rescue nil
73
- if klazz.respond_to?(method)
74
- params = klazz.method(method).parameters
75
- if params.any?{|e|e.first == :req}
76
- code = n.loc.expression
77
- range = [code.first_line, code.last_line].uniq.join(",")
78
- boom_message = "BOOM! #{clazz}.#{method} does not include the REQUIRED parameters!"
79
- puts boom_message, "#{file}:#{range}", code.source
80
- end
81
- end
82
- end
83
- end
84
- end
85
- end
86
- ```
87
-
88
- !!! hint "Preload your environment **before** run the script"
89
-
90
- Keep in mind that you should run it with your environment preloaded otherwise it
91
- will skip the classes.
92
- You can add elses for `const_get` and `respond_to` and report weird cases if
93
- your environment is not preloading properly.