flog 4.9.4 → 4.10.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
  SHA256:
3
- metadata.gz: 9047c8b22acece9cd9e5c1dbb2e44d486fd903c40bd80967d1e4c257fa8669a9
4
- data.tar.gz: 69a903441dcc41ab504399e173f8d231a242869ffbc5b6aebfa74e758822c4af
3
+ metadata.gz: 047dd2034a9a7363af7474e2ec42c541eb08a8460ef065da5ec9cc7ba295e903
4
+ data.tar.gz: 252c85c2e6c942a45cfc8b199c50f1102730cc6f97a3761d8a1f98599c64b78f
5
5
  SHA512:
6
- metadata.gz: 88c867edf9e72b5b1f392697f3de483dfd18234a620028eeb6a60527b5dbdc21e994afb103870fa2d79fd4d88a91301a35fc37c0d095b330d1469be72595b879
7
- data.tar.gz: f57466932cb71074ae085276bb9dbc9f47e684f4da599ce52a33d2a94198d2e9d86b22a41094bf2f65f6a8afa3a3c4be369ded47f970bcaad21fe82d9e334dbd
6
+ metadata.gz: 16a8677d306e6a716df1009600aef984fedf97edb8273b142d467464cac4ac0c951654fda938a975469e250ba6779dce9c73bad18117c41f110a610e8f671957
7
+ data.tar.gz: 458fcfd0cb26ca5adb5ea05fd3b2ae83a1fa90343a9248fe1141923acd506caf45de846a5e125540a37b5dcde0bb61202b4a157fd0add02d9ed0b0d2d546a039
checksums.yaml.gz.sig CHANGED
Binary file
data/History.rdoc CHANGED
@@ -1,3 +1,17 @@
1
+ === 4.10.0 / 2026-11-25
2
+
3
+ * 1 minor enhancement:
4
+
5
+ * Added --timeout=<n> flag. (olbrich)
6
+
7
+ * 5 bug fixes:
8
+
9
+ * Bump prism version to include 2.x.
10
+ * Fix #penalize_by to avoid round-off errors processing deeply nested code. (ThaSMorato)
11
+ * Fixed lvar processing for _1, _2, etc.
12
+ * Fixed processing of absolutely bonkers `call(&obj&.method)` construct.
13
+ * Fixed scoring of const assignments to be counted as a namespace. (chrobo)
14
+
1
15
  === 4.9.4 / 2026-01-06
2
16
 
3
17
  * 2 bug fixes:
data/README.rdoc CHANGED
@@ -29,10 +29,16 @@ report. The higher the score, the more pain the code is in.
29
29
  53.7: Flog#parse_options
30
30
  ...
31
31
 
