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.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +260 -300
  3. data/exe/path_coverage_report +4 -0
  4. data/lib/path_coverage/call_trace.rb +109 -0
  5. data/lib/path_coverage/calls.rb +73 -43
  6. data/lib/path_coverage/{html_syntax.rb → code_tokens.rb} +35 -33
  7. data/lib/path_coverage/condition.rb +14 -14
  8. data/lib/path_coverage/configuration.rb +7 -4
  9. data/lib/path_coverage/configuration_limits.rb +52 -0
  10. data/lib/path_coverage/configuration_paths.rb +7 -7
  11. data/lib/path_coverage/console_summary.rb +28 -26
  12. data/lib/path_coverage/{site.rb → decision.rb} +2 -2
  13. data/lib/path_coverage/decision_locations.rb +76 -0
  14. data/lib/path_coverage/{site_paths.rb → decision_paths.rb} +13 -11
  15. data/lib/path_coverage/{site_tree.rb → decision_tree.rb} +25 -16
  16. data/lib/path_coverage/fold_branches.rb +18 -0
  17. data/lib/path_coverage/fold_cells.rb +42 -0
  18. data/lib/path_coverage/{html_tree.rb → fold_rows.rb} +43 -72
  19. data/lib/path_coverage/gaps.rb +33 -15
  20. data/lib/path_coverage/json_reader.rb +54 -19
  21. data/lib/path_coverage/json_reports.rb +109 -0
  22. data/lib/path_coverage/json_writer.rb +29 -14
  23. data/lib/path_coverage/kernel.rb +77 -58
  24. data/lib/path_coverage/kinds/base.rb +16 -16
  25. data/lib/path_coverage/kinds/block.rb +16 -15
  26. data/lib/path_coverage/kinds/case.rb +14 -12
  27. data/lib/path_coverage/kinds/chain.rb +40 -11
  28. data/lib/path_coverage/kinds/choice.rb +4 -3
  29. data/lib/path_coverage/kinds/contains.rb +40 -32
  30. data/lib/path_coverage/kinds/safe.rb +38 -26
  31. data/lib/path_coverage/kinds/write.rb +6 -6
  32. data/lib/path_coverage/kinds.rb +2 -2
  33. data/lib/path_coverage/leaving.rb +52 -0
  34. data/lib/path_coverage/loop_forks.rb +57 -0
  35. data/lib/path_coverage/loop_nodes.rb +91 -0
  36. data/lib/path_coverage/manifest.rb +92 -58
  37. data/lib/path_coverage/manifest_blocks.rb +5 -26
  38. data/lib/path_coverage/manifest_case.rb +2 -2
  39. data/lib/path_coverage/manifest_chains.rb +8 -8
  40. data/lib/path_coverage/manifest_choices.rb +23 -20
  41. data/lib/path_coverage/manifest_disabled.rb +97 -0
  42. data/lib/path_coverage/manifest_folding.rb +43 -40
  43. data/lib/path_coverage/manifest_ignoring.rb +6 -6
  44. data/lib/path_coverage/manifest_writes.rb +11 -10
  45. data/lib/path_coverage/measured_by.rb +20 -0
  46. data/lib/path_coverage/merge.rb +7 -7
  47. data/lib/path_coverage/method_loops.rb +172 -0
  48. data/lib/path_coverage/method_statements.rb +150 -134
  49. data/lib/path_coverage/method_ways.rb +170 -0
  50. data/lib/path_coverage/paths_context.rb +104 -0
  51. data/lib/path_coverage/paths_fitting.rb +31 -0
  52. data/lib/path_coverage/paths_forks.rb +72 -0
  53. data/lib/path_coverage/paths_gaps.rb +137 -0
  54. data/lib/path_coverage/paths_leaving.rb +40 -0
  55. data/lib/path_coverage/paths_report.rb +16 -16
  56. data/lib/path_coverage/paths_rescues.rb +146 -0
  57. data/lib/path_coverage/paths_scopes.rb +37 -0
  58. data/lib/path_coverage/paths_tree.rb +203 -0
  59. data/lib/path_coverage/paths_way_lists.rb +72 -0
  60. data/lib/path_coverage/paths_ways.rb +193 -0
  61. data/lib/path_coverage/recorder.rb +71 -36
  62. data/lib/path_coverage/recorder_blocks.rb +17 -17
  63. data/lib/path_coverage/recorder_cases.rb +12 -11
  64. data/lib/path_coverage/recorder_chains.rb +12 -12
  65. data/lib/path_coverage/recorder_writes.rb +9 -9
  66. data/lib/path_coverage/render.rb +58 -51
  67. data/lib/path_coverage/rescue_forks.rb +174 -0
  68. data/lib/path_coverage/result.rb +32 -31
  69. data/lib/path_coverage/rewriter.rb +116 -35
  70. data/lib/path_coverage/rewriter_nested.rb +13 -13
  71. data/lib/path_coverage/runs_report.rb +84 -0
  72. data/lib/path_coverage/source_lines.rb +16 -16
  73. data/lib/path_coverage/source_report.rb +100 -0
  74. data/lib/path_coverage/stable_leaves.rb +162 -0
  75. data/lib/path_coverage/statement_arms.rb +53 -38
  76. data/lib/path_coverage/summary.rb +4 -62
  77. data/lib/path_coverage/totals.rb +12 -10
  78. data/lib/path_coverage/version.rb +1 -1
  79. data/lib/path_coverage/walk.rb +106 -0
  80. data/lib/path_coverage/way_algebras.rb +127 -0
  81. data/lib/path_coverage/ways_report.rb +91 -0
  82. data/lib/path_coverage/ways_totals.rb +46 -0
  83. data/lib/path_coverage/within_methods.rb +152 -97
  84. metadata +39 -30
  85. data/lib/path_coverage/card.rb +0 -14
  86. data/lib/path_coverage/card_tree.rb +0 -186
  87. data/lib/path_coverage/card_ways.rb +0 -140
  88. data/lib/path_coverage/html/file.html +0 -33
  89. data/lib/path_coverage/html/index.html +0 -29
  90. data/lib/path_coverage/html/settings.js +0 -50
  91. data/lib/path_coverage/html/side.js +0 -599
  92. data/lib/path_coverage/html/sort.js +0 -34
  93. data/lib/path_coverage/html/style.css +0 -484
  94. data/lib/path_coverage/html/tabs.js +0 -15
  95. data/lib/path_coverage/html/theme.js +0 -27
  96. data/lib/path_coverage/html_branch.rb +0 -30
  97. data/lib/path_coverage/html_cards.rb +0 -88
  98. data/lib/path_coverage/html_cells.rb +0 -71
  99. data/lib/path_coverage/html_file.rb +0 -123
  100. data/lib/path_coverage/html_index.rb +0 -128
  101. data/lib/path_coverage/html_index_head.rb +0 -29
  102. data/lib/path_coverage/html_labels.rb +0 -16
  103. data/lib/path_coverage/html_methods.rb +0 -66
  104. data/lib/path_coverage/html_nav.rb +0 -67
  105. data/lib/path_coverage/html_report.rb +0 -131
  106. data/lib/path_coverage/html_statements.rb +0 -186
  107. data/lib/path_coverage/html_tabs.rb +0 -26
  108. data/lib/path_coverage/html_template.rb +0 -26
