snoot 0.1.0 → 0.2.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/CHANGELOG.md +89 -1
- data/README.md +82 -3
- data/lib/snoot/analyse_run/decision.rb +19 -4
- data/lib/snoot/analyse_run/result.rb +5 -2
- data/lib/snoot/analyse_run.rb +1 -1
- data/lib/snoot/analyser_orchestration/analyser_runs.rb +63 -0
- data/lib/snoot/analyser_orchestration/default.rb +64 -62
- data/lib/snoot/analyser_orchestration/failure_message.rb +26 -0
- data/lib/snoot/analyser_orchestration/result_mapping.rb +61 -17
- data/lib/snoot/analyser_orchestration.rb +2 -1
- data/lib/snoot/cli/argv.rb +18 -0
- data/lib/snoot/cli/internal_error_report.rb +18 -0
- data/lib/snoot/cli/status_line.rb +40 -0
- data/lib/snoot/cli/usage.rb +53 -0
- data/lib/snoot/cli.rb +59 -52
- data/lib/snoot/findings.rb +8 -6
- data/lib/snoot/render_report/complexity_sections.rb +69 -0
- data/lib/snoot/render_report/duplication_sections.rb +58 -0
- data/lib/snoot/render_report/smell_sections.rb +65 -0
- data/lib/snoot/render_report/source_excerpt.rb +66 -0
- data/lib/snoot/render_report.rb +43 -48
- data/lib/snoot/value_types.rb +26 -0
- data/lib/snoot/version.rb +1 -1
- data/lib/snoot.rb +6 -0
- data/snoot.allium +259 -46
- metadata +11 -1
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
module Snoot
|
|
4
4
|
# The duck-typed orchestration contract the CLI demands -- any object
|
|
5
|
-
# responding to these
|
|
5
|
+
# responding to these six methods qualifies (no base class to inherit):
|
|
6
6
|
#
|
|
7
7
|
# vendored_doc(smell_type) -> String?
|
|
8
8
|
# significant_smells(smells) -> Set<Smell>
|
|
9
9
|
# significant_complexities(complexities) -> Set<ComplexityHit>
|
|
10
10
|
# significant_duplications(duplications) -> Set<DuplicationCluster>
|
|
11
|
+
# significance_cutoffs -> SignificanceCutoffs
|
|
11
12
|
# analyse(paths) -> Sources | AnalyserFailure
|
|
12
13
|
#
|
|
13
14
|
# Each call is pure within a single CLI invocation (see snoot.allium's
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Snoot
|
|
4
|
+
module CLI
|
|
5
|
+
# argv split into options and paths. An argument beginning with "-"
|
|
6
|
+
# is an option unless it follows "--", which ends options and is
|
|
7
|
+
# itself consumed.
|
|
8
|
+
Argv = Data.define(:options, :paths) do
|
|
9
|
+
def self.parse(argv)
|
|
10
|
+
separator = argv.index("--")
|
|
11
|
+
leading = separator ? argv.take(separator) : argv
|
|
12
|
+
trailing = separator ? argv.drop(separator + 1) : []
|
|
13
|
+
options, paths = leading.partition { |arg| arg.start_with?("-") }
|
|
14
|
+
new(options: options, paths: paths + trailing)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Snoot
|
|
4
|
+
module CLI
|
|
5
|
+
# What an invocation cut short by an unexpected error writes: the
|
|
6
|
+
# error and its backtrace on stderr, the internal_error status line
|
|
7
|
+
# on stdout.
|
|
8
|
+
module InternalErrorReport
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def write(pipeline, error)
|
|
12
|
+
pipeline.stderr.write("snoot: internal error: #{error.class}: #{error.message}\n",
|
|
13
|
+
*error.backtrace&.map { |frame| " #{frame}\n" })
|
|
14
|
+
pipeline.stdout.write(StatusLine.internal_error(error))
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Snoot
|
|
4
|
+
module CLI
|
|
5
|
+
# The last stdout line of every accepted invocation: a fixed prefix,
|
|
6
|
+
# the outcome name, then key=value fields whose values never contain
|
|
7
|
+
# spaces, so a script can split it without parsing the report.
|
|
8
|
+
module StatusLine
|
|
9
|
+
PREFIX = "snoot-status: "
|
|
10
|
+
CATEGORIES = { Smell => "smell", DuplicationCluster => "duplication", ComplexityHit => "complexity" }.freeze
|
|
11
|
+
|
|
12
|
+
module_function
|
|
13
|
+
|
|
14
|
+
def for_run(run, remaining)
|
|
15
|
+
case run.outcome
|
|
16
|
+
when :finding_rendered then line(:finding_rendered, **finding_fields(run.selected_finding, remaining))
|
|
17
|
+
when :analysis_failed then line(:analysis_failed, analyser: run.failure.analyser)
|
|
18
|
+
else line(run.outcome)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def internal_error(error)
|
|
23
|
+
line(:internal_error, error: error.class.name)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def finding_fields(finding, remaining)
|
|
27
|
+
type = finding.smell_type.name if finding.is_a?(Smell)
|
|
28
|
+
{ category: CATEGORIES.fetch(finding.class), type:, remaining: remaining_total(remaining) }.compact
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def remaining_total(remaining)
|
|
32
|
+
remaining.smell_types + remaining.duplication_clusters + remaining.complexity_hits
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def line(outcome, **fields)
|
|
36
|
+
[PREFIX + outcome.to_s, *fields.map { |key, value| "#{key}=#{value}" }].join(" ") << "\n"
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Snoot
|
|
4
|
+
module CLI
|
|
5
|
+
USAGE = <<~HELP.freeze
|
|
6
|
+
snoot - single-finding reek/flog/flay reporter
|
|
7
|
+
|
|
8
|
+
Usage: snoot [--] [paths...]
|
|
9
|
+
snoot --version
|
|
10
|
+
snoot --help
|
|
11
|
+
|
|
12
|
+
With no path arguments, snoot scans the current directory. Arguments
|
|
13
|
+
after -- are always paths. --help and --version print and exit 0
|
|
14
|
+
whatever else is given.
|
|
15
|
+
|
|
16
|
+
How snoot picks the finding:
|
|
17
|
+
One finding per run; a smell finding lists every instance of its type
|
|
18
|
+
across the scanned files. Fix them, then rerun snoot to see the next one.
|
|
19
|
+
Running reek, flog or flay directly skips these rules and lists a different set.
|
|
20
|
+
Smells outrank duplication, and duplication outranks complexity.
|
|
21
|
+
Within a category the largest wins: most instances, most locations, or highest score.
|
|
22
|
+
Ties break by name, then path, then line.
|
|
23
|
+
#{RenderReport.describe_cutoffs(AnalyserOrchestration::Default.significance_cutoffs)};
|
|
24
|
+
flay's own mass threshold (16) still applies.
|
|
25
|
+
.reek.yml (searched from the current directory upward, then ~) configures reek only.
|
|
26
|
+
.flayignore (current directory; dir/**/*.rb for a directory) excludes files from flay;
|
|
27
|
+
flog scans every file.
|
|
28
|
+
The output names smell types still below the cutoff, so a type shows before it counts.
|
|
29
|
+
A smell report says how to set that detector's options under detectors: in .reek.yml.
|
|
30
|
+
|
|
31
|
+
Exit codes:
|
|
32
|
+
0 nothing to report
|
|
33
|
+
1 one finding rendered
|
|
34
|
+
2 analyser failure
|
|
35
|
+
64 usage error (unknown flag or missing path)
|
|
36
|
+
70 internal error (a bug in snoot; details on stderr)
|
|
37
|
+
|
|
38
|
+
Output:
|
|
39
|
+
The last stdout line of every run is a status line for scripts:
|
|
40
|
+
snoot-status: <outcome> [key=value...], values never containing spaces.
|
|
41
|
+
snoot-status: finding_rendered category=smell type=FeatureEnvy remaining=4
|
|
42
|
+
snoot-status: finding_rendered category=complexity remaining=0
|
|
43
|
+
snoot-status: nothing_to_report
|
|
44
|
+
snoot-status: analysis_failed analyser=reek
|
|
45
|
+
snoot-status: internal_error error=NoMethodError
|
|
46
|
+
remaining counts the significant findings behind this one.
|
|
47
|
+
Usage errors, --help and --version print no status line.
|
|
48
|
+
|
|
49
|
+
Example:
|
|
50
|
+
snoot lib/
|
|
51
|
+
HELP
|
|
52
|
+
end
|
|
53
|
+
end
|
data/lib/snoot/cli.rb
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "snoot/cli/argv"
|
|
3
4
|
require "snoot/cli/event"
|
|
4
5
|
require "snoot/cli/pipeline"
|
|
6
|
+
require "snoot/cli/status_line"
|
|
7
|
+
require "snoot/cli/internal_error_report"
|
|
8
|
+
require "snoot/cli/usage"
|
|
5
9
|
|
|
6
10
|
module Snoot
|
|
7
11
|
# The CLI surface. cli.rb owns the surface entry points: argv-shape
|
|
@@ -10,9 +14,13 @@ module Snoot
|
|
|
10
14
|
# the IO emitter helpers (emit_warnings, emit_failure,
|
|
11
15
|
# emit_nothing_to_report, format_report). The event values (RunInvoked,
|
|
12
16
|
# ReportEmitted) live in cli/event.rb; the Pipeline value lives in
|
|
13
|
-
# cli/pipeline.rb
|
|
14
|
-
#
|
|
15
|
-
#
|
|
17
|
+
# cli/pipeline.rb; the USAGE banner lives in cli/usage.rb; cli/argv.rb
|
|
18
|
+
# splits argv into options and paths. .run prints a BANNERS entry when
|
|
19
|
+
# any option names one, rejects unknown options and missing paths with
|
|
20
|
+
# exit 64, and otherwise threads the paths through .run_invoked,
|
|
21
|
+
# mapping the terminal outcome to an EXIT_CODES integer. Every run ends
|
|
22
|
+
# stdout with its cli/status_line.rb line; an unexpected error escaping
|
|
23
|
+
# the run is written by cli/internal_error_report.rb and exits 70.
|
|
16
24
|
#
|
|
17
25
|
# The two entry points return deliberately different shapes. .run is
|
|
18
26
|
# the POSIX boundary: argv -> Integer, consumed by `exit
|
|
@@ -23,34 +31,11 @@ module Snoot
|
|
|
23
31
|
# so the integer is a lossy view of the Run; callers that need the Run,
|
|
24
32
|
# the events, or both must use .run_invoked.
|
|
25
33
|
module CLI
|
|
26
|
-
NOTHING_TO_REPORT = "nothing to report -- no findings above snoot's significance floor
|
|
34
|
+
NOTHING_TO_REPORT = "nothing to report -- no findings above snoot's significance floor"
|
|
27
35
|
DEFAULT_PATHS = Set[Snoot::Path.new(raw: ".")].freeze
|
|
28
36
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
Usage: snoot [paths...]
|
|
33
|
-
snoot --version
|
|
34
|
-
snoot --help
|
|
35
|
-
|
|
36
|
-
With no path arguments, snoot scans the current directory.
|
|
37
|
-
|
|
38
|
-
Exit codes:
|
|
39
|
-
0 nothing to report
|
|
40
|
-
1 one finding rendered
|
|
41
|
-
2 analyser failure
|
|
42
|
-
64 usage error
|
|
43
|
-
|
|
44
|
-
Example:
|
|
45
|
-
snoot lib/
|
|
46
|
-
HELP
|
|
47
|
-
|
|
48
|
-
EXIT_CODES = {
|
|
49
|
-
finding_rendered: 1,
|
|
50
|
-
nothing_to_report: 0,
|
|
51
|
-
analysis_failed: 2,
|
|
52
|
-
usage_error: 64
|
|
53
|
-
}.freeze
|
|
37
|
+
EXIT_CODES = { finding_rendered: 1, nothing_to_report: 0, analysis_failed: 2, usage_error: 64,
|
|
38
|
+
internal_error: 70 }.freeze
|
|
54
39
|
|
|
55
40
|
BANNERS = {
|
|
56
41
|
%w[--version] => "snoot #{Snoot::VERSION}\n",
|
|
@@ -59,8 +44,10 @@ module Snoot
|
|
|
59
44
|
|
|
60
45
|
module_function
|
|
61
46
|
|
|
62
|
-
def emit_nothing_to_report(
|
|
63
|
-
|
|
47
|
+
def emit_nothing_to_report(remaining, pipeline)
|
|
48
|
+
below_cutoff = RenderReport.describe_below_cutoff(remaining.below_cutoff_smell_types)
|
|
49
|
+
cutoffs = RenderReport.describe_cutoffs(pipeline.orchestration.significance_cutoffs)
|
|
50
|
+
pipeline.stdout.write("#{NOTHING_TO_REPORT}; #{below_cutoff} (#{cutoffs})\n")
|
|
64
51
|
[]
|
|
65
52
|
end
|
|
66
53
|
|
|
@@ -83,17 +70,40 @@ module Snoot
|
|
|
83
70
|
end
|
|
84
71
|
|
|
85
72
|
def run(argv, pipeline: Pipeline.default)
|
|
86
|
-
|
|
73
|
+
parsed = Argv.parse(argv)
|
|
74
|
+
banner = lookup_banner(parsed.options)
|
|
87
75
|
return write_and_return(pipeline.stdout, banner, 0) if banner
|
|
88
|
-
return write_and_return(pipeline.stderr, USAGE, EXIT_CODES.fetch(:usage_error)) if unknown_flag?(argv)
|
|
89
76
|
|
|
90
|
-
|
|
77
|
+
rejection = rejection_message(parsed.options, missing_paths(parsed.paths))
|
|
78
|
+
return usage_error(pipeline, rejection) unless rejection.empty?
|
|
79
|
+
|
|
80
|
+
run_pipeline(parsed.paths, pipeline: pipeline)
|
|
81
|
+
rescue StandardError => error
|
|
82
|
+
InternalErrorReport.write(pipeline, error)
|
|
83
|
+
EXIT_CODES.fetch(:internal_error)
|
|
91
84
|
end
|
|
92
85
|
|
|
93
|
-
def lookup_banner(
|
|
94
|
-
|
|
86
|
+
def lookup_banner(options)
|
|
87
|
+
options.each do |option|
|
|
88
|
+
banner = BANNERS.find { |flags, _| flags.include?(option) }&.last
|
|
89
|
+
return banner if banner
|
|
90
|
+
end
|
|
91
|
+
nil
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def rejection_message(unknown_options, missing)
|
|
95
|
+
lines = unknown_options.map { |option| "snoot: unknown option: #{option}\n" }
|
|
96
|
+
lines.concat(missing.map { |raw| "snoot: no such file or directory: #{raw}\n" })
|
|
97
|
+
lines << "Try 'snoot --help' for usage.\n" if unknown_options.any?
|
|
98
|
+
lines.join
|
|
99
|
+
end
|
|
95
100
|
|
|
96
|
-
|
|
101
|
+
def usage_error(pipeline, message)
|
|
102
|
+
write_and_return(pipeline.stderr, message, EXIT_CODES.fetch(:usage_error))
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def missing_paths(raws)
|
|
106
|
+
raws.reject { |raw| File.exist?(raw) }
|
|
97
107
|
end
|
|
98
108
|
|
|
99
109
|
def write_and_return(io, message, code)
|
|
@@ -101,17 +111,13 @@ module Snoot
|
|
|
101
111
|
code
|
|
102
112
|
end
|
|
103
113
|
|
|
104
|
-
def
|
|
105
|
-
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
def run_pipeline(argv, pipeline:)
|
|
109
|
-
run, _events = run_invoked(build_paths(argv), pipeline: pipeline)
|
|
114
|
+
def run_pipeline(raws, pipeline:)
|
|
115
|
+
run, _events = run_invoked(build_paths(raws), pipeline: pipeline)
|
|
110
116
|
EXIT_CODES.fetch(run.outcome)
|
|
111
117
|
end
|
|
112
118
|
|
|
113
|
-
def build_paths(
|
|
114
|
-
|
|
119
|
+
def build_paths(raws)
|
|
120
|
+
raws.each_with_object(Set[]) { |raw, set| set << Path.new(raw: raw) }
|
|
115
121
|
end
|
|
116
122
|
|
|
117
123
|
def run_invoked(paths, pipeline: Pipeline.default)
|
|
@@ -121,24 +127,25 @@ module Snoot
|
|
|
121
127
|
|
|
122
128
|
def collect_events(paths, pipeline)
|
|
123
129
|
AnalyseRun.invoke(paths, orchestration: pipeline.orchestration) =>
|
|
124
|
-
{ run:, events: analyse_events, smells: }
|
|
130
|
+
{ run:, events: analyse_events, smells:, remaining: }
|
|
125
131
|
emit_warnings(analyse_events, pipeline.stderr)
|
|
126
132
|
events = [RunInvoked.new(paths: paths), *analyse_events,
|
|
127
|
-
*events_for_outcome(run, smells, pipeline: pipeline)]
|
|
133
|
+
*events_for_outcome(run, smells, remaining, pipeline: pipeline)]
|
|
134
|
+
pipeline.stdout.write(StatusLine.for_run(run, remaining))
|
|
128
135
|
[run, events]
|
|
129
136
|
end
|
|
130
137
|
|
|
131
|
-
def events_for_outcome(run, smells, pipeline:)
|
|
138
|
+
def events_for_outcome(run, smells, remaining, pipeline:)
|
|
132
139
|
case run.outcome
|
|
133
|
-
when :finding_rendered then [emit_report(run, smells, pipeline: pipeline)]
|
|
134
|
-
when :nothing_to_report then emit_nothing_to_report(pipeline
|
|
140
|
+
when :finding_rendered then [emit_report(run, smells, remaining, pipeline: pipeline)]
|
|
141
|
+
when :nothing_to_report then emit_nothing_to_report(remaining, pipeline)
|
|
135
142
|
when :analysis_failed then emit_failure(run, pipeline.stderr)
|
|
136
143
|
else []
|
|
137
144
|
end
|
|
138
145
|
end
|
|
139
146
|
|
|
140
|
-
def emit_report(run, smells, pipeline:)
|
|
141
|
-
RenderReport.invoke(run, smells: smells, orchestration: pipeline.orchestration) =>
|
|
147
|
+
def emit_report(run, smells, remaining, pipeline:)
|
|
148
|
+
RenderReport.invoke(run, smells: smells, remaining: remaining, orchestration: pipeline.orchestration) =>
|
|
142
149
|
{ sections:, finding: }
|
|
143
150
|
pipeline.stdout.write(format_report(sections))
|
|
144
151
|
ReportEmitted.new(run: run, finding: finding, sections: sections)
|
data/lib/snoot/findings.rb
CHANGED
|
@@ -7,17 +7,19 @@ module Snoot
|
|
|
7
7
|
# shapes; AnalyseRun's selection phase ranks within and across them.
|
|
8
8
|
|
|
9
9
|
# A single Reek smell instance. Ranked by how often its smell_type
|
|
10
|
-
# recurs within a Run.
|
|
11
|
-
Smell = Data.define(:smell_type, :location, :message)
|
|
10
|
+
# recurs within a Run. source_lines holds every line reek flagged.
|
|
11
|
+
Smell = Data.define(:smell_type, :location, :message, :source_lines)
|
|
12
12
|
|
|
13
13
|
# A single high-complexity method reported by Flog; score (BigDecimal)
|
|
14
|
-
# is the ranking key.
|
|
15
|
-
ComplexityHit = Data.define(:location, :method_name, :score)
|
|
14
|
+
# is the ranking key. source_lines holds every line of the method.
|
|
15
|
+
ComplexityHit = Data.define(:location, :method_name, :score, :breakdown, :source_lines)
|
|
16
16
|
|
|
17
17
|
# A structural-duplication cluster reported by Flay: a signature
|
|
18
18
|
# shared across two or more Locations. Cluster size (locations.size)
|
|
19
|
-
# is the ranking key.
|
|
20
|
-
|
|
19
|
+
# is the ranking key. mass is flay's cluster total; copy_mass is one
|
|
20
|
+
# copy's share of it. copies holds each location's code, ordered by
|
|
21
|
+
# path then line.
|
|
22
|
+
DuplicationCluster = Data.define(:signature, :node_type, :identical, :mass, :copy_mass, :locations, :copies) do
|
|
21
23
|
def size = locations.size
|
|
22
24
|
end
|
|
23
25
|
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Snoot
|
|
4
|
+
module RenderReport
|
|
5
|
+
# The sections of a ComplexityHit report. The context shows the span,
|
|
6
|
+
# the score split into flog's three dimensions, and the method's code.
|
|
7
|
+
module ComplexitySections
|
|
8
|
+
DIMENSION_LABELS = {
|
|
9
|
+
calls: "Calls and other (C)", branching: "Branching (B)", assignments: "Assignments (A)"
|
|
10
|
+
}.freeze
|
|
11
|
+
LABEL_WIDTH = DIMENSION_LABELS.values.map(&:size).max
|
|
12
|
+
TOP_OPERATIONS = 4
|
|
13
|
+
DOC = "High complexity hits indicate a method or class doing too much. " \
|
|
14
|
+
"Consider extracting helpers, simplifying conditionals, or " \
|
|
15
|
+
"splitting the responsibility across smaller units."
|
|
16
|
+
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
def build(hit)
|
|
20
|
+
location = hit.location.description
|
|
21
|
+
name = hit.method_name
|
|
22
|
+
score = RenderReport.format_score(hit.score.round(2))
|
|
23
|
+
{
|
|
24
|
+
header: "High complexity in #{name} at #{location} (score: #{score})",
|
|
25
|
+
finding_context: context(hit, location, name, score),
|
|
26
|
+
doc: DOC
|
|
27
|
+
}
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def context(hit, location, name, score)
|
|
31
|
+
formula = "Score: #{score} = √(A² + B² + C²)"
|
|
32
|
+
summary = ["#{location}\n", "Method: #{name}", formula, *dimension_lines(hit.breakdown)]
|
|
33
|
+
code = SourceExcerpt.block(hit.source_lines)
|
|
34
|
+
(code.empty? ? summary : [*summary, "", *code]).join("\n")
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def dimension_lines(breakdown)
|
|
38
|
+
rows = ranked_dimensions(breakdown)
|
|
39
|
+
totals = rows.map { |_, contributions| one_decimal(contributions.sum(&:score)) }
|
|
40
|
+
width = totals.map(&:size).max
|
|
41
|
+
rows.zip(totals).map do |(label, contributions), total|
|
|
42
|
+
dimension_line(label, total.rjust(width), contributions)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Highest total first; ties keep DIMENSION_LABELS order.
|
|
47
|
+
def ranked_dimensions(breakdown)
|
|
48
|
+
rows = DIMENSION_LABELS.map { |key, label| [label, breakdown.public_send(key)] }
|
|
49
|
+
rows.each_with_index.sort_by { |(_, contributions), order| [-contributions.sum(&:score), order] }.map(&:first)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def dimension_line(label, total, contributions)
|
|
53
|
+
line = " #{label.ljust(LABEL_WIDTH)} #{total}"
|
|
54
|
+
contributions.empty? ? line : "#{line} (#{operations(contributions)})"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def operations(contributions)
|
|
58
|
+
shown = contributions.take(TOP_OPERATIONS).map do |contribution|
|
|
59
|
+
"#{contribution.operation} #{one_decimal(contribution.score)}"
|
|
60
|
+
end
|
|
61
|
+
contributions.size > TOP_OPERATIONS ? [*shown, "…"].join(", ") : shown.join(", ")
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def one_decimal(score)
|
|
65
|
+
format("%.1f", score)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Snoot
|
|
4
|
+
module RenderReport
|
|
5
|
+
# The sections of a DuplicationCluster report. The context lists every
|
|
6
|
+
# copy's span, then the code of one copy when the copies are identical
|
|
7
|
+
# or of the first few when they differ, so the variation is visible.
|
|
8
|
+
module DuplicationSections
|
|
9
|
+
SIMILAR_COPIES_SHOWN = 3
|
|
10
|
+
COPY_MAX_LINES = 20
|
|
11
|
+
DOC = "Structural duplication suggests an extracted abstraction is missing. " \
|
|
12
|
+
"Consider whether the duplicated shape belongs to a single helper, " \
|
|
13
|
+
"module, or value type."
|
|
14
|
+
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
def build(cluster)
|
|
18
|
+
{
|
|
19
|
+
header: header(cluster),
|
|
20
|
+
finding_context: [locations(cluster), *code(cluster)].join("\n\n"),
|
|
21
|
+
doc: DOC
|
|
22
|
+
}
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def header(cluster)
|
|
26
|
+
kind = cluster.identical ? "Identical" : "Similar"
|
|
27
|
+
"#{kind} code (:#{cluster.node_type}) in #{cluster.size} locations, " \
|
|
28
|
+
"mass #{cluster.mass} (#{cluster.copy_mass} per copy)"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def locations(cluster)
|
|
32
|
+
["Locations:", *cluster.locations.sort_by { |location| sort_key(location) }.map(&:description)].join("\n")
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def code(cluster)
|
|
36
|
+
excerpts = cluster.copies.sort_by { |copy| sort_key(copy.location) }.filter_map { |copy| excerpt(copy) }
|
|
37
|
+
return [] if excerpts.empty?
|
|
38
|
+
|
|
39
|
+
shown = excerpts.take(cluster.identical ? 1 : SIMILAR_COPIES_SHOWN)
|
|
40
|
+
[[code_heading(cluster, shown.size), *shown].join("\n")]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def code_heading(cluster, shown_count)
|
|
44
|
+
copies = cluster.size
|
|
45
|
+
cluster.identical ? "Code (identical in all #{copies} copies):" : "Code (#{shown_count} of #{copies} copies):"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def excerpt(copy)
|
|
49
|
+
lines = SourceExcerpt.block(copy.source_lines, max_lines: COPY_MAX_LINES)
|
|
50
|
+
[copy.location.description, *lines].join("\n") unless lines.empty?
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def sort_key(location)
|
|
54
|
+
[location.path.raw, location.line_start]
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Snoot
|
|
4
|
+
module RenderReport
|
|
5
|
+
# The header, instances, tuning and doc sections of a Smell report.
|
|
6
|
+
module SmellSections
|
|
7
|
+
module_function
|
|
8
|
+
|
|
9
|
+
def build(smells, smell, orchestration)
|
|
10
|
+
smell_type = smell.smell_type
|
|
11
|
+
matching = smells.select { |instance| instance.smell_type == smell_type }
|
|
12
|
+
doc = orchestration.vendored_doc(smell_type)
|
|
13
|
+
{
|
|
14
|
+
header: header(smell_type, matching),
|
|
15
|
+
instances: instances(matching),
|
|
16
|
+
tuning: tuning(smell_type, doc),
|
|
17
|
+
doc: doc
|
|
18
|
+
}
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def header(smell_type, smells)
|
|
22
|
+
instances = RenderReport.count_of(smells.size, :instance)
|
|
23
|
+
files = RenderReport.count_of(smells.map { |smell| smell.location.path.raw }.uniq.size, :file)
|
|
24
|
+
"#{humanize(smell_type.name)}: #{instances} across #{files}"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def humanize(pascal_case)
|
|
28
|
+
pascal_case.gsub(/([a-z])([A-Z])/) { "#{Regexp.last_match(1)} #{Regexp.last_match(2).downcase}" }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def tuning(smell_type, doc)
|
|
32
|
+
options_pointer = doc.include?("## Configuration") ? " (the reek doc below lists its options)" : ""
|
|
33
|
+
advice = "Fix every instance, then rerun snoot. If the detector's settings don't suit this codebase, " \
|
|
34
|
+
"configure it in .reek.yml#{options_pointer}:"
|
|
35
|
+
example = <<~YAML.chomp.gsub(/^/, " ")
|
|
36
|
+
detectors:
|
|
37
|
+
#{smell_type.name}:
|
|
38
|
+
<option>: <value>
|
|
39
|
+
YAML
|
|
40
|
+
"#{advice}\n\n#{example}"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def instances(smells)
|
|
44
|
+
groups = groups_by_path(smells)
|
|
45
|
+
"## Instances\n\n#{groups.map { |path, group| instance_group(path, group) }.join("\n\n")}"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def groups_by_path(smells)
|
|
49
|
+
smells.group_by { |smell| smell.location.path.raw }
|
|
50
|
+
.sort_by { |path, group| [-group.size, path] }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def instance_group(path, smells)
|
|
54
|
+
"#{path}\n#{smells.map { |smell| instance(smell) }.join("\n")}"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def instance(smell)
|
|
58
|
+
source_lines = smell.source_lines
|
|
59
|
+
numbers = source_lines.map(&:number)
|
|
60
|
+
label = numbers.one? ? "Line" : "Lines"
|
|
61
|
+
[" #{label} #{numbers.join(', ')}: #{smell.message}", *SourceExcerpt.lines(source_lines)].join("\n")
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Snoot
|
|
4
|
+
module RenderReport
|
|
5
|
+
# The numbered code lines under a smell instance. Lines with no
|
|
6
|
+
# readable text are skipped; past MAX_LINES the rest are listed by
|
|
7
|
+
# number only, so a smell flagging many lines keeps the report short.
|
|
8
|
+
module SourceExcerpt
|
|
9
|
+
MAX_LINES = 5
|
|
10
|
+
MAX_WIDTH = 160
|
|
11
|
+
BLOCK_MAX_LINES = 50
|
|
12
|
+
CONTROL_CHARACTERS = /[\u0000-\u0008\u000b-\u001f\u007f-\u009f]/
|
|
13
|
+
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
def lines(source_lines)
|
|
17
|
+
readable = source_lines.select(&:text)
|
|
18
|
+
shown = readable.take(MAX_LINES)
|
|
19
|
+
hidden = readable.drop(MAX_LINES)
|
|
20
|
+
width = shown.map { |line| line.number.to_s.size }.max
|
|
21
|
+
code = shown.map { |line| code_line(line, width) }
|
|
22
|
+
hidden.any? ? [*code, more_line(hidden)] : code
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# A method's lines, as one block: common indentation is removed so
|
|
26
|
+
# the nesting stays visible without the class-level margin.
|
|
27
|
+
def block(source_lines, max_lines: BLOCK_MAX_LINES)
|
|
28
|
+
shown, hidden = source_lines.select(&:text).partition.with_index { |_, index| index < max_lines }
|
|
29
|
+
indent = common_indent(shown)
|
|
30
|
+
width = shown.map { |line| line.number.to_s.size }.max
|
|
31
|
+
code = shown.map { |line| block_line(line, width, indent) }
|
|
32
|
+
hidden.any? ? [*code, more_lines_line(hidden)] : code
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def more_lines_line(hidden)
|
|
36
|
+
" … #{hidden.size} more lines (#{hidden.first.number}–#{hidden.last.number})"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def block_line(line, width, indent)
|
|
40
|
+
" #{line.number.to_s.rjust(width)} | #{clip(neutralize(line.text.rstrip.delete_prefix(indent)))}".rstrip
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def common_indent(source_lines)
|
|
44
|
+
source_lines.map(&:text).reject { |text| text.strip.empty? }.map { |text| text[/\A\s*/] }.min_by(&:size) || ""
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def code_line(line, width)
|
|
48
|
+
" #{line.number.to_s.rjust(width)} | #{clip(neutralize(line.text.strip))}"
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def more_line(hidden)
|
|
52
|
+
" … #{hidden.size} more: #{hidden.map(&:number).join(', ')}"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Source text comes from the scanned files, so escape sequences in it
|
|
56
|
+
# would otherwise reach whatever terminal displays the report.
|
|
57
|
+
def neutralize(text)
|
|
58
|
+
text.gsub(CONTROL_CHARACTERS, "\uFFFD")
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def clip(text)
|
|
62
|
+
text.size > MAX_WIDTH ? "#{text[0, MAX_WIDTH - 1]}…" : text
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|