32
+ == AI Policy:
33
+
34
+ AI is not allowed. Not in PRs, not in issues... just NO. See {my AI
35
+ policy}[https://zenspider.com/ruby/2026/07/my-ai-policy.html] for more
36
+ details. If it "smells of AI", it will be rejected.
37
+
32
38
  == REQUIREMENTS:
33
39
 
34
40
  * ruby2ruby (soft dependency: only for -v)
35
- * ruby_parser
41
+ * prism (can also still use ruby_parser with --legacy, but why??)
36
42
  * path_expander
37
43
 
38
44
  == INSTALL:
data/Rakefile CHANGED
@@ -22,7 +22,7 @@ Hoe.spec 'flog' do
22
22
  license "MIT"
23
23
 
24
24
  dependency "sexp_processor", "~> 4.8"
25
- dependency "prism", "~> 1.7"
25
+ dependency "prism", [">= 1.9", "< 3"]
26
26
  dependency "path_expander", "~> 2.0"
27
27
  end
28
28
 
data/lib/flog.rb CHANGED
@@ -9,6 +9,10 @@ class Prism::Translation::RubyParser
9
9
  def visit_it_local_variable_read_node(node) # TODO: upstream
10
10
  s(node, :lvar, :it)
11
11
  end
12
+
13
+ def visit_local_variable_read_node(node)
14
+ s(node, :lvar, node.name)
15
+ end
12
16
  end
13
17
  Compiler.prepend TweakIt
14
18
  end
@@ -23,7 +27,7 @@ end
23
27
  # thoroughly test.
24
28
 
25
29
  class Flog < MethodBasedSexpProcessor
26
- VERSION = "4.9.4" # :nodoc:
30
+ VERSION = "4.10.0" # :nodoc:
27
31
 
28
32
  NotRubyParser = Class.new Prism::Translation::RubyParser # compatibility layer :nodoc:
29
33
 
@@ -182,7 +186,7 @@ class Flog < MethodBasedSexpProcessor
182
186
 
183
187
  ruby = file == "-" ? $stdin.read : File.binread(file)
184
188
 
185
- flog_ruby ruby, file
189
+ flog_ruby ruby, file, option[:timeout]
186
190
  end
187
191
 
188
192
  calculate_total_scores
@@ -262,14 +266,14 @@ class Flog < MethodBasedSexpProcessor
262
266
 
263
267
  ##
264
268
  # For the duration of the block the complexity factor is increased
265
- # by #bonus This allows the complexity of sub-expressions to be
266
- # influenced by the expressions in which they are found. Yields 42
267
- # to the supplied block.
269
+ # by #bonus. This allows the complexity of sub-expressions to be
270
+ # influenced by the expressions in which they are found.
268
271
 
269
272
  def penalize_by bonus
273
+ old = @multiplier
270
274
  @multiplier += bonus
271
275
  yield
272
- @multiplier -= bonus
276
+ @multiplier = old
273
277
  end
274
278
 
275
279
  ##
@@ -291,9 +295,9 @@ class Flog < MethodBasedSexpProcessor
291
295
  a, b, c = 0, 0, 0
292
296
  tally.each do |cat, score|
293
297
  case cat
294
- when :assignment then a += score
298
+ when :assignment then a += score
295
299
  when :branch, :block_call then b += score
296
- else c += score
300
+ else c += score
297
301
  end
298
302
  end
299
303
  Math.sqrt(a*a + b*b + c*c)
@@ -379,12 +383,13 @@ class Flog < MethodBasedSexpProcessor
379
383
  # do nothing -- I don't like the indirection, but that's already scored
380
384
  when :lasgn then # f(&l=b)
381
385
  add_to_score :to_proc_lasgn
382
- when :iter, :dsym, :dstr, :hash, *BRANCHING then # below
386
+ when :iter, :dsym, :dstr, :hash, :safe_call, *BRANCHING then # below
383
387
  # f(&proc { ... })
384
388
  # f(&"#{...}")
385
389
  # f(&:"#{...}")
386
390
  # f(&if ... then ... end") and all other branching forms
387
391
  # f(&{ a: 42 }) WHY?!?
392
+ # f(&obj&.meth) WHY?!?
388
393
  add_to_score :to_proc_icky!
389
394
  else
390
395
  raise({:block_pass_even_ickier! => arg}.inspect)
@@ -436,6 +441,23 @@ class Flog < MethodBasedSexpProcessor
436
441
  s()
437
442
  end
438
443
 
444
+ def process_cdecl exp
445
+ name = exp.shift
446
+ val = exp.shift
447
+
448
+ if val.sexp_type == :iter then # eg X = Class.new do ... end
449
+ in_klass name do
450
+ add_to_score :assignment # inside in_klass keeps distance correct
451
+ process val
452
+ end
453
+ else
454
+ add_to_score :assignment
455
+ process val
456
+ end
457
+
458
+ s()
459
+ end
460
+
439
461
  def process_class(exp)
440
462
  super do
441
463
  penalize_by 1.0 do
data/lib/flog_cli.rb CHANGED
@@ -78,6 +78,7 @@ class FlogCLI
78
78
  :quiet => false,
79
79
  :continue => false,
80
80
  :parser => nil,
81
+ :timeout => 10,
81
82
  }.merge extra_options
82
83
 
83
84
  OptionParser.new do |opts|
@@ -143,6 +144,10 @@ class FlogCLI
143
144
  option[:verbose] = true
144
145
  end
145
146
 
147
+ opts.on("--timeout=N", Float, "Set the timeout (in sec) for parsing (default: 10s)") do |n|
148
+ option[:timeout] = n
149
+ end
150
+
146
151
  next if self.plugins.empty?
147
152
  opts.separator "Plugin options:"
148
153
 
data/test/test_flog.rb CHANGED
@@ -3,6 +3,21 @@ require "flog"
3
3
 
4
4
  class Flog
5
5
  attr_writer :calls
