debride 1.11.0 → 1.12.0

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
  SHA256:
3
- metadata.gz: a3d7a09de612d4aa5772fbc3db3e26f51a06331c360c62d359acb8465f3f242a
4
- data.tar.gz: 95ce5aff93c1900456ad929a959d60f275feb036ecbb890edd590246d284c5eb
3
+ metadata.gz: e23117d21c55f9b3a52eb52b6bbd068167cb1b5852650359e62809ed8de15f1b
4
+ data.tar.gz: 231f1adaefd3cfb507d2352c72a7af1200e3db691d1617186f1cd4bc561e68e8
5
5
  SHA512:
6
- metadata.gz: b8c4f5575198e3d8de1c7486cf5b2a6e43e2f2d8ec61af07b3e6d8f5e101cd5343acb108a55468b1e19e8605c3c27acad32918084c4165a666f58e6d367fb72b
7
- data.tar.gz: 17dbd73820e02cea048e1cdc61d4811e3d00702f0d2fcc58098e56982205b80e1932cf6358d3692a7647da0de67251c2cab22e274598b420eae48cedeff94726
6
+ metadata.gz: bea8df45994be8ac31ddf803ec258fcc03054f2f2d3fbed6fd13124bf3c38125398c2f24e5de81e36460305c30b650c4bba2e0f7664854fb0a741085d4d2a138
7
+ data.tar.gz: d9465fbd7fd9404c7b5b381b7d98a40bed4da3de1f87ac39ae11bd13e25a6eb957380537de02f8bd41a6247629f5e6cfe0da975b4dff216850a68bb2d84051d5
checksums.yaml.gz.sig CHANGED
Binary file
data/History.rdoc CHANGED
@@ -1,3 +1,23 @@
1
+ === 1.12.0 / 2023-05-18
2
+
3
+ * 1 major enhancement:
4
+
5
+ * Massive overhaul of bin/debride_rm: faster, cleaner, can run a command between each deletion.
6
+
7
+ * 2 minor enhancements:
8
+
9
+ * Added alias_method and alias as pseudo-calls to source method.
10
+ * Whitelist extended/included/prepended etc by default.
11
+
12
+ * 6 bug fixes:
13
+
14
+ * Added missing rails validation.
15
+ * Bumped sexp_processor and ruby_parser dependencies.
16
+ * Fix --exclude <dir> to properly exclude whole tree.
17
+ * Fixed --exclude option to make it repeatable.
18
+ * Fixed bug on anonymous block forwarding (eg fn(&)). (afuno)
19
+ * Use RubyParser.new instead of RubyParser.for_current_ruby.
20
+
1
21
  === 1.11.0 / 2023-03-24
2
22
 
3
23
  * 6 minor enhancements:
data/README.rdoc CHANGED
@@ -42,7 +42,7 @@ API), then you can whitelist it:
42
42
  MyClass
43
43
  bad_method lib/some/file.rb:20
44
44
  ...
45
-
45
+
46
46
  Usage example for a typical rails application:
47
47
  # dump rake routes into a file
48
48
  % rake routes > routes.txt
@@ -53,13 +53,48 @@ Usage example for a typical rails application:
53
53
  % echo down >> whitelist.txt
54
54
  % echo change >> whitelist.txt
55
55
  # output debride report co standard output with the following options:
56
- # ignore typical rails methods,
56
+ # ignore typical rails methods,
57
57
  # specify generated whitelist,
58
58
  # run in current directory (".")
59
59
  % debride --rails --whitelist whitelist.txt .
60
60
 
61
61
  You can also use regexps in your whitelist by delimiting them with //'s.
62
62
 
63
+ To generate a whitelist for the last 28 days worth of logs on papertrail:
64
+
65
+ % seq 2 29 | xargs -I {} date -u -v-{}d +%Y-%m-%d | \
66
+ xargs -I {} curl --progress-bar -f --no-include -L -H "X-Papertrail-Token: $PAPERTRAIL_APIKEY" https://papertrailapp.com/api/v1/archives/{}/download | \
67
+ gzip -dc | grep production.log | cut -f 10- | \
68
+ debride_rails_whitelist routes.txt - | sort -u > whitelist.txt
69
+
70
+ == debride_rm
71
+
72
+ debride_rm will automatically remove dead code and optionally run a
73
+ command in between each removal. The command will automatically
74
+ substitute "NAME" and "PATH" with the name and path of the thing being
75
+ removed. Eg:
76
+
77
+ % debride_rm -C="git commit -m 'debride NAME in PATH' ." \
78
+ --rails \
79
+ --whitelist whitelist.txt \
80
+ --exclude test \
81
+ --exclude script \
82
+ --exclude bin \
83
+ --minimum 30
84
+
85
+ This command will:
86
+
87
+ 1. run with rails extensions on
88
+ 2. treat anything named in whitelist.txt as "called"
89
+ 3. exclude directories that shouldn't be scanned or mutated
90
+ 4. exclude anything under 30 lines long
91
+
92
+ One thing to note, debride_rm doesn't do terribly well with things
93
+ like attr_accessor lists, so running debride_rm with --minimum 2 (or
94
+ more) is wise.
95
+
96
+ Strategy: start large (40+) and work your way down.
97
+
63
98
  == PLUGINS:
