path_coverage 0.3.0 → 0.5.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.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +316 -177
  3. data/exe/path_coverage_report +4 -0
  4. data/lib/path_coverage/call_trace.rb +88 -0
  5. data/lib/path_coverage/calls.rb +49 -43
  6. data/lib/path_coverage/card_context.rb +104 -0
  7. data/lib/path_coverage/card_forks.rb +44 -0
  8. data/lib/path_coverage/card_gaps.rb +125 -0
  9. data/lib/path_coverage/card_scopes.rb +37 -0
  10. data/lib/path_coverage/card_tree.rb +199 -0
  11. data/lib/path_coverage/card_way_lists.rb +72 -0
  12. data/lib/path_coverage/card_ways.rb +196 -0
  13. data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +38 -25
  14. data/lib/path_coverage/condition.rb +14 -14
  15. data/lib/path_coverage/configuration.rb +7 -4
  16. data/lib/path_coverage/configuration_limits.rb +52 -0
  17. data/lib/path_coverage/configuration_paths.rb +7 -7
  18. data/lib/path_coverage/console_summary.rb +24 -22
  19. data/lib/path_coverage/{site.rb → decision.rb} +2 -2
  20. data/lib/path_coverage/decision_locations.rb +76 -0
  21. data/lib/path_coverage/{site_paths.rb → decision_paths.rb} +18 -8
  22. data/lib/path_coverage/{site_tree.rb → decision_tree.rb} +25 -16
  23. data/lib/path_coverage/fold_branches.rb +18 -0
  24. data/lib/path_coverage/fold_cells.rb +42 -0
  25. data/lib/path_coverage/fold_rows.rb +140 -0
  26. data/lib/path_coverage/gaps.rb +33 -15
  27. data/lib/path_coverage/json_reader.rb +54 -19
  28. data/lib/path_coverage/json_reports.rb +102 -0
  29. data/lib/path_coverage/json_writer.rb +31 -16
  30. data/lib/path_coverage/kernel.rb +56 -57
  31. data/lib/path_coverage/kinds/base.rb +16 -16
  32. data/lib/path_coverage/kinds/block.rb +16 -15
  33. data/lib/path_coverage/kinds/case.rb +14 -17
  34. data/lib/path_coverage/kinds/chain.rb +40 -11
  35. data/lib/path_coverage/kinds/choice.rb +7 -10
  36. data/lib/path_coverage/kinds/contains.rb +43 -33
  37. data/lib/path_coverage/kinds/safe.rb +44 -46
  38. data/lib/path_coverage/kinds/write.rb +6 -12
  39. data/lib/path_coverage/kinds.rb +2 -2
  40. data/lib/path_coverage/leaving.rb +44 -0
  41. data/lib/path_coverage/loop_forks.rb +57 -0
  42. data/lib/path_coverage/loop_nodes.rb +55 -0
  43. data/lib/path_coverage/manifest.rb +94 -59
  44. data/lib/path_coverage/manifest_blocks.rb +5 -26
  45. data/lib/path_coverage/manifest_case.rb +2 -2
  46. data/lib/path_coverage/manifest_chains.rb +8 -8
  47. data/lib/path_coverage/manifest_choices.rb +52 -29
  48. data/lib/path_coverage/manifest_disabled.rb +97 -0
  49. data/lib/path_coverage/manifest_folding.rb +66 -26
  50. data/lib/path_coverage/manifest_ignoring.rb +6 -6
  51. data/lib/path_coverage/manifest_writes.rb +11 -10
  52. data/lib/path_coverage/measured_by.rb +20 -0
  53. data/lib/path_coverage/merge.rb +7 -7
  54. data/lib/path_coverage/method_loops.rb +128 -0
  55. data/lib/path_coverage/method_statements.rb +147 -139
  56. data/lib/path_coverage/method_ways.rb +142 -0
  57. data/lib/path_coverage/paths_report.rb +16 -16
  58. data/lib/path_coverage/recorder.rb +57 -36
  59. data/lib/path_coverage/recorder_blocks.rb +17 -17
  60. data/lib/path_coverage/recorder_cases.rb +12 -11
  61. data/lib/path_coverage/recorder_chains.rb +12 -12
  62. data/lib/path_coverage/recorder_writes.rb +9 -9
  63. data/lib/path_coverage/render.rb +58 -51
  64. data/lib/path_coverage/result.rb +32 -31
  65. data/lib/path_coverage/rewriter.rb +39 -27
  66. data/lib/path_coverage/rewriter_nested.rb +13 -13
  67. data/lib/path_coverage/runs_report.rb +84 -0
  68. data/lib/path_coverage/source_lines.rb +16 -16
  69. data/lib/path_coverage/source_report.rb +100 -0
  70. data/lib/path_coverage/stable_leaves.rb +162 -0
  71. data/lib/path_coverage/statement_arms.rb +61 -27
  72. data/lib/path_coverage/summary.rb +4 -62
  73. data/lib/path_coverage/totals.rb +10 -8
  74. data/lib/path_coverage/version.rb +1 -1
  75. data/lib/path_coverage/walk.rb +106 -0
  76. data/lib/path_coverage/way_algebras.rb +127 -0
  77. data/lib/path_coverage/ways_report.rb +91 -0
  78. data/lib/path_coverage/ways_totals.rb +46 -0
  79. data/lib/path_coverage/within_methods.rb +184 -44
  80. metadata +36 -29
  81. data/lib/path_coverage/card.rb +0 -14
  82. data/lib/path_coverage/html/file.html +0 -33
  83. data/lib/path_coverage/html/index.html +0 -29
  84. data/lib/path_coverage/html/settings.js +0 -50
  85. data/lib/path_coverage/html/side.js +0 -470
  86. data/lib/path_coverage/html/sort.js +0 -34
  87. data/lib/path_coverage/html/style.css +0 -470
  88. data/lib/path_coverage/html/tabs.js +0 -15
  89. data/lib/path_coverage/html/theme.js +0 -27
  90. data/lib/path_coverage/html_branch.rb +0 -30
  91. data/lib/path_coverage/html_cards.rb +0 -111
  92. data/lib/path_coverage/html_cells.rb +0 -66
  93. data/lib/path_coverage/html_file.rb +0 -123
  94. data/lib/path_coverage/html_index.rb +0 -128
  95. data/lib/path_coverage/html_index_head.rb +0 -29
  96. data/lib/path_coverage/html_labels.rb +0 -23
  97. data/lib/path_coverage/html_methods.rb +0 -66
  98. data/lib/path_coverage/html_nav.rb +0 -67
  99. data/lib/path_coverage/html_report.rb +0 -131
  100. data/lib/path_coverage/html_statements.rb +0 -166
  101. data/lib/path_coverage/html_tabs.rb +0 -26
  102. data/lib/path_coverage/html_template.rb +0 -26
  103. data/lib/path_coverage/html_tree.rb +0 -131
