flog-rs 0.1.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 +7 -0
- data/Cargo.lock +486 -0
- data/Cargo.toml +22 -0
- data/DIVERGENCES.md +14 -0
- data/LICENSE +22 -0
- data/README.md +100 -0
- data/THIRD_PARTY_NOTICES.md +45 -0
- data/crates/flog-cli/Cargo.toml +23 -0
- data/crates/flog-cli/src/main.rs +224 -0
- data/crates/flog-core/Cargo.toml +18 -0
- data/crates/flog-core/src/attach_comments.rs +271 -0
- data/crates/flog-core/src/lib.rs +307 -0
- data/crates/flog-core/src/literal.rs +217 -0
- data/crates/flog-core/src/ruby_format.rs +131 -0
- data/crates/flog-core/src/ruby_sort.rs +192 -0
- data/crates/flog-core/src/scorer.rs +1851 -0
- data/crates/flog-core/src/scores.rs +96 -0
- data/crates/flog-core/src/syntax_error.rs +145 -0
- data/crates/flog-core/src/translation.rs +487 -0
- data/ext/flog_rs/Cargo.toml +18 -0
- data/ext/flog_rs/extconf.rb +6 -0
- data/ext/flog_rs/src/flog.rs +493 -0
- data/ext/flog_rs/src/lib.rs +39 -0
- data/lib/flog_rs/version.rb +8 -0
- data/lib/flog_rs.rb +16 -0
- metadata +86 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
//! Flog 4.10.0's score tables (`Flog::SCORES` and `Flog::OTHER_SCORES`).
|
|
2
|
+
|
|
3
|
+
/// `Flog::SCORES`: the weight of a method call by name. Every other name is 1.
|
|
4
|
+
pub fn call_score(name: &str) -> f64 {
|
|
5
|
+
match name {
|
|
6
|
+
"define_method" | "eval" | "module_eval" | "class_eval" | "instance_eval" => 5.0,
|
|
7
|
+
"send" => 3.0,
|
|
8
|
+
"alias_method"
|
|
9
|
+
| "extend"
|
|
10
|
+
| "include"
|
|
11
|
+
| "instance_method"
|
|
12
|
+
| "instance_methods"
|
|
13
|
+
| "method_added"
|
|
14
|
+
| "method_defined?"
|
|
15
|
+
| "method_removed"
|
|
16
|
+
| "method_undefined"
|
|
17
|
+
| "private_class_method"
|
|
18
|
+
| "private_instance_methods"
|
|
19
|
+
| "private_method_defined?"
|
|
20
|
+
| "protected_instance_methods"
|
|
21
|
+
| "protected_method_defined?"
|
|
22
|
+
| "public_class_method"
|
|
23
|
+
| "public_instance_methods"
|
|
24
|
+
| "public_method_defined?"
|
|
25
|
+
| "remove_method"
|
|
26
|
+
| "undef_method"
|
|
27
|
+
| "inject" => 2.0,
|
|
28
|
+
_ => 1.0,
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/// `Flog::OTHER_SCORES`: the weight of each non-call construct.
|
|
33
|
+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
34
|
+
pub enum OtherScore {
|
|
35
|
+
Alias,
|
|
36
|
+
Assignment,
|
|
37
|
+
BlockPass,
|
|
38
|
+
BlockCall,
|
|
39
|
+
Branch,
|
|
40
|
+
MagicNumber,
|
|
41
|
+
Sclass,
|
|
42
|
+
Super,
|
|
43
|
+
ToProcIcky,
|
|
44
|
+
ToProcLasgn,
|
|
45
|
+
Yield,
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
impl OtherScore {
|
|
49
|
+
/// The tally key Flog uses (the `OTHER_SCORES` symbol).
|
|
50
|
+
pub fn name(self) -> &'static str {
|
|
51
|
+
match self {
|
|
52
|
+
OtherScore::Alias => "alias",
|
|
53
|
+
OtherScore::Assignment => "assignment",
|
|
54
|
+
OtherScore::BlockPass => "block_pass",
|
|
55
|
+
OtherScore::BlockCall => "block_call",
|
|
56
|
+
OtherScore::Branch => "branch",
|
|
57
|
+
OtherScore::MagicNumber => "magic_number",
|
|
58
|
+
OtherScore::Sclass => "sclass",
|
|
59
|
+
OtherScore::Super => "super",
|
|
60
|
+
OtherScore::ToProcIcky => "to_proc_icky!",
|
|
61
|
+
OtherScore::ToProcLasgn => "to_proc_lasgn",
|
|
62
|
+
OtherScore::Yield => "yield",
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
pub fn score(self) -> f64 {
|
|
67
|
+
match self {
|
|
68
|
+
OtherScore::Alias => 2.0,
|
|
69
|
+
OtherScore::Assignment => 1.0,
|
|
70
|
+
OtherScore::BlockPass => 1.0,
|
|
71
|
+
OtherScore::BlockCall => 1.0,
|
|
72
|
+
OtherScore::Branch => 1.0,
|
|
73
|
+
OtherScore::MagicNumber => 0.25,
|
|
74
|
+
OtherScore::Sclass => 5.0,
|
|
75
|
+
OtherScore::Super => 1.0,
|
|
76
|
+
OtherScore::ToProcIcky => 10.0,
|
|
77
|
+
OtherScore::ToProcLasgn => 15.0,
|
|
78
|
+
OtherScore::Yield => 1.0,
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/// `Flog#score_method`: the distance formula over a method's tally, with
|
|
84
|
+
/// `assignment` in A, `branch` and `block_call` in B, and everything else in C,
|
|
85
|
+
/// summed in tally order.
|
|
86
|
+
pub fn score_method<'a>(tally: impl IntoIterator<Item = (&'a str, f64)>) -> f64 {
|
|
87
|
+
let (mut a, mut b, mut c) = (0.0_f64, 0.0_f64, 0.0_f64);
|
|
88
|
+
for (name, score) in tally {
|
|
89
|
+
match name {
|
|
90
|
+
"assignment" => a += score,
|
|
91
|
+
"branch" | "block_call" => b += score,
|
|
92
|
+
_ => c += score,
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
(a * a + b * b + c * c).sqrt()
|
|
96
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
//! The `rescue RubyParser::SyntaxError` clause of `Flog#flog_ruby`: what
|
|
2
|
+
//! Flog warns and whether it re-raises when a file fails to parse.
|
|
3
|
+
|
|
4
|
+
/// The syntax error exception, as the `rescue` clause reads it.
|
|
5
|
+
#[derive(Clone, Copy, Debug)]
|
|
6
|
+
pub struct SyntaxErrorException<'a> {
|
|
7
|
+
/// The file Flog was scoring.
|
|
8
|
+
pub path: &'a str,
|
|
9
|
+
/// `e.message`: `"file:line :: message"` (see [`crate::FileOutcome::SyntaxError`]).
|
|
10
|
+
pub message: &'a str,
|
|
11
|
+
/// `e.class.to_s`.
|
|
12
|
+
pub class_name: &'a str,
|
|
13
|
+
/// `e.inspect`.
|
|
14
|
+
pub inspect: &'a str,
|
|
15
|
+
/// `e.backtrace`.
|
|
16
|
+
pub backtrace: &'a [String],
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/// What Flog does with a syntax error.
|
|
20
|
+
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
21
|
+
pub struct SyntaxErrorHandling {
|
|
22
|
+
/// The messages Flog passes to `warn`, in order.
|
|
23
|
+
pub warnings: Vec<String>,
|
|
24
|
+
/// Whether Flog re-raises the exception after the warnings.
|
|
25
|
+
pub raises: bool,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/// Flog 4.10.0's `rescue` clause. A file that looks like an ERB template
|
|
29
|
+
/// (`e.inspect =~ /<\%|%\>/ or ruby =~ /<\%|%\>/`) is skipped. Any other
|
|
30
|
+
/// file is skipped when `continue` is set, and re-raises otherwise. `quiet`
|
|
31
|
+
/// leaves out every warning except the one before re-raising.
|
|
32
|
+
pub fn handle_syntax_error(
|
|
33
|
+
exception: &SyntaxErrorException,
|
|
34
|
+
looks_like_erb: bool,
|
|
35
|
+
quiet: bool,
|
|
36
|
+
continue_on_error: bool,
|
|
37
|
+
) -> SyntaxErrorHandling {
|
|
38
|
+
let first_five = &exception.backtrace[..exception.backtrace.len().min(5)];
|
|
39
|
+
let mut warnings = Vec::new();
|
|
40
|
+
|
|
41
|
+
if looks_like_erb {
|
|
42
|
+
if !quiet {
|
|
43
|
+
warnings.push(format!("{} at {}", exception.inspect, first_five.join(", ")));
|
|
44
|
+
warnings.push("\n...stupid lemmings and their bad erb templates... skipping".to_string());
|
|
45
|
+
}
|
|
46
|
+
return SyntaxErrorHandling { warnings, raises: false };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if !quiet {
|
|
50
|
+
warnings.push(format!("ERROR: parsing ruby file {}", exception.path));
|
|
51
|
+
}
|
|
52
|
+
if !continue_on_error {
|
|
53
|
+
warnings.push("ERROR! Aborting. You may want to run with --continue.".to_string());
|
|
54
|
+
return SyntaxErrorHandling { warnings, raises: true };
|
|
55
|
+
}
|
|
56
|
+
if !quiet {
|
|
57
|
+
warnings.push(format!(
|
|
58
|
+
"{}: {} at:\n {}",
|
|
59
|
+
exception.class_name,
|
|
60
|
+
exception.message.trim(),
|
|
61
|
+
first_five.join("\n ")
|
|
62
|
+
));
|
|
63
|
+
}
|
|
64
|
+
SyntaxErrorHandling { warnings, raises: false }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
#[cfg(test)]
|
|
68
|
+
mod tests {
|
|
69
|
+
use super::*;
|
|
70
|
+
|
|
71
|
+
fn backtrace() -> Vec<String> {
|
|
72
|
+
(1..=6).map(|frame| format!("frame{frame}")).collect()
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
fn exception(backtrace: &[String]) -> SyntaxErrorException<'_> {
|
|
76
|
+
SyntaxErrorException {
|
|
77
|
+
path: "a.rb",
|
|
78
|
+
message: "a.rb:1 :: unexpected end ",
|
|
79
|
+
class_name: "FlogRs::SyntaxError",
|
|
80
|
+
inspect: "#<FlogRs::SyntaxError: a.rb:1 :: unexpected end >",
|
|
81
|
+
backtrace,
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
#[test]
|
|
86
|
+
fn erb_is_skipped_with_two_warnings() {
|
|
87
|
+
let backtrace = backtrace();
|
|
88
|
+
let handling = handle_syntax_error(&exception(&backtrace), true, false, false);
|
|
89
|
+
assert_eq!(
|
|
90
|
+
handling.warnings,
|
|
91
|
+
[
|
|
92
|
+
"#<FlogRs::SyntaxError: a.rb:1 :: unexpected end > at frame1, frame2, frame3, frame4, frame5",
|
|
93
|
+
"\n...stupid lemmings and their bad erb templates... skipping",
|
|
94
|
+
]
|
|
95
|
+
);
|
|
96
|
+
assert!(!handling.raises);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
#[test]
|
|
100
|
+
fn erb_is_skipped_silently_when_quiet() {
|
|
101
|
+
let backtrace = backtrace();
|
|
102
|
+
let handling = handle_syntax_error(&exception(&backtrace), true, true, false);
|
|
103
|
+
assert_eq!(handling, SyntaxErrorHandling { warnings: vec![], raises: false });
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
#[test]
|
|
107
|
+
fn continue_skips_with_the_trimmed_message_and_five_frames() {
|
|
108
|
+
let backtrace = backtrace();
|
|
109
|
+
let handling = handle_syntax_error(&exception(&backtrace), false, false, true);
|
|
110
|
+
assert_eq!(
|
|
111
|
+
handling.warnings,
|
|
112
|
+
[
|
|
113
|
+
"ERROR: parsing ruby file a.rb",
|
|
114
|
+
"FlogRs::SyntaxError: a.rb:1 :: unexpected end at:\n frame1\n frame2\n frame3\n frame4\n frame5",
|
|
115
|
+
]
|
|
116
|
+
);
|
|
117
|
+
assert!(!handling.raises);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
#[test]
|
|
121
|
+
fn continue_skips_silently_when_quiet() {
|
|
122
|
+
let backtrace = backtrace();
|
|
123
|
+
let handling = handle_syntax_error(&exception(&backtrace), false, true, true);
|
|
124
|
+
assert_eq!(handling, SyntaxErrorHandling { warnings: vec![], raises: false });
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
#[test]
|
|
128
|
+
fn without_continue_it_raises() {
|
|
129
|
+
let backtrace = backtrace();
|
|
130
|
+
let handling = handle_syntax_error(&exception(&backtrace), false, false, false);
|
|
131
|
+
assert_eq!(
|
|
132
|
+
handling.warnings,
|
|
133
|
+
["ERROR: parsing ruby file a.rb", "ERROR! Aborting. You may want to run with --continue."]
|
|
134
|
+
);
|
|
135
|
+
assert!(handling.raises);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
#[test]
|
|
139
|
+
fn without_continue_quiet_still_warns_before_raising() {
|
|
140
|
+
let backtrace = backtrace();
|
|
141
|
+
let handling = handle_syntax_error(&exception(&backtrace), false, true, false);
|
|
142
|
+
assert_eq!(handling.warnings, ["ERROR! Aborting. You may want to run with --continue."]);
|
|
143
|
+
assert!(handling.raises);
|
|
144
|
+
}
|
|
145
|
+
}
|