@@ -1,50 +1,78 @@
1
1
  require 'prism'
2
2
  require_relative 'condition'
3
+ require_relative 'leaving'
3
4
  require_relative 'manifest_blocks'
4
5
  require_relative 'manifest_case'
5
6
  require_relative 'manifest_chains'
6
7
  require_relative 'manifest_choices'
8
+ require_relative 'manifest_disabled'
7
9
  require_relative 'manifest_folding'
8
10
  require_relative 'manifest_ignoring'
9
11
  require_relative 'manifest_writes'
10
- require_relative 'site'
12
+ require_relative 'decision'
11
13
 
12
14
  module PathCoverage
13
- # Finds every condition site in source without running it: each
15
+ # Finds every condition decision in source without running it: each
14
16
  # outermost &&, ||, and or or, each ternary, if, unless and case
15
17
  # with a subject, with the conditions nested in it.
16
18
  module Manifest
17
- # { site => Condition } for each site in source, none of
19
+ # { decision => Condition } for each decision in source, none of
18
20
  # them named by an operator in ignored; a call to a method
19
21
  # named in present, as a &.'s receiver, is never nil, and a
20
22
  # call to one named in nonempty, eg any?, is true exactly
21
- # when its receiver has items.
22
- def self.sites(source, path, ignored = [], present = [],
23
- nonempty = [])
23
+ # when its receiver has items. None is on a line not
24
+ # analysed, as ManifestDisabled.lines gives them.
25
+ def self.decisions(source, path, ignored = [], present = [],
26
+ nonempty = [])
24
27
  collector = ConditionSites.new(path, ignored,
25
28
  [present, nonempty])