@@ -0,0 +1,97 @@
1
+ module PathCoverage
2
+ # The lines of a source not analysed: those between a
3
+ # # path: disable comment and the next # path: enable, the
4
+ # line straight after each disable saying why on a # reason:
5
+ # line. Only a comment that is its line's whole text is a
6
+ # marker, so a string or heredoc holding the text is not.
7
+ module ManifestDisabled
8
+ DISABLE = '# path: disable'.freeze
9
+ ENABLE = '# path: enable'.freeze
10
+ REASON = '# reason:'.freeze
11
+
12
+ # The line numbers not analysed in source, at path, parsed as
13
+ # parsed; ArgumentError naming a disable with no reason line
14
+ # straight after it, or with no enable after it, or a
15
+ # statement starting on such a line and ending after it.
16
+ def self.lines(source, parsed, path)
17
+ texts = source.lines
18
+ found = markers(parsed.comments, texts)
19
+ require_reasons(found, texts, path)
20
+ disabled = between(found, path)
21
+ refuse_straddling(parsed.value, disabled, path)
22
+ disabled
23
+ end
24
+
25
+ # [line number, DISABLE or ENABLE] of each comment, of those,
26
+ # that is one of them and its line's whole text, in order.
27
+ def self.markers(comments, texts)
28
+ comments.filter_map do |comment|
29
+ number = comment.location.start_line
30
+ text = texts[number - 1].strip
31
+ [number, text] if [DISABLE, ENABLE].include?(text)
32
+ end
33
+ end
34
+
35
+ # Refuses each DISABLE among found whose next line, of texts,
36
+ # is no REASON line.
37
+ def self.require_reasons(found, texts, path)
38
+ found.each do |number, text|
39
+ next unless text == DISABLE
40
+ # texts[number] is the next line, nil after the last.
41
+ next if texts[number].to_s.strip.start_with?(REASON)
42
+
43
+ raise ArgumentError, "#{path}:#{number} #{DISABLE} needs " \
44
+ "a #{REASON} line straight after it"
45
+ end
46
+ end
47
+
48
+ # The line numbers between each DISABLE among found and the
49
+ # next marker; refuses a DISABLE with no marker after it.
50
+ def self.between(found, path)
51
+ disabled = Set.new
52
+ opened = nil
53
+ found.each do |number, text|
54
+ disabled.merge((opened + 1)...number) if opened
55
+ opened = text == DISABLE ? number : nil
56
+ end
57
+ return disabled unless opened
58
+
59
+ raise ArgumentError,
60
+ "#{path}:#{opened} #{DISABLE} has no #{ENABLE} after it"
61
+ end
62
+
63
+ # Refuses the first statement under tree that starts on a
64
+ # disabled line and ends on an analysed one, as a statement
65
+ # is analysed whole or not at all.
66
+ def self.refuse_straddling(tree, disabled, path)
67
+ found = straddler(tree, disabled)
68
+ return unless found
69
+
70
+ raise ArgumentError, "#{path}:#{found.location.start_line} " \
71
+ 'the statement starting here ends ' \
72
+ "after its #{ENABLE}"
73
+ end
74
+
75
+ # The first statement under node that starts on one of the
76
+ # disabled lines and ends on an analysed one, else nil.
77
+ def self.straddler(node, disabled)
78
+ statements = node.is_a?(Prism::StatementsNode) ? node.body : []
79
+ statements.each do |statement|
80
+ return statement if straddles?(statement, disabled)
81
+ end
82
+ node.compact_child_nodes.each do |child|
83
+ found = straddler(child, disabled)
84
+ return found if found
85
+ end
86
+ nil
87
+ end
88
+
89
+ # True for a statement starting on a disabled line and
90
+ # ending on an analysed one.
91
+ def self.straddles?(statement, disabled)
92
+ location = statement.location
93
+ disabled.include?(location.start_line) &&
94
+ !disabled.include?(location.end_line)
95
+ end
96
+ end
97
+ end
@@ -1,16 +1,61 @@
1
1
  require_relative 'condition'