64
99
 
65
100
  debride-erb :: Extends debride to analyze erb files (via erubis ala rails).
@@ -69,7 +104,7 @@ debride-slim :: Extends debride to analyze Slim files
69
104
 
70
105
  == EDITOR INTEGRATION:
71
106
 
72
- TextMate 2 :: * {Debride-Rails.tmbundle}[https://github.com/jjuliano/Debride-Rails.tmbundle] - Debride with Rails support
107
+ TextMate 2 :: * {Debride-Rails.tmbundle}[https://github.com/jjuliano/Debride-Rails.tmbundle] - Debride with Rails support
73
108
  * {Debride.tmbundle}[https://github.com/jjuliano/Debride-Rails.tmbundle] - Debride for Ruby
74
109
 
75
110
  == REQUIREMENTS:
data/Rakefile CHANGED
@@ -7,6 +7,7 @@ Hoe::add_include_dirs("../../sexp_processor/dev/lib",
7
7
  "../../ruby_parser/dev/lib",
8
8
  "../../ruby2ruby/dev/lib",
9
9
  "../../ZenTest/dev/lib",
10
+ "../../debride-erb/dev/lib",
10
11
  "../../path_expander/dev/lib",
11
12
  "lib")
12
13
 
@@ -19,8 +20,8 @@ Hoe.spec "debride" do
19
20
  developer "Ryan Davis", "ryand-ruby@zenspider.com"
20
21
  license "MIT"
21
22
 
22
- dependency "sexp_processor", "~> 4.5"
23
- dependency "ruby_parser", "~> 3.6"
23
+ dependency "sexp_processor", "~> 4.17"
24
+ dependency "ruby_parser", "~> 3.20"
24
25
  dependency "path_expander", "~> 1.0"
25
26
  end
26
27
 
@@ -28,7 +29,9 @@ def run dir, whitelist
28
29
  abort "Specify dir to scan with D=<path>" unless dir
29
30
 
30
31
  ENV["GEM_HOME"] = "tmp/isolate"
31
- ENV["GEM_PATH"] = "../../debride-erb/dev/tmp/isolate"
32
+ ENV["GEM_PATH"] = "#{Gem.paths.path.join ":"}:../../debride-erb/dev/tmp/isolate"
33
+
34
+ Gem.paths = ENV
32
35
 
33
36
  whitelist = whitelist && ["--whitelist", whitelist]
34
37
  verbose = ENV["V"] && "-v"
data/bin/debride_rm CHANGED
@@ -1,96 +1,59 @@
1
- #!/usr/bin/ruby -w
1
+ #!/usr/bin/env ruby -ws
2
2
 
3
- def autoclave nuke
4
- skips = 0
3
+ $C ||= false # command to run between deletions
5
4
 
6
- nuke.each do |path, ary|
7
- # warn path
5
+ require "set"
6
+ require_relative "../lib/debride"
8
7
 
9
- file = File.readlines path
8
+ def autoclave nuke, cmd
9
+ nuke.each do |path, lines_to_remove, klass, name|
10
+ warn "#{path} #{lines_to_remove} #{klass}##{name}"
10
11
 
11
- ary.each do |(line, name)|
12
- opener = file[line-1]
13
-
14
- case opener
15
- when /^\s*def/
16
- # do nothing
17
- else # attr_accessor, etc
18
- # warn " unsupported: #{opener.strip}"
19
- skips += 1
20
- next
21
- end
22
-
23
- leader = opener[/^\s+/]
24
- end_line = file[line..-1].find_index { |l| l =~ /^#{leader}end/ }
25
-
26
- if end_line then
27
- end_line += line + 1
28
- end_line += 1 while file[end_line] =~ /^\s*$/
29
- end_line += 1 while file[end_line] =~ /^\s*alias.*?\b#{name}$/
30
- end_line += 1 while file[end_line] =~ /^\s*$/
31
-
32
- end_line.downto line do |i|
33
- file.delete_at(i-1)
34
- end
35
- else
36
- warn "NOT FOUND: ending for #{name} #{path}:#{line}"
37
- end
38
- end
12
+ file = File.foreach(path).with_index.map { |l, i| [i+1, l] }
39
13
 
40
14
  File.open path, "w" do |f|
41
- f.write file.join
42
- end
43
- end
44
-
45
- print " skips = %3d" % skips if skips > 0
46
- end
47
-
48
- def read_debride path
49
- nuke = Hash.new { |h,k| h[k] = [] }
50
-
51
- count = 0
52
-
53
- File.foreach path do |line|
54
- case line
55
- when /^ (\S+)\s+(\S+):(\d+)$/ then
56
- name, path, line = $1, $2, $3.to_i
57
- nuke[path] << [line, name]
58
- count += 1
59
- when /^[\w:]+$/, "\n", /:$/ then
60
- # ignore
61
- else
62
- warn "unparsed: #{line.chomp}"
15
+ file.each do |idx, line|
16
+ # skip empty line after a removed line
17
+ next if line.chomp.empty? && lines_to_remove.include?(idx-1)
18
+ f.write line unless lines_to_remove.include? idx
19
+ end
63
20
  end
64
- end
65
21
 
66
- nuke.each do |k, ary|
67
- nuke[k] = ary.sort.reverse
22
+ `#{cmd.gsub(/\bNAME\b/, "#{klass}##{name}").gsub(/\bPATH\b/, path)}` if cmd
68
23
  end
69
-
70
- [nuke, count]
71
24
  end
72
25
 
73
26
  iter = 0
74
27
  old_count = nil
75
28
 
76
- abort "usage: #{$0} [debride args]+" if ARGV.empty? or ARGV.include? "-h"
77
-
78
- cmd = %w[debride] + ARGV + %w[> dummy_for_cmd_below]
29
+ abort "usage: #{$0} [-C=cmd] [debride args]+" if ARGV.empty? or ARGV.include? "-h"
79
30
 
80
31
  loop do
81
32
  iter += 1
82
- dead = "dead%02d.txt" % iter
83
- cmd[-1] = dead
84
33
 
85
- Process.wait Process.spawn cmd.join " "
86
- abort "debride failed: #{$?}" unless $?.success?
34
+ debride = Debride.run ARGV.dup
35
+
36
+ min = debride.option[:minimum] || 0
37
+
38
+ nuke = debride.missing_locations
39
+ .flat_map { |klass, meths|
40
+ meths
41
+ .reject { |(meth, loc)| !loc }
42
+ .map { |(meth, loc)|
43
+ path, start, finish = /^(.+):(\d+)(?:-(\d+))?$/.match(loc).captures
44
+ finish ||= start
45
+ [path, start.to_i..finish.to_i, klass, meth]
46
+ }
47
+ .reject { |p,r,k,m| r.size <= min }
48
+ }
49
+ .sort_by { |p,r,k,m| [p, -r.begin, m] }
87
50
 
88
- nuke, count = read_debride dead
51
+ count = nuke.size
89
52
 
90
- break if old_count == count
53
+ break if count.zero? || old_count == count
91
54
  old_count = count
92
55
 
93
- print "iter = %2d count = %4d" % [iter, count]
94
- autoclave nuke
56
+ puts "iter = %2d count = %4d" % [iter, count]
57
+ autoclave nuke, $C
95
58
  puts
96
59
  end
data/lib/debride.rb CHANGED
@@ -12,7 +12,7 @@ require "path_expander"
12
12
  # A static code analyzer that points out possible dead methods.
13
13
 
14
14
  class Debride < MethodBasedSexpProcessor
15
- VERSION = "1.11.0" # :nodoc:
15
+ VERSION = "1.12.0" # :nodoc:
16
16
  PROJECT = "debride"
17
17
 
18
18
  def self.load_plugins proj = PROJECT
@@ -56,7 +56,7 @@ class Debride < MethodBasedSexpProcessor
56
56
  expander = PathExpander.new(args, glob)
57
57
  files = expander.process
58
58
  excl = debride.option[:exclude]
59
- excl.map! { |fd| File.directory?(fd) ? "#{fd}/" : fd } if excl
59
+ excl.map! { |fd| File.directory?(fd) ? "#{fd}/**/*" : fd } if excl
60
60
 
61
61
  files = expander.filter_files files, StringIO.new(excl.join "\n") if excl
62
62
 
@@ -97,8 +97,7 @@ class Debride < MethodBasedSexpProcessor
97
97
  raise "Unhandled type: #{path_or_io.class}:#{path_or_io.inspect}"
98
98
  end
99
99
 
100
- rp = RubyParser.for_current_ruby rescue RubyParser.new
101
- rp.process(file, path, option[:timeout])
100
+ RubyParser.new.process(file, path, option[:timeout])
102
101
  rescue Racc::ParseError, RegexpError => e
103
102
  warn "Parse Error parsing #{path}. Skipping."
104
103
  warn " #{e.message}"
@@ -111,7 +110,15 @@ class Debride < MethodBasedSexpProcessor
111
110
 
112
111
  def self.parse_options args
113
112
  options = {
114
- :whitelist => [],
113
+ :whitelist => %i[
114
+ extended
115
+ included
116
+ inherited
117
+ method_added
118
+ method_missing
119
+ prepended
120
+ ],
121
+ :exclude => [],
115
122
  :format => :text,
116
123
  }
117
124
 
@@ -129,7 +136,7 @@ class Debride < MethodBasedSexpProcessor
129
136
  end
130
137
 
131
138
  opts.on("-e", "--exclude FILE1,FILE2,ETC", Array, "Exclude files or directories in comma-separated list.") do |list|
132
- options[:exclude] = list
139
+ options[:exclude].concat list
133
140
  end
134
141
 
135
142
  opts.on("-w", "--whitelist FILE", String, "Whitelist these messages.") do |s|
@@ -334,6 +341,17 @@ class Debride < MethodBasedSexpProcessor
334
341
  method_name = method_name.to_s.delete_suffix("_path").to_sym if option[:rails]
335
342
  when /^deliver_/ then
336
343
  method_name = method_name.to_s.delete_prefix("deliver_").to_sym if option[:rails]
344
+ when :alias_method then
345
+ _, _, _, lhs, rhs = sexp
346
+
347
+ if Sexp === lhs and Sexp === rhs then
348
+ lhs = lhs.last
349
+ rhs = rhs.last
350
+
351
+ record_method lhs, sexp.file, sexp.line
352
+
353
+ called << rhs
354
+ end
337
355
  end
338
356
 
339
357
  called << method_name
@@ -343,9 +361,21 @@ class Debride < MethodBasedSexpProcessor
343
361
  sexp
344
362
  end
345
363
 
364
+ def process_alias exp
365
+ _, (_, lhs), (_, rhs) = exp
366
+
367
+ record_method lhs, exp.file, exp.line
368
+
369
+ called << rhs
370
+
371
+ exp
372
+ end
373
+
346
374
  def process_block_pass exp # :nodoc:
347
375
  _, name = exp
348
376
 
377
+ return exp unless name
378
+
349
379
  case name.sexp_type
350
380
  when :lit then # eg &:to_sym
351
381
  called << name.last
@@ -576,7 +606,7 @@ class Debride < MethodBasedSexpProcessor
576
606
  :around_action,
577
607
  :before_action,
578
608
 
579
- # https://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
609
+ # https://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html (at bottom)
580
610
  :after_commit,
581
611
  :after_create,
582
612
  :after_destroy,
@@ -610,6 +640,7 @@ class Debride < MethodBasedSexpProcessor
610
640
  :validates,
611
641
  :validates_absence_of,
612
642
  :validates_acceptance_of,
643
+ :validates_comparison_of,
613
644
  :validates_confirmation_of,
614
645
  :validates_exclusion_of,
615
646
  :validates_format_of,
data/test/test_debride.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require "minitest/autorun"
2
2
  require "debride"
3
3
 
4
+ $: << "../../debride-erb/dev/tmp/isolate"
5
+
4
6
  class SafeDebride < Debride
5
7
  def self.abort s
6
8
  raise s
@@ -9,9 +11,9 @@ end
9
11
 
10
12
  class TestDebride < Minitest::Test
11
13
  EXP_LIST = [["Debride",
12
- [:process_attrasgn,
14
+ [:process_alias,
15
+ :process_attrasgn,
13
16
  :process_block_pass,
14
- :process_call,
15
17
  :process_cdecl,
16
18
  :process_colon2,
17
19
  :process_colon3,
@@ -20,6 +22,7 @@ class TestDebride < Minitest::Test
20
22
  :process_defs,
21
23
  :process_op_asgn2,
22
24
  :process_rb,
25
+ :process_safe_call,
23
26
  :report,
24
27
  :report_json,
25
28
  :report_text,
@@ -31,10 +34,14 @@ class TestDebride < Minitest::Test
31
34
 
32
35
  EXP_FORMATTED = { :missing => formatted_vals }
33
36
 
37
+ make_my_diffs_pretty!
38
+
34
39
  def assert_option arg, exp_arg, exp_opt
35
40
  opt = SafeDebride.parse_options arg
36
41
 
37
- exp_opt = {:whitelist => [], :format => :text}.merge exp_opt
42
+ wl = [:extended, :included, :inherited, :method_added, :method_missing, :prepended]
43
+
44
+ exp_opt = {:whitelist => wl, :exclude => [], :format => :text}.merge exp_opt
38
45
  assert_equal exp_opt, opt
39
46
  assert_equal exp_arg, arg
40
47
  end
@@ -143,7 +150,7 @@ class TestDebride < Minitest::Test
143
150
  debride.report(io)
144
151
 
145
152
  exp = JSON.load JSON.dump EXP_FORMATTED # force stringify
146
- data = JSON.load io.string.gsub(/\d+-\d+/, "###")
153
+ data = JSON.load io.string.gsub(/:\d+(-\d+)?/, ":###")
147
154
 
148
155
  assert_equal exp, data
149
156
  end
@@ -155,7 +162,7 @@ class TestDebride < Minitest::Test
155
162
  debride.report(io)
156
163
 
157
164
  exp = EXP_FORMATTED
158
- data = YAML.load io.string.gsub(/\d+-\d+/, "###")
165
+ data = YAML.load io.string.gsub(/:\d+(-\d+)?/, ":###")
159
166
 
160
167
  assert_equal exp, data
161
168
  end
@@ -334,6 +341,14 @@ class TestDebride < Minitest::Test
334
341
  assert_process [["Seattle", [:uncalled]]], ruby
335
342
  end
336
343
 
344
+ def test_block_pass_empty
345
+ ruby = <<-RUBY.strip
346
+ f(&) # block forwarding
347
+ RUBY
348
+
349
+ assert_process [], ruby
350
+ end
351
+
337
352
  def test_safe_navigation_operator
338
353
  ruby = <<-RUBY.strip
339
354
  class Seattle
@@ -500,8 +515,8 @@ class TestDebride < Minitest::Test
500
515
  "AttributeAccessor#w4=" => "(io):3",
501
516
  "AttributeAccessor#r1" => "(io):4",
502
517
  "AttributeAccessor#r2" => "(io):4",
503
- "AttributeAccessor#initialize" => "(io):5-9",
504
- "AttributeAccessor::class_method" => "(io):12-13"
518
+ "AttributeAccessor#initialize" => "(io):5-10",
519
+ "AttributeAccessor::class_method" => "(io):12-14"
505
520
  }
506
521
 
507
522
  assert_equal exp, d.method_locations
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: debride
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.11.0
4
+ version: 1.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Davis
@@ -29,7 +29,7 @@ cert_chain:
29
29
  ROfWo9Uyp8ba/j9eVG14KkYRaLydAY1MNQk2yd3R5CGfeOpD1kttxjoypoUJ2dOG
30
30
  nsNBRuQJ1UfiCG97a6DNm+Fr
31
31
  -----END CERTIFICATE-----
32
- date: 2023-03-24 00:00:00.000000000 Z
32
+ date: 2023-05-18 00:00:00.000000000 Z
33
33
  dependencies:
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: sexp_processor
@@ -37,28 +37,28 @@ dependencies:
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '4.5'
40
+ version: '4.17'
41
41
  type: :runtime
42
42
  prerelease: false
43
43
  version_requirements: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '4.5'
47
+ version: '4.17'
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: ruby_parser
50
50
  requirement: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '3.6'
54
+ version: '3.20'
55
55
  type: :runtime
56
56
  prerelease: false
57
57
  version_requirements: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '3.6'
61
+ version: '3.20'
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: path_expander
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -166,7 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
166
166
  - !ruby/object:Gem::Version
167
167
  version: '0'
168
168
  requirements: []
169
- rubygems_version: 3.4.6
169
+ rubygems_version: 3.4.10
170
170
  signing_key:
171
171
  specification_version: 4
172
172
  summary: Analyze code for potentially uncalled / dead methods, now with auto-removal.
metadata.gz.sig CHANGED
@@ -1,3 +1,2 @@
1
- �����=��BV��`L�*�tJcA�
2
- E��Q�v���!zK��
3
- ���F� `�|��A�Ev<
1
+ Z�&��x��À`�]r1��i�>f��� ��r��ê�He������/Sk�W���|��G�����ҍ�2(R:��(�
2
+ ��أ�y2"����A��Xz�� Z�\,z[�;@Xk���V��