26
- Prism.parse(source).value.accept(collector)
27
- collector.sites
29
+ parsed = Prism.parse(source)
30
+ parsed.value.accept(collector)
31
+ disabled = ManifestDisabled.lines(source, parsed, path)
32
+ collector.decisions.reject do |name, _|
33
+ disabled.include?(name.split(':')[-2].to_i)
34
+ end
28
35
  end
29
36
 
30
- # The source and sites of the covered file at full_path.
37
+ # The source and decisions of the covered file at full_path.
31
38
  def self.parse(full_path, config)
32
39
  source = File.read(full_path)
33
40
  path = config.relative(full_path)
34
41
  treated = %w[present nonempty].map { config.treated(_1, path) }
35
42
  ignored = config.ignored
36
- { source:, sites: sites(source, path, ignored, *treated) }
43
+ { source:,
44
+ decisions: decisions(source, path, ignored, *treated) }
45
+ end
46
+
47
+ # True for an expression that is no bare argument, so a
48
+ # rewrite wrapping it in another call must parenthesise it: a
49
+ # command call, an and or an or, or a modifier, eg x unless y.
50
+ def self.bare_less?(node)
51
+ return true if command?(node) || modifier?(node)
52
+
53
+ %i[and_node or_node].include?(node.type) &&
54
+ %w[and or].include?(node.operator)
37
55
  end
38
56
 
39
57
  # True for a call to a named method with arguments and no
40
- # parentheses, eg `id.include? 'N'`, but not `id.size == 6`;
41
- # such a call is no bare argument, so a rewrite wrapping it
42
- # in another call must parenthesise it.
58
+ # parentheses, eg `id.include? 'N'`, but not `id.size == 6`.
43
59
  def self.command?(node)
44
60
  node.type == :call_node && node.arguments &&
45
61
  node.opening_loc.nil? && node.name.match?(/\A[a-z_]/i)
46
62
  end
47
63
 
64
+ # True for a modifier if, unless or rescue, eg x if y: one
65
+ # with no end keyword, a ternary being no modifier.
66
+ def self.modifier?(node)
67
+ case node.type
68
+ when :rescue_modifier_node then true
69
+ when :unless_node then node.end_keyword_loc.nil?
70
+ when :if_node
71
+ !node.if_keyword_loc.nil? && node.end_keyword_loc.nil?
72
+ else false
73
+ end
74
+ end
75
+
48
76
  # Literals and lambdas, whose value is truthy by their form;
49
77
  # a constant is not among them, as it can hold nil or false.