2
+ require_relative 'decision'
3
+ require_relative 'decision_paths'
2
4
 
3
5
  module PathCoverage
4
- # The part of the manifest's visitor that folds every site in one
5
- # expression into one site. While a leaf is visited, each site
6
- # found in it, the leaf itself included, is collected as a nested
7
- # Condition of that leaf, rather than noted as a site of its own.
8
- # A new scope, eg a block's body or an if's arm, stops the
9
- # collecting, so a site in it is a site of its own. It relies on
10
- # its includer's condition_of, chain_condition, safe_chain? and
11
- # extent_of.
6
+ # The part of the manifest's visitor that folds every decision
7
+ # in one expression into one decision. While a leaf is
8
+ # visited, each decision found in it, the leaf itself
9
+ # included, is collected as a nested Condition of that leaf,
10
+ # rather than noted as a decision of its own. A new scope, eg
11
+ # a block's body or an if's arm, stops the collecting, so a
12
+ # decision in it is a decision of its own. It relies on its
13
+ # includer's condition_of, chain_condition, safe_chain?,
14
+ # block_decision? and extent_of.
12
15
  module ManifestFolding
13
- # node's Condition as a site nested in a leaf: a &. chain's
16
+ # The decisions that fold into the leaf operand, as collected
17
+ # finds them; none for a leaf that is wholly a decision of
18
+ # its own, as own_decision? has it, visited as one, as the
19
+ # leaf's outcome is its value.
20
+ def leaf_decisions(operand)
21
+ unless own_decision?(operand)
22
+ return collected { operand.accept(self) }
23
+ end
24
+
25
+ scoped { operand.accept(self) }
26
+ []
27
+ end
28
+
29
+ # True for a leaf that is wholly a decision of its own: a block
30
+ # method's call whose value follows from its path, or an && or
31
+ # ||, eg a predicate, not ignored.
32
+ def own_decision?(node)
33
+ valued?(node) || (operator?(node) && !ignored?(node))
34
+ end
35
+
36
+ # True for a block method's call that is a decision, not ignored,
37
+ # whose value follows from its path, eg any?, but not find.
38
+ def valued?(node)
39
+ node.type == :call_node && block_decision?(node) &&
40
+ VALUED.include?(node.name.to_s) && !ignored?(node)
41
+ end
42
+
43
+ # The one outcome each of a decision's values gives a leaf.
44
+ GIVEN = { [true] => :truthy, [false] => :falsy }.freeze
45
+
46
+ # The one outcome the leaf node can have, as the decision it
47
+ # wholly is gives it on every one of its paths, eg :truthy for
48
+ # a select, whose array is truthy; nil when its paths give
49
+ # both, or node is no such decision.
50
+ def given_outcome(node)
51
+ return nil unless own_decision?(node)
52
+
53
+ name = Decision.at(@path, decision_loc(node))
54
+ decision = @decisions.fetch(name)
55
+ GIVEN[DecisionPaths.values(decision).uniq]
56
+ end
57
+
58
+ # node's Condition as a decision nested in a leaf: a &. chain's
14
59
  # with its value as its last leaf, as its leaf's outcome
