path_coverage 0.4.0 → 0.6.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 +4 -4
- data/README.md +260 -300
- data/exe/path_coverage_report +4 -0
- data/lib/path_coverage/call_trace.rb +109 -0
- data/lib/path_coverage/calls.rb +73 -43
- data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +35 -33
- data/lib/path_coverage/condition.rb +14 -14
- data/lib/path_coverage/configuration.rb +7 -4
- data/lib/path_coverage/configuration_limits.rb +52 -0
- data/lib/path_coverage/configuration_paths.rb +7 -7
- data/lib/path_coverage/console_summary.rb +28 -26
- data/lib/path_coverage/{site.rb → decision.rb} +2 -2
- data/lib/path_coverage/decision_locations.rb +76 -0
- data/lib/path_coverage/{site_paths.rb → decision_paths.rb} +13 -11
- data/lib/path_coverage/{site_tree.rb → decision_tree.rb} +25 -16
- data/lib/path_coverage/fold_branches.rb +18 -0
- data/lib/path_coverage/fold_cells.rb +42 -0
- data/lib/path_coverage/{html_tree.rb → fold_rows.rb} +43 -72
- data/lib/path_coverage/gaps.rb +33 -15
- data/lib/path_coverage/json_reader.rb +54 -19
- data/lib/path_coverage/json_reports.rb +109 -0
- data/lib/path_coverage/json_writer.rb +29 -14
- data/lib/path_coverage/kernel.rb +77 -58
- data/lib/path_coverage/kinds/base.rb +16 -16
- data/lib/path_coverage/kinds/block.rb +16 -15
- data/lib/path_coverage/kinds/case.rb +14 -12
- data/lib/path_coverage/kinds/chain.rb +40 -11
- data/lib/path_coverage/kinds/choice.rb +4 -3
- data/lib/path_coverage/kinds/contains.rb +40 -32
- data/lib/path_coverage/kinds/safe.rb +38 -26
- data/lib/path_coverage/kinds/write.rb +6 -6
- data/lib/path_coverage/kinds.rb +2 -2
- data/lib/path_coverage/leaving.rb +52 -0
- data/lib/path_coverage/loop_forks.rb +57 -0
- data/lib/path_coverage/loop_nodes.rb +91 -0
- data/lib/path_coverage/manifest.rb +92 -58
- data/lib/path_coverage/manifest_blocks.rb +5 -26
- data/lib/path_coverage/manifest_case.rb +2 -2
- data/lib/path_coverage/manifest_chains.rb +8 -8
- data/lib/path_coverage/manifest_choices.rb +23 -20
- data/lib/path_coverage/manifest_disabled.rb +97 -0
- data/lib/path_coverage/manifest_folding.rb +43 -40
- data/lib/path_coverage/manifest_ignoring.rb +6 -6
- data/lib/path_coverage/manifest_writes.rb +11 -10
- data/lib/path_coverage/measured_by.rb +20 -0
- data/lib/path_coverage/merge.rb +7 -7
- data/lib/path_coverage/method_loops.rb +172 -0
- data/lib/path_coverage/method_statements.rb +150 -134
- data/lib/path_coverage/method_ways.rb +170 -0
- data/lib/path_coverage/paths_context.rb +104 -0
- data/lib/path_coverage/paths_fitting.rb +31 -0
- data/lib/path_coverage/paths_forks.rb +72 -0
- data/lib/path_coverage/paths_gaps.rb +137 -0
- data/lib/path_coverage/paths_leaving.rb +40 -0
- data/lib/path_coverage/paths_report.rb +16 -16
- data/lib/path_coverage/paths_rescues.rb +146 -0
- data/lib/path_coverage/paths_scopes.rb +37 -0
- data/lib/path_coverage/paths_tree.rb +203 -0
- data/lib/path_coverage/paths_way_lists.rb +72 -0
- data/lib/path_coverage/paths_ways.rb +193 -0
- data/lib/path_coverage/recorder.rb +71 -36
- data/lib/path_coverage/recorder_blocks.rb +17 -17
- data/lib/path_coverage/recorder_cases.rb +12 -11
- data/lib/path_coverage/recorder_chains.rb +12 -12
- data/lib/path_coverage/recorder_writes.rb +9 -9
- data/lib/path_coverage/render.rb +58 -51
- data/lib/path_coverage/rescue_forks.rb +174 -0
- data/lib/path_coverage/result.rb +32 -31
- data/lib/path_coverage/rewriter.rb +116 -35
- data/lib/path_coverage/rewriter_nested.rb +13 -13
- data/lib/path_coverage/runs_report.rb +84 -0
- data/lib/path_coverage/source_lines.rb +16 -16
- data/lib/path_coverage/source_report.rb +100 -0
- data/lib/path_coverage/stable_leaves.rb +162 -0
- data/lib/path_coverage/statement_arms.rb +53 -38
- data/lib/path_coverage/summary.rb +4 -62
- data/lib/path_coverage/totals.rb +12 -10
- data/lib/path_coverage/version.rb +1 -1
- data/lib/path_coverage/walk.rb +106 -0
- data/lib/path_coverage/way_algebras.rb +127 -0
- data/lib/path_coverage/ways_report.rb +91 -0
- data/lib/path_coverage/ways_totals.rb +46 -0
- data/lib/path_coverage/within_methods.rb +152 -97
- metadata +39 -30
- data/lib/path_coverage/card.rb +0 -14
- data/lib/path_coverage/card_tree.rb +0 -186
- data/lib/path_coverage/card_ways.rb +0 -140
- data/lib/path_coverage/html/file.html +0 -33
- data/lib/path_coverage/html/index.html +0 -29
- data/lib/path_coverage/html/settings.js +0 -50
- data/lib/path_coverage/html/side.js +0 -599
- data/lib/path_coverage/html/sort.js +0 -34
- data/lib/path_coverage/html/style.css +0 -484
- data/lib/path_coverage/html/tabs.js +0 -15
- data/lib/path_coverage/html/theme.js +0 -27
- data/lib/path_coverage/html_branch.rb +0 -30
- data/lib/path_coverage/html_cards.rb +0 -88
- data/lib/path_coverage/html_cells.rb +0 -71
- data/lib/path_coverage/html_file.rb +0 -123
- data/lib/path_coverage/html_index.rb +0 -128
- data/lib/path_coverage/html_index_head.rb +0 -29
- data/lib/path_coverage/html_labels.rb +0 -16
- data/lib/path_coverage/html_methods.rb +0 -66
- data/lib/path_coverage/html_nav.rb +0 -67
- data/lib/path_coverage/html_report.rb +0 -131
- data/lib/path_coverage/html_statements.rb +0 -186
- data/lib/path_coverage/html_tabs.rb +0 -26
- data/lib/path_coverage/html_template.rb +0 -26
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
require_relative 'condition'
|
|
2
|
-
require_relative '
|
|
3
|
-
require_relative '
|
|
2
|
+
require_relative 'decision'
|
|
3
|
+
require_relative 'decision_paths'
|
|
4
4
|
|
|
5
5
|
module PathCoverage
|
|
6
|
-
# The part of the manifest's visitor that folds every
|
|
7
|
-
# expression into one
|
|
8
|
-
# found in it, the leaf itself
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
# its
|
|
13
|
-
#
|
|
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.
|
|
14
15
|
module ManifestFolding
|
|
15
|
-
# The
|
|
16
|
-
# finds them; none for a leaf that is wholly a
|
|
17
|
-
# as
|
|
18
|
-
# its value.
|
|
19
|
-
def
|
|
20
|
-
unless
|
|
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)
|
|
21
22
|
return collected { operand.accept(self) }
|
|
22
23
|
end
|
|
23
24
|
|
|
@@ -25,35 +26,36 @@ module PathCoverage
|
|
|
25
26
|
[]
|
|
26
27
|
end
|
|
27
28
|
|
|
28
|
-
# True for a leaf that is wholly a
|
|
29
|
+
# True for a leaf that is wholly a decision of its own: a block
|
|
29
30
|
# method's call whose value follows from its path, or an && or
|
|
30
31
|
# ||, eg a predicate, not ignored.
|
|
31
|
-
def
|
|
32
|
+
def own_decision?(node)
|
|
32
33
|
valued?(node) || (operator?(node) && !ignored?(node))
|
|
33
34
|
end
|
|
34
35
|
|
|
35
|
-
# True for a block method's call that is a
|
|
36
|
+
# True for a block method's call that is a decision, not ignored,
|
|
36
37
|
# whose value follows from its path, eg any?, but not find.
|
|
37
38
|
def valued?(node)
|
|
38
|
-
node.type == :call_node &&
|
|
39
|
+
node.type == :call_node && block_decision?(node) &&
|
|
39
40
|
VALUED.include?(node.name.to_s) && !ignored?(node)
|
|
40
41
|
end
|
|
41
42
|
|
|
42
|
-
# The one outcome each of a
|
|
43
|
+
# The one outcome each of a decision's values gives a leaf.
|
|
43
44
|
GIVEN = { [true] => :truthy, [false] => :falsy }.freeze
|
|
44
45
|
|
|
45
|
-
# The one outcome the leaf node can have, as the
|
|
46
|
-
# is gives it on every one of its paths, eg :truthy for
|
|
47
|
-
# select, whose array is truthy; nil when its paths give
|
|
48
|
-
# or node is no such
|
|
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.
|
|
49
50
|
def given_outcome(node)
|
|
50
|
-
return nil unless
|
|
51
|
+
return nil unless own_decision?(node)
|
|
51
52
|
|
|
52
|
-
|
|
53
|
-
|
|
53
|
+
name = Decision.at(@path, decision_loc(node))
|
|
54
|
+
decision = @decisions.fetch(name)
|
|
55
|
+
GIVEN[DecisionPaths.values(decision).uniq]
|
|
54
56
|
end
|
|
55
57
|
|
|
56
|
-
# node's Condition as a
|
|
58
|
+
# node's Condition as a decision nested in a leaf: a &. chain's
|
|
57
59
|
# with its value as its last leaf, as its leaf's outcome
|
|
58
60
|
# follows from it.
|
|
59
61
|
def nested_condition_of(node)
|
|
@@ -62,8 +64,8 @@ module PathCoverage
|
|
|
62
64
|
condition_of(node)
|
|
63
65
|
end
|
|
64
66
|
|
|
65
|
-
# The Conditions of the
|
|
66
|
-
# collected rather than noted as a
|
|
67
|
+
# The Conditions of the decisions found while yielding, each one
|
|
68
|
+
# collected rather than noted as a decision of its own.
|
|
67
69
|
def collected
|
|
68
70
|
outer = @inner
|
|
69
71
|
@inner = []
|
|
@@ -73,8 +75,8 @@ module PathCoverage
|
|
|
73
75
|
@inner = outer
|
|
74
76
|
end
|
|
75
77
|
|
|
76
|
-
# Yields with no collecting, so a
|
|
77
|
-
# 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.
|
|
78
80
|
def scoped
|
|
79
81
|
outer = @inner
|
|
80
82
|
@inner = nil
|
|
@@ -83,11 +85,12 @@ module PathCoverage
|
|
|
83
85
|
@inner = outer
|
|
84
86
|
end
|
|
85
87
|
|
|
86
|
-
# range as a leaf, or, with inner
|
|
87
|
-
# named contains: those
|
|
88
|
-
# fixed as :nonempty when nonempty. A leaf
|
|
89
|
-
# block method's call whose value follows
|
|
90
|
-
# comes here: it is a
|
|
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.
|
|
91
94
|
def contained(inner, range, nonempty: false)
|
|
92
95
|
return range if inner.empty?
|
|
93
96
|
|
|
@@ -98,8 +101,8 @@ module PathCoverage
|
|
|
98
101
|
|
|
99
102
|
# True for a call, with no arguments or block, to a method
|
|
100
103
|
# treated as nonempty, eg any?, whose receiver, parenthesised
|
|
101
|
-
# or not, is one of inner, the
|
|
102
|
-
# then follows from that
|
|
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
|
|
103
106
|
# includer's grouped.
|
|
104
107
|
def nonempty_call?(node, inner)
|
|
105
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
|
|
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
|
-
#
|
|
5
|
-
# is a leaf. It relies on its includer's
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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) ?
|
|
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
|
|
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
|
|
44
|
-
# the right side, is a
|
|
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
|
-
#
|
|
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
|
data/lib/path_coverage/merge.rb
CHANGED
|
@@ -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 |
|
|
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
|
|
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
|
-
#
|
|
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 |
|
|
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 #{
|
|
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, {
|
|
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 |
|
|
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,172 @@
|
|
|
1
|
+
require 'prism'
|
|
2
|
+
require_relative 'paths_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
|
+
require_relative 'rescue_forks'
|
|
9
|
+
|
|
10
|
+
module PathCoverage
|
|
11
|
+
# The loops of each method: the blocks, lambdas, whiles, untils
|
|
12
|
+
# and fors in its body, each run 0 or more times, and which of
|
|
13
|
+
# them hold each of its rows, as a call's Walk takes them; a
|
|
14
|
+
# while's or until's body holds its rows, its test only its
|
|
15
|
+
# first run's, as Walk's repeats have them.
|
|
16
|
+
module MethodLoops
|
|
17
|
+
# { def decision => { name => the loops holding it, outermost
|
|
18
|
+
# first } } for each def in source, at path. It holds each of
|
|
19
|
+
# its rows, as MethodStatements gives them, and each of its
|
|
20
|
+
# rescue forks, as RescueForks gives them. It also holds each
|
|
21
|
+
# loop holding one of those, or forking, as LoopForks finds
|
|
22
|
+
# them. Each is by name, as Decision names where it starts.
|
|
23
|
+
def self.of(source, path, decisions = nil)
|
|
24
|
+
decisions ||= Manifest.decisions(source, path)
|
|
25
|
+
spots = spots_in(source, path, decisions)
|
|
26
|
+
forks = LoopForks.of(source, path, decisions)
|
|
27
|
+
starts = PathsTree.line_starts(source)
|
|
28
|
+
LoopNodes.defs(Prism.parse(source).value).to_h do |node|
|
|
29
|
+
decision = Decision.of(path, node)
|
|
30
|
+
loops = [named(node, path), forks[decision].keys]
|
|
31
|
+
[decision, holding(spots.fetch(decision), loops, starts)]
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# A spot a loop can hold, named where it is, like a row.
|
|
36
|
+
Spot = Data.define(:name, :line, :column)
|
|
37
|
+
|
|
38
|
+
# The lines of the defs in source whose body's rescue retries.
|
|
39
|
+
# A retry there restarts the method's frame, so Ruby reports a
|
|
40
|
+
# call of it again, with no return of the first.
|
|
41
|
+
def self.restarts(source)
|
|
42
|
+
defs = LoopNodes.defs(Prism.parse(source).value)
|
|
43
|
+
defs.filter_map do |node|
|
|
44
|
+
node.location.start_line if restarting?(node.body)
|
|
45
|
+
end.to_set
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# True for body, a def's, when a retry restarts it: a begin of
|
|
49
|
+
# the def's own whose rescue retries. An empty body is nil.
|
|
50
|
+
def self.restarting?(body)
|
|
51
|
+
return false if body.nil?
|
|
52
|
+
|
|
53
|
+
LoopNodes.implicit?(body) && LoopNodes.loop?(body)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# { def decision => its spots } for each def in source, at
|
|
57
|
+
# path. Its spots are its rows, as MethodStatements gives them,
|
|
58
|
+
# and the Spot of each of its rescue forks.
|
|
59
|
+
def self.spots_in(source, path, decisions)
|
|
60
|
+
rows = MethodStatements.of(source, path, decisions)
|
|
61
|
+
rescues = RescueForks.of(source, path)
|
|
62
|
+
rows.to_h do |decision, held|
|
|
63
|
+
[decision, held + spots_of(rescues.fetch(decision))]
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# The Spot of each rescue fork of forks, a def's, as
|
|
68
|
+
# RescueForks gives them.
|
|
69
|
+
def self.spots_of(forks)
|
|
70
|
+
forks.keys.map do |name|
|
|
71
|
+
line, column = name.split(':').last(2).map(&:to_i)
|
|
72
|
+
Spot.new(name:, line:, column:)
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# { loop's name => where its first statement starts, a byte
|
|
77
|
+
# offset } of each loop in source, at path, holding a row or
|
|
78
|
+
# forking, as loops, the result of of, has them: where a pass
|
|
79
|
+
# call goes.
|
|
80
|
+
def self.passes(source, path, loops)
|
|
81
|
+
held = loops.values.flat_map { _1.to_a.flatten }.to_set
|
|
82
|
+
defs = LoopNodes.defs(Prism.parse(source).value)
|
|
83
|
+
nodes = defs.flat_map { LoopNodes.loops_in(_1.body) }
|
|
84
|
+
nodes.filter_map { pass_at(_1, path, held) }.to_h
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# [name, offset] of node, a loop, where its pass call goes,
|
|
88
|
+
# when it is among held and its body has a statement; else nil.
|
|
89
|
+
def self.pass_at(node, path, held)
|
|
90
|
+
name = Decision.at(path, LoopNodes.at(node))
|
|
91
|
+
return unless held.include?(name) && !LoopNodes.modifier?(node)
|
|
92
|
+
|
|
93
|
+
first = LoopNodes.first_statement(LoopNodes.body_of(node))
|
|
94
|
+
[name, first.location.start_offset] if first
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# [[name, location], ...] of the loops in node's body, a def's,
|
|
98
|
+
# in the order they start, each location the source holding
|
|
99
|
+
# its rows: a block's or lambda's whole, a keyword loop's body;
|
|
100
|
+
# none of a keyword loop with an empty body.
|
|
101
|
+
def self.named(node, path)
|
|
102
|
+
LoopNodes.loops_in(node.body).filter_map do |loop|
|
|
103
|
+
keyword = LoopNodes::KEYWORDS.include?(loop.type)
|
|
104
|
+
held = keyword ? loop.statements : loop
|
|
105
|
+
[Decision.at(path, LoopNodes.at(loop)), held.location] if held
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# { def decision => { row's name => the name of the while or
|
|
110
|
+
# until whose test holds it } } for each def in source, at
|
|
111
|
+
# path: rows whose later runs repeat the loop's first, so take
|
|
112
|
+
# no step, as Walk's repeats have them.
|
|
113
|
+
def self.repeats(source, path, decisions = nil)
|
|
114
|
+
rows = MethodStatements.of(source, path, decisions)
|
|
115
|
+
starts = PathsTree.line_starts(source)
|
|
116
|
+
LoopNodes.defs(Prism.parse(source).value).to_h do |node|
|
|
117
|
+
decision = Decision.of(path, node)
|
|
118
|
+
tests = tests_in(node.body, path)
|
|
119
|
+
[decision, tested(rows[decision], tests, starts)]
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# { row's name => the loop whose test holds it } of each of
|
|
124
|
+
# rows in one of tests, as tests_in gives them.
|
|
125
|
+
def self.tested(rows, tests, starts)
|
|
126
|
+
rows.filter_map do |row|
|
|
127
|
+
loop = around(tests, starts[row.line - 1] + row.column).first
|
|
128
|
+
[row.name, loop] if loop
|
|
129
|
+
end.to_h
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# [[name, location], ...] of the test of each while and until
|
|
133
|
+
# in node's body, by its loop's name.
|
|
134
|
+
def self.tests_in(node, path)
|
|
135
|
+
LoopNodes.loops_in(node).filter_map do |loop|
|
|
136
|
+
next unless %i[while_node until_node].include?(loop.type)
|
|
137
|
+
|
|
138
|
+
name = Decision.at(path, LoopNodes.at(loop))
|
|
139
|
+
[name, loop.predicate.location]
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# { name => the loops holding it, outermost first } of each of
|
|
144
|
+
# rows, of each loop holding one, and of each of forked, the
|
|
145
|
+
# names of the loops that fork; loops is [[name, location],
|
|
146
|
+
# ...] in the order they start.
|
|
147
|
+
def self.holding(rows, (loops, forked), starts)
|
|
148
|
+
held = rows.to_h do |row|
|
|
149
|
+
[row.name, around(loops, starts[row.line - 1] + row.column)]
|
|
150
|
+
end
|
|
151
|
+
held.merge(outer((held.values.flatten + forked).uniq, loops))
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# { name => the loops holding it, outermost first } of each
|
|
155
|
+
# loop named in names; loops as holding has them.
|
|
156
|
+
def self.outer(names, loops)
|
|
157
|
+
locations = loops.to_h
|
|
158
|
+
names.to_h do |name|
|
|
159
|
+
[name, around(loops, locations[name].start_offset) - [name]]
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# The names of the loops whose source holds the byte at
|
|
164
|
+
# offset, outermost first.
|
|
165
|
+
def self.around(loops, offset)
|
|
166
|
+
loops.filter_map do |name, location|
|
|
167
|
+
name if (location.start_offset...location.end_offset)
|
|
168
|
+
.cover?(offset)
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|