50
78
  TRUTHY_TYPES = %i[
@@ -82,7 +110,7 @@ module PathCoverage
82
110
  TRUTHY_GLOBALS.include?(node.name)
83
111
  end
84
112
 
85
- # Collects each site and its Condition.
113
+ # Collects each decision and its Condition.
86
114
  class ConditionSites < Prism::Visitor
87
115
  include ManifestBlocks
88
116
  include ManifestCase
@@ -92,7 +120,7 @@ module PathCoverage
92
120
  include ManifestIgnoring
93
121
  include ManifestWrites
94
122
 
95
- # Starts with no sites found, to find none named by an
123
+ # Starts with no decisions found, to find none named by an
96
124
  # operator in ignored, and to take a call to a method named
97
125
  # in present as never nil, and to one in nonempty as true
98
126
  # exactly when its receiver has items.
@@ -101,72 +129,72 @@ module PathCoverage
101
129
  @ignored = ignored
102
130
  @present = present
103
131
  @nonempty = nonempty
104
- @sites = {}
132
+ @decisions = {}
105
133
  @notes = nil
106
134
  @inner = nil
107
135
  super()
108
136
  end
109
137
 
110
- # The sites found so far.
111
- attr_reader :sites
138
+ # The decisions found so far.
139
+ attr_reader :decisions
112
140
 
113
141
  OPERATOR_TYPES = %i[and_node or_node].freeze
114
142
 
115
- # Notes an && or and site.
143
+ # Notes an && or and decision.
116
144
  def visit_and_node(node)
117
- add_site(node)
145
+ add_decision(node)
118
146
  end
119
147
 
120
- # Notes an || or or site.
148
+ # Notes an || or or decision.
121
149
  def visit_or_node(node)
122
- add_site(node)
150
+ add_decision(node)
123
151
  end
124
152
 
125
- # Notes a ternary or an if site, a modifier if included,
153
+ # Notes a ternary or an if decision, a modifier if included,
126
154
  # its arms included.
127
155
  def visit_if_node(node)
128
- add_site(node)
156
+ add_decision(node)
129
157
  end
130
158
 
131
- # Notes an unless site, a modifier unless included, its
159
+ # Notes an unless decision, a modifier unless included, its
132
160
  # arms included.
133
161
  def visit_unless_node(node)
134
- add_site(node)
162
+ add_decision(node)
135
163
  end
136
164
 
137
- # Notes a case site, when its paths can be seen; any other
138
- # case is no site, though a condition inside it may be.
165
+ # Notes a case decision, when its paths can be seen; any other
166
+ # case is no decision, though a condition inside it may be.
139
167
  def visit_case_node(node)
140
- return super unless case_site?(node)
168
+ return super unless case_decision?(node)
141
169
 
142
- add_site(node)
170
+ add_decision(node)
143
171
  end
144
172
 
145
- # Notes a &. chain as a site, or a block method's call, when
173
+ # Notes a &. chain as a decision, or a block method's call, when
146
174
  # its block's every call can be seen; any other call is no
147
- # site, though a condition inside it may be.
175
+ # decision, though a condition inside it may be.
148
176
  def visit_call_node(node)
149
- return add_site(node) if safe_chain?(node)
150
- return super unless block_site?(node)
177
+ return add_decision(node) if safe_chain?(node)
178
+ return super unless block_decision?(node)
151
179
 
152
- add_site(node)
180
+ add_decision(node)
153
181
  end
154
182
 
155
- # Notes node as a site, named where it starts: an if or an
183
+ # Notes node as a decision, named where it starts: an if or an
156
184
  # unless at its keyword. It claims its place first, so a
157
- # site found inside one of its leaves or arms follows it.
158
- # While a leaf is being visited, any site is collected as
185
+ # decision found inside one of its leaves or arms follows it.
186
+ # While a leaf is being visited, any decision is collected as
159
187
  # part of that leaf instead, as it is part of one expression.
160
- def add_site(node)
188
+ def add_decision(node)
161
189
  return visit_child_nodes(node) if ignored?(node)
162
190
  return @inner << nested_condition_of(node) if @inner
163
191
 
164
- site = Site.at(@path, site_loc(node))
165
- @sites[site] = nil
192
+ decision = Decision.at(@path, decision_loc(node))
193
+ @decisions[decision] = nil
166
194
  outer = @notes
167
195
  @notes = { commands: [], fixed: {} }
168
196
  condition = scoped { condition_of(node) }
169
- @sites[site] = condition.with(**@notes)
197
+ @decisions[decision] = condition.with(**@notes)
170
198
  @notes = outer
171
199
  end
172
200
 
@@ -190,20 +218,26 @@ module PathCoverage
190
218
 
191
219
  # node's operands: a chain of the same operator flattened
192
220
  # into one list, the other operator nested as a Condition,
193
- # as is a &. chain, its value its last leaf, anything else a
194
- # leaf's byte range.
221
+ # as is a &. chain, its value its last leaf, an operand that
222
+ # leaves the method an Arm, eg return t in x or return t,
223
+ # anything else a leaf's byte range.
195
224
  def operands_of(node)
196
- [node.left, node.right].flat_map do |written|
197
- operand = grouped(written)
198
- next [leaf(operand)] if ignored?(operand)
199
- next operands_of(operand) if operand.type == node.type
200
- next [condition_of(operand)] if operator?(operand)
201
- if safe_chain?(operand)
202
- next [chain_condition(operand, value: true)]
203
- end
204
-
205
- [leaf(operand)]
225
+ [node.left, node.right].flat_map { operands_in(_1, node) }
226
+ end
227
+
228
+ # The operands written, one side of node, stands for, as
229
+ # operands_of gives them.
230
+ def operands_in(written, node)
231
+ operand = grouped(written)
232
+ return [leaf(operand)] if ignored?(operand)
233
+ return [Leaving.arm(operand)] if Leaving.word(operand)
234
+ return operands_of(operand) if operand.type == node.type
235
+ return [condition_of(operand)] if operator?(operand)
236
+ if safe_chain?(operand)
237
+ return [chain_condition(operand, value: true)]
206
238
  end
239
+
240
+ [leaf(operand)]
207
241
  end
208
242
 
209
243
  # The byte range of node as written.
@@ -217,14 +251,14 @@ module PathCoverage
217
251
  OPERATOR_TYPES.include?(node.type)
218
252
  end
219
253
 
220
- # The byte range of a leaf operand, noted among its site's
221
- # commands and fixed leaves where it is one; or, when sites
254
+ # The byte range of a leaf operand, noted among its decision's
255
+ # commands and fixed leaves where it is one; or, when decisions
222
256
  # that fold are found inside it, the leaf with them nested.
223
257
  def leaf(operand)
224
- inner = leaf_sites(operand)
258
+ inner = leaf_decisions(operand)
225
259
  loc = operand.location
226
260
  range = loc.start_offset...loc.end_offset
227
- @notes[:commands] << range if Manifest.command?(operand)
261
+ @notes[:commands] << range if Manifest.bare_less?(operand)
228
262
  outcome = Manifest.fixed_outcome(operand) ||
229
263
  given_outcome(operand)
230
264
  @notes[:fixed][range] = outcome if outcome
@@ -1,7 +1,7 @@
1
1
  require_relative 'condition'
2
2
 
3
3
  module PathCoverage
4
- # The part of the manifest's visitor that builds the site of a
4
+ # The part of the manifest's visitor that builds the decision of a
5
5
  # call to a block method, one that calls its block an item at
6
6
  # a time, until it decides, eg any?, or for every item, eg
7
7
  # select: its leaves are its receiver, whose items the block
@@ -14,14 +14,14 @@ module PathCoverage
14
14
  # make it ignore its block, not by &., which skips it for
15
15
  # nil, and with a block of one expression that is a value,
16
16
  # so can be wrapped: not break, next or return.
17
- def block_site?(node)
17
+ def block_decision?(node)
18
18
  return false unless BLOCK_PATHS.key?(node.name.to_s)
19
19
 
20
20
  plain_call?(node) && value?(block_expression(node.block))
21
21
  end
22
22
 
23
23
  # True for a call on a receiver, with no arguments. A call by
24
- # &. never gets here, as it is a site of its own first.
24
+ # &. never gets here, as it is a decision of its own first.
25
25
  def plain_call?(node)
26
26
  !node.receiver.nil? && node.arguments.nil?
27
27
  end
@@ -60,36 +60,15 @@ module PathCoverage
60
60
  )