15
60
  # follows from it.
16
61
  def nested_condition_of(node)
@@ -19,8 +64,8 @@ module PathCoverage
19
64
  condition_of(node)
20
65
  end
21
66
 
22
- # The Conditions of the sites found while yielding, each one
23
- # collected rather than noted as a site of its own.
67
+ # The Conditions of the decisions found while yielding, each one
68
+ # collected rather than noted as a decision of its own.
24
69
  def collected
25
70
  outer = @inner
26
71
  @inner = []
@@ -30,8 +75,8 @@ module PathCoverage
30
75
  @inner = outer
31
76
  end
32
77
 
33
- # Yields with no collecting, so a site found is one of its own:
34
- # for a new scope, eg a block's body or an if's arm.
78
+ # Yields with no collecting, so a decision found is one of its
79
+ # own: for a new scope, eg a block's body or an if's arm.
35
80
  def scoped
36
81
  outer = @inner
37
82
  @inner = nil
@@ -40,20 +85,15 @@ module PathCoverage
40
85
  @inner = outer
41
86
  end
42
87
 
43
- # range as a leaf, or, with inner sites found in it, a Condition
44
- # named contains: those sites, then range, the leaf's outcome,
45
- # fixed as :nonempty when nonempty. A leaf that is wholly one
46
- # block method's call whose value follows from its path is
47
- # that call alone, needing no outcome of its own.
88
+ # range as a leaf, or, with inner decisions found in it, a
89
+ # Condition named contains: those decisions, then range, the
90
+ # leaf's outcome, fixed as :nonempty when nonempty. A leaf
91
+ # that is wholly one block method's call whose value follows
92
+ # from its path never comes here: it is a decision of its
93
+ # own, as leaf_decisions has it.
48
94
  def contained(inner, range, nonempty: false)
49
95
  return range if inner.empty?
50
96
 
51
- only = inner.first
52
- if inner.size == 1 && only.extent == range &&
53
- VALUED.include?(only.operator)
54
- return only
55
- end
56
-
57
97
  fixed = nonempty ? { range => :nonempty } : {}
58
98
  Condition.new(operator: CONTAINS, operands: [*inner, range],
59
99
  extent: range, fixed:)
@@ -61,8 +101,8 @@ module PathCoverage
61
101
 
62
102
  # True for a call, with no arguments or block, to a method
63
103
  # treated as nonempty, eg any?, whose receiver, parenthesised
64
- # or not, is one of inner, the sites found in it: its outcome
65
- # then follows from that site's path. It relies on its
104
+ # or not, is one of inner, the decisions found in it: its outcome
105
+ # then follows from that decision's path. It relies on its
66
106
  # includer's grouped.
67
107
  def nonempty_call?(node, inner)
68
108
  return false unless node.type == :call_node
@@ -1,12 +1,12 @@
1
1
  module PathCoverage