6
+
7
+ class NotRubyParser # TODO: remove once upstreamed
8
+ attr_accessor :scopes
9
+
10
+ def initialize scopes:nil
11
+ super()
12
+ self.scopes = [scopes] if scopes
13
+ end
14
+
15
+ # overridden from prism to add scopes arg
16
+ def parse(source, filepath = "(string)")
17
+ translate(Prism.parse(source, filepath:, partial_script: true, scopes:), filepath)
18
+ end
19
+ end
20
+
6
21
  end
7
22
 
8
23
  class FlogTest < Minitest::Test
@@ -88,6 +103,81 @@ class TestFlog < FlogTest
88
103
  assert_in_epsilon 1, @flog.multiplier
89
104
  end
90
105
 
106
+ def test_penalize_by__underflow_does_not_change_score
107
+ assert_equal 1.0, @flog.multiplier # floats=immediates, so equal will work
108
+
109
+ n = 1000
110
+ compound_penalty = -> {
111
+ @flog.penalize_by 0.1 do
112
+ n -= 1
113
+ if n.zero? then
114
+ assert_in_epsilon 101, @flog.multiplier
115
+ else
116
+ compound_penalty.call
117
+ end
118
+ end
119
+ }
120
+
121
+ compound_penalty.call
122
+
123
+ assert_equal 1.0, @flog.multiplier
124
+ end
125
+
126
+ def test_process__iter_lasgn
127
+ # x = Hash.new { |h, k| h[k] = 1.0 }
128
+ sexp = s(:lasgn, :x,
129
+ s(:iter, s(:call, s(:const, :Hash), :new), s(:args, :h, :k),
130
+ s(:attrasgn, s(:lvar, :h), :[]=, s(:lvar, :k), s(:lit, 1.0))))
131
+
132
+ assert_process(sexp, 2.65,
133
+ block_call: 1.0, new: 1.0, assignment: 2.1, magic_number: 0.275)
134
+ end
135
+
136
+ def test_process__iter_cdecl
137
+ # X = Hash.new { |h, k| h[k] = 1.0 }
138
+ sexp = s(:cdecl, :X,
139
+ s(:iter, s(:call, s(:const, :Hash), :new), s(:args, :h, :k),
140
+ s(:attrasgn, s(:lvar, :h), :[]=, s(:lvar, :k), s(:lit, 1.0))))
141
+
142
+ @klass = "X"
143
+ assert_process(sexp, 2.65,
144
+ block_call: 1.0, new: 1.0, assignment: 2.1, magic_number: 0.275)
145
+ end
146
+
147
+ def test_process__iter_cdecl_methods
148
+ ruby = <<~RUBY
149
+ module A
150
+ B = Data.define(:b) do
151
+ def to_s = ""
152
+ end
153
+
154
+ C = Struct.new(:c) do
155
+ def to_s = ""
156
+ end
157
+ end
158
+ RUBY
159
+
160
+ sexp = s(:module, :A,
161
+ s(:cdecl, :B,
162
+ s(:iter, s(:call, s(:const, :Data), :define, s(:lit, :b)), 0,
163
+ s(:defn, :to_s, s(:args), s(:str, "")))),
164
+ s(:cdecl, :C,
165
+ s(:iter, s(:call, s(:const, :Struct), :new, s(:lit, :c)), 0,
166
+ s(:defn, :to_s, s(:args), s(:str, "")))))
167
+
168
+ sexp = assert_parse sexp, ruby # now with line numbers!
169
+
170
+ assert_process(sexp, 3.464,
171
+ "A::B#none" => {assignment: 1.0, block_call: 1.0, define: 1.0},
172
+ "A::C#none" => {assignment: 1.0, block_call: 1.0, new: 1.0})
173
+
174
+ exp = {
175
+ "A::B#to_s" => "file.rb:3-3",
176
+ "A::C#to_s" => "file.rb:7-7",
177
+ }
178
+ assert_equal exp, @flog.method_locations
179
+ end
180
+
91
181
  def test_process_alias
92
182
  sexp = s(:alias, s(:lit, :a), s(:lit, :b))
93
183
 
@@ -217,6 +307,42 @@ class TestFlog < FlogTest
217
307
  assert_process sexp, 3.6, :a => 1.4, :b => 1.2, :c => 1.0
218
308
  end
219
309
 