61
61
  end
62
62
 
63
- # The byte range of block's one expression, any site in it
63
+ # The byte range of block's one expression, any decision in it
64
64
  # visited as one of its own, as the block runs once per item;
65
65
  # noted among the commands when it is no bare argument.
66
66
  def block_body(block)
67
67
  body = block_expression(block)
68
68
  scoped { body.accept(self) }
69
69
  range = extent_of(body)
70
- @notes[:commands] << range if bare_less?(body)
70
+ @notes[:commands] << range if Manifest.bare_less?(body)
71
71
  range
72
72
  end
73
-
74
- # True for an expression that is no bare argument: a command
75
- # call, an and or an or, or a modifier, eg x unless y.
76
- def bare_less?(node)
77
- return true if Manifest.command?(node) || modifier?(node)
78
-
79
- %i[and_node or_node].include?(node.type) &&
80
- %w[and or].include?(node.operator)
81
- end
82
-
83
- # True for a modifier if, unless or rescue, eg x if y: one
84
- # with no end keyword, a ternary being no modifier.
85
- def modifier?(node)
86
- case node.type
87
- when :rescue_modifier_node then true
88
- when :unless_node then node.end_keyword_loc.nil?
89
- when :if_node
90
- !node.if_keyword_loc.nil? && node.end_keyword_loc.nil?
91
- else false
92
- end
93
- end
94
73
  end
95
74
  end
@@ -1,7 +1,7 @@
1
1
  require_relative 'condition'
2
2
 
3
3
  module PathCoverage
4
- # The part of the manifest's visitor that builds a case site,
4
+ # The part of the manifest's visitor that builds a case decision,
5
5
  # named case: its arms are its whens, each holding a when
6
6
  # condition of its patterns, and its else. It relies on its
7
7
  # includer's extent_of and statement_arm.