2
- # The part of the manifest's visitor that leaves out the sites
2
+ # The part of the manifest's visitor that leaves out the decisions
3
3
  # of ignored operators, eg ignore '&&='. An ignored node is no
4
- # site, though a site inside it may be; nested in a site, it
5
- # is a leaf. It relies on its includer's @ignored, ternary?
6
- # and safe_chain?.
4
+ # decision, though a decision inside it may be; nested in a
5
+ # decision, it is a leaf. It relies on its includer's
6
+ # @ignored, ternary? and safe_chain?.
7
7
  module ManifestIgnoring
8
8
  # The operator of each node type that always names one kind
9
- # of site.
9
+ # of decision.
10
10
  NAMED = { unless_node: 'unless', case_node: 'case' }.freeze
11
11
 
12
12
  # True for a node whose operator is ignored.
@@ -14,7 +14,7 @@ module PathCoverage
14
14
  @ignored.include?(operator_of(node))
15
15
  end
16
16
 
17
- # The operator naming node's kind of site, eg ?: for a
17
+ # The operator naming node's kind of decision, eg ?: for a
18
18
  # ternary or any? for a block method's call; nil for a
19
19
  # node with none.
20
20
  def operator_of(node)
@@ -1,9 +1,9 @@
1
1
  require_relative 'condition'
2
2
 
3
3
  module PathCoverage
4
- # The part of the manifest's visitor that builds the sites of
4
+ # The part of the manifest's visitor that builds the decisions of
5
5
  # ||= and &&= writes, eg @clock ||= Clock.new. It relies on
6
- # its includer's add_site, bare_less? and extent_of.
6
+ # its includer's add_decision and extent_of.
7
7
  module ManifestWrites
8
8
  # Every node type of a ||= or &&= write, by its target: a
9
9
  # variable of each kind, a constant, a constant path, an