310
+ def test_process_call__block_arg # a(&b)
311
+ ruby = "a(&b)"
312
+ sexp = s(:call, nil, :a, s(:block_pass, s(:lvar, :b)))
313
+
314
+ assert_parse sexp, ruby
315
+
316
+ assert_process(sexp, 2.2,
317
+ :a => 1,
318
+ :block_pass => 1.2)
319
+ end
320
+
321
+ def test_process_call__block_arg_call # a(&b.c)
322
+ ruby = "a(&b.c)"
323
+ sexp = s(:call, nil, :a, s(:block_pass, s(:call, s(:lvar, :b), :c)))
324
+
325
+ assert_parse sexp, ruby
326
+
327
+ assert_process(sexp, 3.4,
328
+ :a => 1,
329
+ :c => 1.2,
330
+ :block_pass => 1.2)
331
+ end
332
+
333
+ def test_process_call__block_arg_safe_call # a(&b&.c)
334
+ ruby = "a(&b&.c)"
335
+ sexp = s(:call, nil, :a, s(:block_pass, s(:safe_call, s(:lvar, :b), :c)))
336
+
337
+ assert_parse sexp, ruby
338
+
339
+ assert_process(sexp, 15.40,
340
+ :a => 1,
341
+ :block_pass => 1.2,
342
+ :c => 1.2,
343
+ :to_proc_icky! => 12)
344
+ end
345
+
220
346
  def test_process_safe_call2
221
347
  sexp = s(:safe_call, s(:call, nil, :a), :b) # a&.b
222
348
  assert_process sexp, 2.3, :a => 1.3, :b => 1.0
@@ -345,6 +471,8 @@ class TestFlog < FlogTest
345
471
  end
346
472
 
347
473
  def test_process_iter__it
474
+ skip "n/a" if RUBY_VERSION < "3.4"
475
+
348
476
  ruby = "blah { puts it }"