@@ -10,7 +10,7 @@ module PathCoverage
10
10
  # subject, as each pattern is then matched against it, and
11
11
  # no splat among its patterns, whose items cannot each be
12
12
  # stood in for.
13
- def case_site?(node)
13
+ def case_decision?(node)
14
14
  return false if node.predicate.nil?
15
15
 
16
16
  patterns = node.conditions.flat_map(&:conditions)
@@ -1,7 +1,7 @@
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
  # safe-navigation chains, eg user&.account&.owner. It relies
6
6
  # on its includer's extent_of.
7
7
  module ManifestChains
@@ -23,7 +23,7 @@ module PathCoverage
23
23
  chain_calls(node).any?(&:safe_navigation?)
24
24
  end
25
25
 
26
- # A call that is a site as a Condition: a &. chain's, or a
26
+ # A call that is a decision as a Condition: a &. chain's, or a
27
27
  # block method call's. It relies on its includer's
28
28
  # block_condition.
29
29
  def call_condition(node)
@@ -36,23 +36,23 @@ module PathCoverage
36
36
  # receivers of its &. calls, innermost first, each nil or
37
37
  # not nil, and, with value, the whole chain's value last, as
38
38
  # when it is an operand of an && or an if's predicate. Any
39
- # site in a call's arguments or block, or in the chain's
40
- # first receiver, is a site of its own. A chain that is no
39
+ # decision in a call's arguments or block, or in the chain's
40
+ # first receiver, is a decision of its own. A chain that is no
41
41
  # bare argument, eg a command call, is noted among the
42
- # site's commands, to be parenthesised.
42
+ # decision's commands, to be parenthesised.
43
43
  def chain_condition(node, value:)
44
44
  calls = chain_calls(node)
45
45
  visit_chain(calls)
46
46
  safe = calls.select(&:safe_navigation?).reverse
47
47
  receivers = safe.map { |call| receiver_of(call) }
48
48
  extent = extent_of(node)
49
- @notes[:commands] << extent if Manifest.command?(node)
49
+ @notes[:commands] << extent if Manifest.bare_less?(node)
50
50
  operands = value ? [*receivers, extent] : receivers
51
51
  Condition.new(operator: SAFE, operands:, extent:)
52
52
  end
53
53
 
54
54
  # The byte range of a &. call's receiver, noted among its
55
- # site's fixed leaves as :present, never nil, when it is a
55
+ # decision's fixed leaves as :present, never nil, when it is a
56
56
  # call to a method treated as present.
57
57
  def receiver_of(call)
58
58
  receiver = call.receiver
@@ -63,7 +63,7 @@ module PathCoverage
63
63
  range
64
64
  end
65
65
 
66
- # Visits the sites inside a chain's calls' arguments and
66
+ # Visits the decisions inside a chain's calls' arguments and
67
67
  # blocks, and inside its first receiver, eg (a || b) in
68
68
  # (a || b)&.size, but never a call of the chain itself.
69
69
  def visit_chain(calls)
@@ -1,7 +1,8 @@
1
1
  require_relative 'condition'
2
+ require_relative 'leaving'
2
3
 
3
4
  module PathCoverage
4
- # The part of the manifest's visitor that builds the sites
5
+ # The part of the manifest's visitor that builds the decisions
5
6
  # whose predicate picks an arm: a ternary, named ?:, an if,
6
7
  # named if, and an unless, named unless. It relies on its
7
8
  # includer's condition_of, extent_of, grouped, leaf and
@@ -11,12 +12,12 @@ module PathCoverage
11
12
  # being an if_node with no if keyword.
12
13
  CHOICE_TYPES = %i[if_node unless_node].freeze
13
14
 
14
- # Where node's site is named: a &. chain's start; a block
15
+ # Where node's decision is named: a &. chain's start; a block
15
16
  # method's call at its method; a ternary at its ?, so it never
16
- # shares the name of a site starting where it does, eg its
17
+ # shares the name of a decision starting where it does, eg its
17
18
  # predicate; an if's or an unless's keyword, which a
18
19
  # modifier's statement comes before; anything else's own start.
19
- def site_loc(node)
20
+ def decision_loc(node)
20
21
  return node.location if safe_chain?(node)
21
22
  return node.message_loc if node.type == :call_node
22
23
  return node.location unless CHOICE_TYPES.include?(node.type)
@@ -72,16 +73,18 @@ module PathCoverage
72
73
  end