@@ -15,15 +15,15 @@ module PathCoverage
15
15
  %W[#{target}_or_write_node #{target}_and_write_node]
16
16
  end.map(&:to_sym).freeze
17
17
 
18
- # Notes each kind of write as a site, eg
18
+ # Notes each kind of write as a decision, eg
19
19
  # visit_index_or_write_node for store[key] ||= 0.
20
20
  WRITE_TYPES.each do |type|
21
21
  define_method(:"visit_#{type}") do |node|
22
- write?(node) ? add_site(node) : visit_child_nodes(node)
22
+ write?(node) ? add_decision(node) : visit_child_nodes(node)
23
23
  end
24
24
  end
25
25
 
26
- # True for a ||= or &&= write that is a site: any but one
26
+ # True for a ||= or &&= write that is a decision: any but one
27
27
  # through &., eg box&.size ||= 3, which a nil box skips whole,
28
28
  # its target never looked at, so neither of its paths taken.
29
29
  def write?(node)
@@ -40,15 +40,16 @@ module PathCoverage
40
40
  # target is false for ||=, true for &&=. The target is a
41
41
  # leaf the rewriter never reads again, as reading it could
42
42
  # call a method twice; its outcome follows from whether the
43
- # arm ran. Any site inside the write, eg in an index or on
44
- # the right side, is a site of its own. A right side that is
43
+ # arm ran. Any decision inside the write, eg in an index or on
44
+ # the right side, is a decision of its own. A right side that is
45
45
  # no bare argument, eg a command call, is noted among the
46
- # site's commands, to be parenthesised.
46
+ # decision's commands, to be parenthesised.
47
47
  def write_condition(node)
48
48
  visit_child_nodes(node)
49
49
  value = extent_of(node.value)
50
- @notes[:commands] << value if bare_less?(node.value)
51
- arm = Arm.new(kind: :then, extent: value, body: nil)
50
+ @notes[:commands] << value if Manifest.bare_less?(node.value)
51
+ arm = Arm.new(kind: :then, extent: value, body: nil,
52
+ leaves: nil)
52
53
  Condition.new(
53
54
  operator: node.operator, operands: [target_of(node), arm],
54
55
  extent: extent_of(node)
@@ -0,0 +1,20 @@
1
+ require 'prism'
2
+ require_relative 'version'
3
+
4
+ module PathCoverage
5
+ # The tools that parse and measure a run, each with its version:
6
+ # Ruby, Prism and path_coverage itself. A change to any of them
7
+ # can list a file's decisions or paths differently, so a report
8
+ # names them as it is written, and reports merge only when they
9
+ # name the same.
10
+ module MeasuredBy
11
+ # { tool name => its version } of this process, eg
12
+ # { 'ruby' => '4.0.5', 'prism' => '1.8.1', ... }.
13
+ def self.versions
14
+ {
15
+ 'ruby' => RUBY_VERSION, 'prism' => Prism::VERSION,
16
+ 'path_coverage' => VERSION
17
+ }
18
+ end
19
+ end
20
+ end
@@ -5,21 +5,21 @@ module PathCoverage
5
5
  # counts are the sums of every run's.
6
6
  def self.operands(runs)
7
7
  runs.reduce do |total, run|
8
- total.merge(run) do |_site, mine, theirs|
8
+ total.merge(run) do |_decision, mine, theirs|
9
9
  mine.zip(theirs).map { |a, b| sum(a, b) }
10
10
  end
11
11
  end
12
12
  end
13
13
 
14
- # One site paths hash, as Result#paths gives them, each
14
+ # One decision paths hash, as Result#paths gives them, each
15
15
  # path's count the sum of every run's. Runs that list a
16
- # site's paths differently raise, as adding their counts by
16
+ # decision's paths differently raise, as adding their counts by
17
17
  # position would credit one path's count to another.
18
18
  def self.paths(runs)
19
19
  runs.reduce do |total, run|
20
- total.merge(run) do |site, mine, theirs|
20
+ total.merge(run) do |name, mine, theirs|
21
21
  same = mine.map { _1[:path] } == theirs.map { _1[:path] }
22
- raise ArgumentError, "runs differ at #{site}" unless same
22
+ raise ArgumentError, "runs differ at #{name}" unless same
23
23
 
24
24
  mine.zip(theirs).map do |a, b|
25
25
  { path: a[:path], count: a[:count] + b[:count] }
@@ -28,13 +28,13 @@ module PathCoverage
28
28
  end
29
29
  end
30
30
 
31
- # One listed hash, { site => { path => count } }, eg the
31
+ # One listed hash, { decision => { path => count } }, eg the
32
32
  # paths a treatment rules out, each path's count the sum of
33
33
  # every run's, eg [true, false] taken once in each of two
34
34
  # runs is taken twice.
35
35
  def self.listed(runs)
36
36
  runs.reduce do |total, run|
37
- total.merge(run) do |_site, mine, theirs|
37
+ total.merge(run) do |_decision, mine, theirs|
38
38
  mine.merge(theirs) { |_path, a, b| a + b }
39
39
  end
40
40
  end
@@ -0,0 +1,128 @@
1
+ require 'prism'
2
+ require_relative 'card_tree'
3
+ require_relative 'decision'
4
+ require_relative 'loop_forks'
5
+ require_relative 'loop_nodes'
6
+ require_relative 'manifest'
7
+ require_relative 'method_statements'
8
+
9
+ module PathCoverage
10
+ # The loops of each method: the blocks, lambdas, whiles, untils
11
+ # and fors in its body, each run 0 or more times, and which of
12
+ # them hold each of its rows, as a call's Walk takes them; a
13
+ # while's or until's body holds its rows, its test only its
14
+ # first run's, as Walk's repeats have them.
15
+ module MethodLoops
16
+ # { def decision => { name => the loops holding it, outermost
17
+ # first } } for each def in source, at path: each of its
18
+ # rows, as MethodStatements gives them, and each loop holding
19
+ # one or forking, as LoopForks finds them, by name, as Decision
20
+ # names where it starts.
21
+ def self.of(source, path, decisions = nil)
22
+ decisions ||= Manifest.decisions(source, path)
23
+ rows = MethodStatements.of(source, path, decisions)
24
+ forks = LoopForks.of(source, path, decisions)
25
+ starts = CardTree.line_starts(source)
26
+ LoopNodes.defs(Prism.parse(source).value).to_h do |node|
27
+ decision = Decision.of(path, node)
28
+ loops = [named(node, path), forks[decision].keys]
29
+ [decision, holding(rows[decision], loops, starts)]
30
+ end
31
+ end
32
+
33
+ # { loop's name => where its first statement starts, a byte
34
+ # offset } of each loop in source, at path, holding a row or
35
+ # forking, as loops, the result of of, has them: where a pass
36
+ # call goes.
37
+ def self.passes(source, path, loops)
38
+ held = loops.values.flat_map { _1.to_a.flatten }.to_set
39
+ defs = LoopNodes.defs(Prism.parse(source).value)
40
+ nodes = defs.flat_map { LoopNodes.loops_in(_1.body) }
41
+ nodes.filter_map { pass_at(_1, path, held) }.to_h
42
+ end
43
+
44
+ # [name, offset] of node, a loop, where its pass call goes,
45
+ # when it is among held and its body has a statement; else nil.
46
+ def self.pass_at(node, path, held)
47
+ name = Decision.at(path, node.location)
48
+ return unless held.include?(name) && !LoopNodes.modifier?(node)
49
+
50
+ first = LoopNodes.first_statement(LoopNodes.body_of(node))
51
+ [name, first.location.start_offset] if first
52
+ end
53
+
54
+ # [[name, location], ...] of the loops in node's body, a def's,
55
+ # in the order they start, each location the source holding
56
+ # its rows: a block's or lambda's whole, a keyword loop's body;
57
+ # none of a keyword loop with an empty body.
58
+ def self.named(node, path)
59
+ LoopNodes.loops_in(node.body).filter_map do |loop|
60
+ keyword = LoopNodes::KEYWORDS.include?(loop.type)
61
+ held = keyword ? loop.statements : loop
62
+ [Decision.at(path, loop.location), held.location] if held
63
+ end
64
+ end
65
+
66
+ # { def decision => { row's name => the name of the while or
67
+ # until whose test holds it } } for each def in source, at
68
+ # path: rows whose later runs repeat the loop's first, so take
69
+ # no step, as Walk's repeats have them.
70
+ def self.repeats(source, path, decisions = nil)
71
+ rows = MethodStatements.of(source, path, decisions)
72
+ starts = CardTree.line_starts(source)
73
+ LoopNodes.defs(Prism.parse(source).value).to_h do |node|
74
+ decision = Decision.of(path, node)
75
+ tests = tests_in(node.body, path)
76
+ [decision, tested(rows[decision], tests, starts)]
77
+ end
78
+ end
79
+
80
+ # { row's name => the loop whose test holds it } of each of
81
+ # rows in one of tests, as tests_in gives them.
82
+ def self.tested(rows, tests, starts)
83
+ rows.filter_map do |row|
84
+ loop = around(tests, starts[row.line - 1] + row.column).first
85
+ [row.name, loop] if loop
86
+ end.to_h
87
+ end
88
+
89
+ # [[name, location], ...] of the test of each while and until
90
+ # in node's body, by its loop's name.
91
+ def self.tests_in(node, path)
92
+ LoopNodes.loops_in(node).filter_map do |loop|
93
+ next unless %i[while_node until_node].include?(loop.type)
94
+
95
+ [Decision.at(path, loop.location), loop.predicate.location]
96
+ end
97
+ end
98
+
99
+ # { name => the loops holding it, outermost first } of each of
100
+ # rows, of each loop holding one, and of each of forked, the
101
+ # names of the loops that fork; loops is [[name, location],
102
+ # ...] in the order they start.
103
+ def self.holding(rows, (loops, forked), starts)
104
+ held = rows.to_h do |row|
105
+ [row.name, around(loops, starts[row.line - 1] + row.column)]
106
+ end
107
+ held.merge(outer((held.values.flatten + forked).uniq, loops))
108
+ end
109
+
110
+ # { name => the loops holding it, outermost first } of each
111
+ # loop named in names; loops as holding has them.
112
+ def self.outer(names, loops)
113
+ locations = loops.to_h
114
+ names.to_h do |name|
115
+ [name, around(loops, locations[name].start_offset) - [name]]
116
+ end
117
+ end
118
+
119
+ # The names of the loops whose source holds the byte at
120
+ # offset, outermost first.
121
+ def self.around(loops, offset)
122
+ loops.filter_map do |name, location|
123
+ name if (location.start_offset...location.end_offset)
124
+ .cover?(offset)
125
+ end
126
+ end
127
+ end
128
+ end