349
477
  sexp = s(:iter,
350
478
  s(:call, nil, :blah), 0,
@@ -355,6 +483,17 @@ class TestFlog < FlogTest
355
483
  assert_process sexp, 2.326, blah: 1, block_call: 1, puts: 1.1
356
484
  end
357
485
 
486
+ def test_process_iter__lvar__1
487
+ ruby = "blah { puts _1 }"
488
+ sexp = s(:iter,
489
+ s(:call, nil, :blah), 0,
490
+ s(:call, nil, :puts, s(:lvar, :_1)))
491
+
492
+ assert_parse sexp, ruby
493
+
494
+ assert_process sexp, 2.326, blah: 1, block_call: 1, puts: 1.1
495
+ end
496
+
358
497
  def test_process_iter_dsl
359
498
  # task :blah do
360
499
  # something
@@ -462,7 +601,7 @@ class TestFlog < FlogTest
462
601
 
463
602
  def test_process_lit_int__const
464
603
  sexp = s(:cdecl, :X, s(:lit, 42))
465
- assert_process sexp, 0.0
604
+ assert_process sexp, 1.0
466
605
  end
467
606
 
468
607
  def test_process_lit_float # and other lits
@@ -472,7 +611,7 @@ class TestFlog < FlogTest
472
611
 
473
612
  def test_process_lit_float__const
474
613
  sexp = s(:cdecl, :X, s(:lit, 3.1415))
475
- assert_process sexp, 0.0
614
+ assert_process sexp, 1.0
476
615
  end
477
616
 
478
617
  def test_process_lit_complex
@@ -575,7 +714,7 @@ class TestFlog < FlogTest
575
714
  end
576
715
 
577
716
  def assert_hash_in_epsilon exp, act
578
- assert_equal exp.keys.sort_by(&:to_s), act.keys.sort_by(&:to_s)
717
+ assert_equal exp.keys.sort, act.keys.sort
579
718
 
580
719
  exp.keys.each do |k|
581
720
  assert_in_epsilon exp[k], act[k], 0.001, "key = #{k.inspect}"
@@ -583,7 +722,11 @@ class TestFlog < FlogTest
583
722
  end
584
723
 
585
724
  def assert_parse sexp, ruby
586
- assert_equal sexp, Flog::NotRubyParser.new.process(ruby)
725
+ parser = Flog::NotRubyParser.new scopes: %i[a b c d]
726
+
727
+ act = parser.process ruby, "file.rb"
728
+ assert_equal sexp, act
729
+ act
587
730
  end
588
731
 
589
732
  def assert_process sexp, score = -1, hash = {}
@@ -597,8 +740,10 @@ class TestFlog < FlogTest
597
740
  key = "#{@klass}#{@meth}"
598
741
  act = @flog.calls
599
742
 
600
- assert_equal [key], act.keys.sort
601
- assert_hash_in_epsilon hash, act[key]
743
+ # cheat?
744
+ hash = { key => hash } if hash.keys.all? { |x| Symbol === x }
745
+
746
+ assert_equal hash, act # also cheat: needs recursive assert_hash_in_epsilon
602
747
  end
603
748
 
604
749
  @flog.calculate_total_scores
@@ -39,6 +39,7 @@ class TestFlogCLI < FlogTest
39
39
  "--score" => :score,
40
40
  "-t" => [:threshold, "75", 0.75],
41
41
  "--threshold" => [:threshold, "75", 0.75],
42
+ "--timeout" => [:timeout, "1.5", 1.5],
42
43
  "-v" => :verbose,
43
44
  "--verbose" => :verbose,
44
45
  # TODO: (maybe)
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flog
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.9.4
4
+ version: 4.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Davis
@@ -48,16 +48,22 @@ dependencies:
48
48
  name: prism
49
49
  requirement: !ruby/object:Gem::Requirement
50
50
  requirements:
51
- - - "~>"
51
+ - - ">="
52
52
  - !ruby/object:Gem::Version
53
- version: '1.7'
53
+ version: '1.9'
54
+ - - "<"
55
+ - !ruby/object:Gem::Version
56
+ version: '3'
54
57
  type: :runtime
55
58
  prerelease: false
56
59
  version_requirements: !ruby/object:Gem::Requirement
57
60
  requirements:
58
- - - "~>"
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '1.9'
64
+ - - "<"
59
65
  - !ruby/object:Gem::Version
60
- version: '1.7'
66
+ version: '3'
61
67
  - !ruby/object:Gem::Dependency
62
68
  name: path_expander
63
69
  requirement: !ruby/object:Gem::Requirement
@@ -78,37 +84,45 @@ dependencies:
78
84
  requirements:
79
85
  - - ">="
80
86
  - !ruby/object:Gem::Version
81
- version: '6.0'
87
+ version: '7.0'
82
88
  - - "<"
83
89
  - !ruby/object:Gem::Version
84
- version: '8'
90
+ version: '9'
85
91
  type: :development
86
92
  prerelease: false
87
93
  version_requirements: !ruby/object:Gem::Requirement
88
94
  requirements:
89
95
  - - ">="
90
96
  - !ruby/object:Gem::Version
91
- version: '6.0'
97
+ version: '7.0'
92
98
  - - "<"
93
99
  - !ruby/object:Gem::Version
94
- version: '8'
100
+ version: '9'
95
101
  - !ruby/object:Gem::Dependency
96
102
  name: hoe
97
103
  requirement: !ruby/object:Gem::Requirement
98
104
  requirements:
99
105
  - - "~>"
100
106
  - !ruby/object:Gem::Version
101
- version: '4.5'
107
+ version: '4.7'
102
108
  type: :development
103
109
  prerelease: false
104
110
  version_requirements: !ruby/object:Gem::Requirement
105
111
  requirements:
106
112
  - - "~>"
107
113
  - !ruby/object:Gem::Version
108
- version: '4.5'
114
+ version: '4.7'
109
115
  description: |-
110
116
  Flog reports the most tortured code in an easy to read pain
111
117
  report. The higher the score, the more pain the code is in.
118
+
119
+ == Features/Problems:
120
+
121
+ * Easy to read reporting of complexity/pain.
122
+ * Uses path_expander, so you can use:
123
+ * dir_arg -- expand a directory automatically
124
+ * @file_of_args -- persist arguments in a file
125
+ * -path_to_subtract -- ignore intersecting subsets of files/directories
112
126
  email:
113
127
  - ryand-ruby@zenspider.com
114
128
  executables:
@@ -116,7 +130,6 @@ executables:
116
130
  extensions: []
117
131
  extra_rdoc_files:
118
132
  - History.rdoc
119
- - Manifest.txt
120
133
  - README.rdoc
121
134
  files:
122
135
  - ".autotest"
@@ -137,6 +150,7 @@ licenses:
137
150
  metadata:
138
151
  homepage_uri: http://ruby.sadi.st/
139
152
  source_code_uri: https://github.com/seattlerb/flog
153
+ documentation_uri: http://docs.seattlerb.org/flog
140
154
  rdoc_options:
141
155
  - "--main"
142
156
  - README.rdoc
metadata.gz.sig CHANGED
Binary file