73
74
 
74
75
  # An if's arm, statements its code, which never folds into
75
- # the if's site; as a new scope, any site inside it is a site
76
- # of its own. An arm with no statements has no extent.
76
+ # the if's decision; as a new scope, any decision inside it
77
+ # is a decision of its own. An arm with no statements has no
78
+ # extent.
77
79
  def statement_arm(kind, statements)
78
80
  scoped { statements&.accept(self) }
79
81
  extent = statements ? extent_of(statements) : nil
80
- Arm.new(kind:, extent:, body: nil)
82
+ leaves = Leaving.of(statements)
83
+ Arm.new(kind:, extent:, body: nil, leaves:)
81
84
  end
82
85
 
83
86
  # An if's else arm, from subsequent: an else's statements,
84
- # or an elsif, which is a site of its own; none for none.
87
+ # or an elsif, which is a decision of its own; none for none.
85
88
  def else_arm(subsequent)
86
89
  return [] if subsequent.nil?
87
90
  if subsequent.type == :else_node
@@ -90,7 +93,7 @@ module PathCoverage
90
93
 
91
94
  scoped { subsequent.accept(self) }
92
95
  extent = extent_of(subsequent)
93
- [Arm.new(kind: :else, extent:, body: nil)]
96
+ [Arm.new(kind: :else, extent:, body: nil, leaves: nil)]
94
97
  end
95
98
 
96
99
  # A ternary's predicate, then its then and else arms.
@@ -104,32 +107,32 @@ module PathCoverage
104
107
 
105
108
  # One arm of a ternary, statements its code: a ternary, or a
106
109
  # block method's call whose value follows from its path, is a
107
- # site of its own, as an if's arm holding an if is, so the arm
110
+ # decision of its own, as an if's arm holding an if is, so the arm
108
111
  # is plain; another condition folds in as its body, being part
109
- # of the one expression, as does a plain arm with sites inside
112
+ # of the one expression, as does a plain arm with decisions inside
110
113
  # it, its value its last leaf; a plain arm with none has no
111
114
  # body.
112
115
  def arm_of(kind, statements)
113
116
  written = grouped(statements.body.first)
114
117
  extent = extent_of(statements)
115
118
  body = if ternary?(written) || valued?(written)
116
- own_site(statements)
119
+ own_decision(statements)
117
120
  else
118
121
  arm_body(written, statements, extent)
119
122
  end
120
- Arm.new(kind:, extent:, body:)
123
+ Arm.new(kind:, extent:, body:, leaves: Leaving.of(statements))
121
124
  end
122
125
 
123
- # No body, for an arm, statements its code, whose one site is
124
- # a site of its own: visited in a new scope, so noted as one.
125
- def own_site(statements)
126
+ # No body, for an arm, statements its code, whose one decision is
127
+ # a decision of its own: visited in a new scope, so noted as one.
128
+ def own_decision(statements)
126
129
  scoped { statements.accept(self) }
127
130
  nil
128
131
  end
129
132
 
130
133
  # The body of a ternary's arm, statements its code, written its
131
134
  # one expression, extent its byte range: the condition written
132
- # is, when it folds; else the sites inside it, contained, nil
135
+ # is, when it folds; else the decisions inside it, contained, nil
133
136
  # for none.
134
137
  def arm_body(written, statements, extent)
135
138
  return condition_of(written) if foldable?(written)
@@ -138,7 +141,7 @@ module PathCoverage
138
141
  inner.empty? ? nil : contained(inner, extent)
139
142
  end
140
143
 
141
- # True for a condition that folds into a ternary's site: an
144
+ # True for a condition that folds into a ternary's decision: an
142
145
  # &&, ||, and or or not ignored.
143
146
  def foldable?(node)
144
147
  operator?(node) && !ignored?(node)
@@ -151,8 +154,8 @@ module PathCoverage
151
154
 
152
155
  # A ternary's or an if's predicate as its operand: a &. chain
153
156
  # nested as a Condition, its value its last leaf; anything
154
- # else a leaf, an && or || among them, as a site of its own,
155
- # as leaf_sites has it.
157
+ # else a leaf, an && or || among them, as a decision of its own,
158
+ # as leaf_decisions has it.
156
159
  def predicate_of(node)
157
160
  predicate = grouped(node.predicate)
158
161
  return leaf(predicate) if ignored?(predicate)
@@ -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