reek-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 +614 -0
- data/Cargo.toml +24 -0
- data/DIVERGENCES.md +15 -0
- data/LICENSE +22 -0
- data/README.md +125 -0
- data/THIRD_PARTY_NOTICES.md +57 -0
- data/crates/reek-cli/Cargo.toml +22 -0
- data/crates/reek-cli/src/main.rs +277 -0
- data/crates/reek-core/Cargo.toml +21 -0
- data/crates/reek-core/src/ast.rs +449 -0
- data/crates/reek-core/src/code_comment.rs +245 -0
- data/crates/reek-core/src/comments.rs +179 -0
- data/crates/reek-core/src/config.rs +803 -0
- data/crates/reek-core/src/context.rs +891 -0
- data/crates/reek-core/src/detectors/attribute.rs +13 -0
- data/crates/reek-core/src/detectors/boolean_parameter.rs +20 -0
- data/crates/reek-core/src/detectors/class_variable.rs +25 -0
- data/crates/reek-core/src/detectors/control_parameter.rs +110 -0
- data/crates/reek-core/src/detectors/data_clump.rs +114 -0
- data/crates/reek-core/src/detectors/duplicate_method_call.rs +63 -0
- data/crates/reek-core/src/detectors/feature_envy.rs +32 -0
- data/crates/reek-core/src/detectors/instance_variable_assumption.rs +39 -0
- data/crates/reek-core/src/detectors/irresponsible_module.rs +12 -0
- data/crates/reek-core/src/detectors/long_parameter_list.rs +16 -0
- data/crates/reek-core/src/detectors/long_yield_list.rs +20 -0
- data/crates/reek-core/src/detectors/manual_dispatch.rs +18 -0
- data/crates/reek-core/src/detectors/missing_safe_method.rs +34 -0
- data/crates/reek-core/src/detectors/mod.rs +479 -0
- data/crates/reek-core/src/detectors/module_initialize.rs +15 -0
- data/crates/reek-core/src/detectors/nested_iterators.rs +82 -0
- data/crates/reek-core/src/detectors/nil_check.rs +40 -0
- data/crates/reek-core/src/detectors/repeated_conditional.rs +51 -0
- data/crates/reek-core/src/detectors/subclassed_from_core_class.rs +22 -0
- data/crates/reek-core/src/detectors/too_many_constants.rs +18 -0
- data/crates/reek-core/src/detectors/too_many_instance_variables.rs +25 -0
- data/crates/reek-core/src/detectors/too_many_methods.rs +15 -0
- data/crates/reek-core/src/detectors/too_many_statements.rs +16 -0
- data/crates/reek-core/src/detectors/uncommunicative_method_name.rs +32 -0
- data/crates/reek-core/src/detectors/uncommunicative_module_name.rs +33 -0
- data/crates/reek-core/src/detectors/uncommunicative_parameter_name.rs +49 -0
- data/crates/reek-core/src/detectors/uncommunicative_variable_name.rs +138 -0
- data/crates/reek-core/src/detectors/unused_parameters.rs +21 -0
- data/crates/reek-core/src/detectors/unused_private_method.rs +59 -0
- data/crates/reek-core/src/detectors/utility_function.rs +27 -0
- data/crates/reek-core/src/encodings.rs +180 -0
- data/crates/reek-core/src/examiner.rs +165 -0
- data/crates/reek-core/src/lib.rs +21 -0
- data/crates/reek-core/src/node.rs +423 -0
- data/crates/reek-core/src/ruby_format.rs +60 -0
- data/crates/reek-core/src/ruby_inspect.rs +172 -0
- data/crates/reek-core/src/ruby_regexp.rs +243 -0
- data/crates/reek-core/src/single_byte_encodings.rs +6007 -0
- data/crates/reek-core/src/source.rs +280 -0
- data/crates/reek-core/src/translate.rs +3112 -0
- data/crates/reek-core/src/yaml.rs +520 -0
- data/ext/reek_rs/Cargo.toml +18 -0
- data/ext/reek_rs/extconf.rb +6 -0
- data/ext/reek_rs/src/lib.rs +155 -0
- data/lib/reek_rs/version.rb +8 -0
- data/lib/reek_rs.rb +390 -0
- metadata +123 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//! `Attribute`: a public attribute writer (`attr_writer`, `attr_accessor`,
|
|
2
|
+
//! `attr ..., true`).
|
|
3
|
+
|
|
4
|
+
use super::{line, Detector, SniffResult};
|
|
5
|
+
use crate::context::Visibility;
|
|
6
|
+
|
|
7
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
8
|
+
if detector.contexts.context(detector.context).visibility != Visibility::Public {
|
|
9
|
+
return Ok(Vec::new());
|
|
10
|
+
}
|
|
11
|
+
let line = line(detector.tree(), detector.expression())?;
|
|
12
|
+
Ok(vec![detector.smell_warning(vec![line], "is a writable attribute".to_string(), Vec::new())?])
|
|
13
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//! `BooleanParameter`: a parameter defaulting to `true` or `false`.
|
|
2
|
+
|
|
3
|
+
use super::{Detector, Parameter, SniffResult};
|
|
4
|
+
|
|
5
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
6
|
+
let tree = detector.tree();
|
|
7
|
+
let mut smells = Vec::new();
|
|
8
|
+
for (parameter, value) in detector.contexts.default_assignments(detector.context)? {
|
|
9
|
+
if !matches!(tree.ty(value), "true" | "false") {
|
|
10
|
+
continue;
|
|
11
|
+
}
|
|
12
|
+
let parameter = parameter.to_s().to_string();
|
|
13
|
+
smells.push(detector.smell_warning(
|
|
14
|
+
vec![detector.source_line()?],
|
|
15
|
+
format!("has boolean parameter '{parameter}'"),
|
|
16
|
+
vec![("parameter", Parameter::String(parameter))],
|
|
17
|
+
)?);
|
|
18
|
+
}
|
|
19
|
+
Ok(smells)
|
|
20
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//! `ClassVariable`: a class variable read or assigned in a class or
|
|
2
|
+
//! module, one smell per variable.
|
|
3
|
+
|
|
4
|
+
use indexmap::IndexMap;
|
|
5
|
+
|
|
6
|
+
use super::{line, Detector, Parameter, SniffResult};
|
|
7
|
+
use crate::node::Name;
|
|
8
|
+
|
|
9
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
10
|
+
let tree = detector.tree();
|
|
11
|
+
let mut occurrences: IndexMap<Name, Vec<usize>> = IndexMap::new();
|
|
12
|
+
for node in detector.contexts.local_nodes(detector.context, &["cvar", "cvasgn", "cvdecl"], &[]) {
|
|
13
|
+
occurrences.entry(tree.name(node)).or_default().push(line(tree, node)?);
|
|
14
|
+
}
|
|
15
|
+
let mut smells = Vec::new();
|
|
16
|
+
for (variable, lines) in occurrences {
|
|
17
|
+
let variable = variable.to_s().to_string();
|
|
18
|
+
smells.push(detector.smell_warning(
|
|
19
|
+
lines,
|
|
20
|
+
format!("declares the class variable '{variable}'"),
|
|
21
|
+
vec![("name", Parameter::String(variable))],
|
|
22
|
+
)?);
|
|
23
|
+
}
|
|
24
|
+
Ok(smells)
|
|
25
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
//! `ControlParameter`: a parameter only used to choose a branch (in the
|
|
2
|
+
//! condition of an `if`, `case`, `&&` or `||`).
|
|
3
|
+
|
|
4
|
+
use super::{line, Detector, Parameter, SniffResult};
|
|
5
|
+
use crate::ast::{NodeId, Tree};
|
|
6
|
+
use crate::context::ContextError;
|
|
7
|
+
use crate::node::Name;
|
|
8
|
+
|
|
9
|
+
/// `ControlParameterFinder::CONDITIONAL_NODE_TYPES`.
|
|
10
|
+
const CONDITIONAL_NODE_TYPES: [&str; 4] = ["if", "case", "and", "or"];
|
|
11
|
+
|
|
12
|
+
/// `CallInConditionFinder::COMPARISON_METHOD_NAMES`.
|
|
13
|
+
const COMPARISON_METHOD_NAMES: [&str; 3] = ["==", "!=", "=~"];
|
|
14
|
+
|
|
15
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
16
|
+
let tree = detector.tree();
|
|
17
|
+
let mut smells = Vec::new();
|
|
18
|
+
for parameter in tree.parameter_names(detector.expression())? {
|
|
19
|
+
let finder = ControlParameterFinder { tree, node: detector.expression(), parameter: ¶meter };
|
|
20
|
+
let occurrences = finder.find_matches()?;
|
|
21
|
+
if occurrences.is_empty() {
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
let lines = occurrences.iter().map(|occurrence| line(tree, *occurrence)).collect::<Result<_, _>>()?;
|
|
25
|
+
let argument = parameter.to_s().to_string();
|
|
26
|
+
smells.push(detector.smell_warning(
|
|
27
|
+
lines,
|
|
28
|
+
format!("is controlled by argument '{argument}'"),
|
|
29
|
+
vec![("argument", Parameter::String(argument))],
|
|
30
|
+
)?);
|
|
31
|
+
}
|
|
32
|
+
Ok(smells)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/// `ControlParameterHelpers::ControlParameterFinder`: the uses of a
|
|
36
|
+
/// parameter in conditions within a node, unless it is used otherwise.
|
|
37
|
+
struct ControlParameterFinder<'a> {
|
|
38
|
+
tree: &'a Tree,
|
|
39
|
+
node: NodeId,
|
|
40
|
+
parameter: &'a Name,
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
impl ControlParameterFinder<'_> {
|
|
44
|
+
/// `find_matches`: the parameter's reads in the conditions of nested
|
|
45
|
+
/// conditionals, then in this node's condition.
|
|
46
|
+
fn find_matches(&self) -> Result<Vec<NodeId>, ContextError> {
|
|
47
|
+
if self.has_legitimate_uses()? {
|
|
48
|
+
return Ok(Vec::new());
|
|
49
|
+
}
|
|
50
|
+
let mut matches = Vec::new();
|
|
51
|
+
for finder in self.nested_finders()? {
|
|
52
|
+
matches.extend(finder.find_matches()?);
|
|
53
|
+
}
|
|
54
|
+
matches.extend(self.uses_of_param_in_condition());
|
|
55
|
+
Ok(matches)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/// `legitimate_uses?`: the parameter is passed to a call in the
|
|
59
|
+
/// condition, used in the body, or used so in a nested conditional.
|
|
60
|
+
fn has_legitimate_uses(&self) -> Result<bool, ContextError> {
|
|
61
|
+
if self.uses_param_in_call_in_condition() {
|
|
62
|
+
return Ok(true);
|
|
63
|
+
}
|
|
64
|
+
if self.is_parameter_used_in_body()? {
|
|
65
|
+
return Ok(true);
|
|
66
|
+
}
|
|
67
|
+
for finder in self.nested_finders()? {
|
|
68
|
+
if finder.has_legitimate_uses()? {
|
|
69
|
+
return Ok(true);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
Ok(false)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/// `CallInConditionFinder#uses_param_in_call_in_condition?`: a call in
|
|
76
|
+
/// the condition, other than a comparison, involves the parameter.
|
|
77
|
+
fn uses_param_in_call_in_condition(&self) -> bool {
|
|
78
|
+
let Some(condition) = self.tree.condition(self.node) else { return false };
|
|
79
|
+
self.tree.each_node(condition, &["send"], &[]).into_iter().any(|call| {
|
|
80
|
+
let is_comparison = matches!(self.tree.name(call), Name::Symbol(name) if COMPARISON_METHOD_NAMES.contains(&name.as_str()));
|
|
81
|
+
self.involves_parameter(call) && !is_comparison
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
fn involves_parameter(&self, node: NodeId) -> bool {
|
|
86
|
+
self.tree.each_node(node, &["lvar"], &[]).into_iter().any(|lvar| self.tree.name(lvar) == *self.parameter)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/// `parameter_used_in_body?`.
|
|
90
|
+
fn is_parameter_used_in_body(&self) -> Result<bool, ContextError> {
|
|
91
|
+
let nodes = self.tree.body_nodes(self.node, &["lvar"], &CONDITIONAL_NODE_TYPES)?;
|
|
92
|
+
Ok(nodes.into_iter().any(|lvar| self.tree.name(lvar) == *self.parameter))
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/// `nested_finders`: a finder for each conditional in the body.
|
|
96
|
+
fn nested_finders(&self) -> Result<Vec<Self>, ContextError> {
|
|
97
|
+
let conditional_nodes = self.tree.body_nodes(self.node, &CONDITIONAL_NODE_TYPES, &[])?;
|
|
98
|
+
Ok(conditional_nodes
|
|
99
|
+
.into_iter()
|
|
100
|
+
.map(|node| ControlParameterFinder { tree: self.tree, node, parameter: self.parameter })
|
|
101
|
+
.collect())
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/// `uses_of_param_in_condition`.
|
|
105
|
+
fn uses_of_param_in_condition(&self) -> Vec<NodeId> {
|
|
106
|
+
let Some(condition) = self.tree.condition(self.node) else { return Vec::new() };
|
|
107
|
+
let lvars = self.tree.each_node(condition, &["lvar"], &[]);
|
|
108
|
+
lvars.into_iter().filter(|lvar| self.tree.name(*lvar) == *self.parameter).collect()
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
//! `DataClump`: the same group of parameters taken by several methods of a
|
|
2
|
+
//! class or module.
|
|
3
|
+
|
|
4
|
+
use super::{at_least, line, Detector, Parameter, SniffResult};
|
|
5
|
+
use crate::ast::NodeId;
|
|
6
|
+
use crate::context::ContextError;
|
|
7
|
+
use crate::node::Name;
|
|
8
|
+
use crate::yaml::Value;
|
|
9
|
+
|
|
10
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
11
|
+
let tree = detector.tree();
|
|
12
|
+
let candidate_methods = detector.contexts.node_instance_methods(detector.context);
|
|
13
|
+
let arg_names: Vec<Vec<Name>> =
|
|
14
|
+
candidate_methods.iter().map(|method| tree.arg_names(*method)).collect::<Result<_, _>>()?;
|
|
15
|
+
let mut smells = Vec::new();
|
|
16
|
+
for clump in candidate_clumps(detector, &arg_names)? {
|
|
17
|
+
let methods: Vec<NodeId> = candidate_methods
|
|
18
|
+
.iter()
|
|
19
|
+
.zip(&arg_names)
|
|
20
|
+
.filter(|(_, names)| intersection(&clump, names) == clump)
|
|
21
|
+
.map(|(method, _)| *method)
|
|
22
|
+
.collect();
|
|
23
|
+
let count = methods.len();
|
|
24
|
+
let names: Vec<String> = clump.iter().map(|name| name.to_s().to_string()).collect();
|
|
25
|
+
let printed: Vec<String> = names.iter().map(|name| format!("'{name}'")).collect();
|
|
26
|
+
smells.push(detector.smell_warning(
|
|
27
|
+
methods.iter().map(|method| line(tree, *method)).collect::<Result<_, _>>()?,
|
|
28
|
+
format!("takes parameters [{}] to {count} methods", printed.join(", ")),
|
|
29
|
+
vec![("parameters", Parameter::Strings(names)), ("count", Parameter::Integer(count as i64))],
|
|
30
|
+
)?);
|
|
31
|
+
}
|
|
32
|
+
Ok(smells)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/// `candidate_clumps`: for each combination of `max_copies + 1` methods,
|
|
36
|
+
/// the argument names all of them take (`inject(:&)`) without `nil`,
|
|
37
|
+
/// sorted; those with at least `min_clump_size` names, without duplicates.
|
|
38
|
+
fn candidate_clumps(detector: &Detector, arg_names: &[Vec<Name>]) -> Result<Vec<Vec<Name>>, ContextError> {
|
|
39
|
+
let size = integer(&detector.value("max_copies")?, "+")? + 1;
|
|
40
|
+
if size == 0 {
|
|
41
|
+
// `[[]].map { [].inject(:&) }` is `nil.compact`.
|
|
42
|
+
return Err(ContextError::Unexpected("undefined method 'compact' for nil".to_string()));
|
|
43
|
+
}
|
|
44
|
+
if size < 0 || size as usize > arg_names.len() {
|
|
45
|
+
return Ok(Vec::new());
|
|
46
|
+
}
|
|
47
|
+
// Reek reads `min_clump_size` when selecting from the first
|
|
48
|
+
// combination; there is at least one.
|
|
49
|
+
let min_clump_size = detector.value("min_clump_size")?;
|
|
50
|
+
let mut search = ClumpSearch { arg_names, size: size as usize, min_clump_size: &min_clump_size, clumps: Vec::new() };
|
|
51
|
+
search.combine(0, &mut Vec::new(), None)?;
|
|
52
|
+
Ok(search.clumps)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/// An Integer configuration value (`combination` truncates a Float).
|
|
56
|
+
fn integer(value: &Value, method: &str) -> Result<i64, ContextError> {
|
|
57
|
+
match value {
|
|
58
|
+
Value::Int(value) => Ok(*value),
|
|
59
|
+
Value::Float(value) => Ok(*value as i64),
|
|
60
|
+
other => Err(ContextError::Unexpected(format!("undefined method '{method}' for an instance of {}", other.class_name()))),
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/// Enumerates the combinations in `Array#combination` order, skipping
|
|
65
|
+
/// those whose common names (which only shrink as methods are added)
|
|
66
|
+
/// already number fewer than `min_clump_size`.
|
|
67
|
+
struct ClumpSearch<'a> {
|
|
68
|
+
arg_names: &'a [Vec<Name>],
|
|
69
|
+
size: usize,
|
|
70
|
+
min_clump_size: &'a Value,
|
|
71
|
+
clumps: Vec<Vec<Name>>,
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
impl ClumpSearch<'_> {
|
|
75
|
+
fn combine(&mut self, start: usize, chosen: &mut Vec<usize>, common: Option<&[Name]>) -> Result<(), ContextError> {
|
|
76
|
+
if chosen.len() == self.size {
|
|
77
|
+
// `inject` returns a single method's names as they are.
|
|
78
|
+
let mut clump: Vec<Name> = common.expect("at least one method is chosen").to_vec();
|
|
79
|
+
clump.retain(|name| *name != Name::Nil);
|
|
80
|
+
clump.sort_by(|a, b| a.to_s().as_bytes().cmp(b.to_s().as_bytes()));
|
|
81
|
+
if at_least(clump.len() as i64, self.min_clump_size)? && !self.clumps.contains(&clump) {
|
|
82
|
+
self.clumps.push(clump);
|
|
83
|
+
}
|
|
84
|
+
return Ok(());
|
|
85
|
+
}
|
|
86
|
+
for index in start..=self.arg_names.len() - (self.size - chosen.len()) {
|
|
87
|
+
let names = match common {
|
|
88
|
+
None => self.arg_names[index].clone(),
|
|
89
|
+
Some(common) => intersection(common, &self.arg_names[index]),
|
|
90
|
+
};
|
|
91
|
+
if chosen.len() + 1 < self.size && common.is_some() {
|
|
92
|
+
let named = names.iter().filter(|name| **name != Name::Nil).count();
|
|
93
|
+
if !at_least(named as i64, self.min_clump_size)? {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
chosen.push(index);
|
|
98
|
+
self.combine(index + 1, chosen, Some(&names))?;
|
|
99
|
+
chosen.pop();
|
|
100
|
+
}
|
|
101
|
+
Ok(())
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/// `Array#&`: the elements of `a` that are in `b`, without duplicates.
|
|
106
|
+
fn intersection(a: &[Name], b: &[Name]) -> Vec<Name> {
|
|
107
|
+
let mut out: Vec<Name> = Vec::new();
|
|
108
|
+
for name in a {
|
|
109
|
+
if b.contains(name) && !out.contains(name) {
|
|
110
|
+
out.push(name.clone());
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
out
|
|
114
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
//! `DuplicateMethodCall`: the same call (or block) made more than
|
|
2
|
+
//! `max_calls` times in a method.
|
|
3
|
+
|
|
4
|
+
use super::{array_items, exceeds, interpolated_match, line, Detector, NodeMap, Parameter, SniffResult};
|
|
5
|
+
use crate::ast::NodeId;
|
|
6
|
+
|
|
7
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
8
|
+
let tree = detector.tree();
|
|
9
|
+
let max_allowed_calls = detector.value("max_calls")?;
|
|
10
|
+
let allow_calls = detector.value("allow_calls")?;
|
|
11
|
+
let mut smells = Vec::new();
|
|
12
|
+
for (call_node, occurrences) in calls(detector) {
|
|
13
|
+
let occurs = occurrences.len();
|
|
14
|
+
if !exceeds(occurs as i64, &max_allowed_calls)? {
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
let call = tree.format_to_ruby(call_node);
|
|
18
|
+
if is_allowed_call(&allow_calls, &call)? {
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
let call = String::from_utf8_lossy(&call).into_owned();
|
|
22
|
+
smells.push(detector.smell_warning(
|
|
23
|
+
occurrences.iter().map(|occurrence| line(tree, *occurrence)).collect::<Result<_, _>>()?,
|
|
24
|
+
format!("calls '{call}' {occurs} times"),
|
|
25
|
+
vec![("name", Parameter::String(call)), ("count", Parameter::Integer(occurs as i64))],
|
|
26
|
+
)?);
|
|
27
|
+
}
|
|
28
|
+
Ok(smells)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/// `CallCollector#calls`: equal calls grouped, with their occurrences.
|
|
32
|
+
/// Calls to `new` and calls without a receiver or arguments do not count;
|
|
33
|
+
/// calls in multiple assignment targets are not searched.
|
|
34
|
+
fn calls(detector: &Detector) -> Vec<(NodeId, Vec<NodeId>)> {
|
|
35
|
+
let tree = detector.tree();
|
|
36
|
+
let mut result: NodeMap<Vec<NodeId>> = NodeMap::new();
|
|
37
|
+
for call_node in detector.contexts.local_nodes(detector.context, &["send"], &["mlhs"]) {
|
|
38
|
+
if tree.is_object_creation_call(call_node) || is_simple_method_call(detector, call_node) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
result.entry(tree, call_node, Vec::new).push(call_node);
|
|
42
|
+
}
|
|
43
|
+
for call_node in detector.contexts.local_nodes(detector.context, &["block"], &[]) {
|
|
44
|
+
result.entry(tree, call_node, Vec::new).push(call_node);
|
|
45
|
+
}
|
|
46
|
+
result.into_entries()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/// `simple_method_call?`: no receiver and no arguments.
|
|
50
|
+
fn is_simple_method_call(detector: &Detector, call_node: NodeId) -> bool {
|
|
51
|
+
let tree = detector.tree();
|
|
52
|
+
tree.receiver(call_node).is_none() && tree.send_args(call_node).is_empty()
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/// `allow_calls?(method)`: the call matches an `allow_calls` pattern.
|
|
56
|
+
fn is_allowed_call(allow_calls: &crate::yaml::Value, call: &[u8]) -> Result<bool, crate::context::ContextError> {
|
|
57
|
+
for allow in array_items(allow_calls, "any?")? {
|
|
58
|
+
if interpolated_match(&allow, call)? {
|
|
59
|
+
return Ok(true);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
Ok(false)
|
|
63
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
//! `FeatureEnvy`: a method that refers to another object more than to
|
|
2
|
+
//! `self`.
|
|
3
|
+
|
|
4
|
+
use super::{Detector, Parameter, SniffResult};
|
|
5
|
+
use crate::context::ContextError;
|
|
6
|
+
|
|
7
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
8
|
+
let contexts = detector.contexts;
|
|
9
|
+
if contexts.is_singleton_method(detector.context) || contexts.is_module_function(detector.context) {
|
|
10
|
+
return Ok(Vec::new());
|
|
11
|
+
}
|
|
12
|
+
if !contexts.references_self(detector.context) {
|
|
13
|
+
return Ok(Vec::new());
|
|
14
|
+
}
|
|
15
|
+
let refs = &contexts.context(detector.context).refs;
|
|
16
|
+
if refs.self_is_max() {
|
|
17
|
+
return Ok(Vec::new());
|
|
18
|
+
}
|
|
19
|
+
let mut smells = Vec::new();
|
|
20
|
+
for (name, lines) in refs.most_popular() {
|
|
21
|
+
let lines = lines
|
|
22
|
+
.iter()
|
|
23
|
+
.map(|line| line.ok_or_else(|| ContextError::Unexpected("a reference without a line".to_string())))
|
|
24
|
+
.collect::<Result<_, _>>()?;
|
|
25
|
+
smells.push(detector.smell_warning(
|
|
26
|
+
lines,
|
|
27
|
+
format!("refers to '{name}' more than self (maybe move it to another class?)"),
|
|
28
|
+
vec![("name", Parameter::String(name.to_string()))],
|
|
29
|
+
)?);
|
|
30
|
+
}
|
|
31
|
+
Ok(smells)
|
|
32
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
//! `InstanceVariableAssumption`: a class reading instance variables that
|
|
2
|
+
//! `initialize` does not set.
|
|
3
|
+
|
|
4
|
+
use super::{Detector, Parameter, SniffResult};
|
|
5
|
+
use crate::node::Name;
|
|
6
|
+
|
|
7
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
8
|
+
let tree = detector.tree();
|
|
9
|
+
let method_expressions = detector.contexts.node_instance_methods(detector.context);
|
|
10
|
+
// `variables_from_context`: instance variables read, outside `||=`.
|
|
11
|
+
let variables_from_context: Vec<Name> = method_expressions
|
|
12
|
+
.iter()
|
|
13
|
+
.flat_map(|method| tree.each_node(*method, &["ivar"], &["or_asgn"]))
|
|
14
|
+
.map(|ivar| tree.name(ivar))
|
|
15
|
+
.collect();
|
|
16
|
+
// `variables_from_initializers`: instance variables `initialize` sets.
|
|
17
|
+
let variables_from_initializers: Vec<Name> = method_expressions
|
|
18
|
+
.iter()
|
|
19
|
+
.filter(|method| tree.name(**method).is_symbol("initialize"))
|
|
20
|
+
.flat_map(|method| tree.each_node(*method, &["ivasgn"], &[]))
|
|
21
|
+
.map(|ivasgn| tree.name(ivasgn))
|
|
22
|
+
.collect();
|
|
23
|
+
let mut assumptions: Vec<Name> = Vec::new();
|
|
24
|
+
for variable in variables_from_context {
|
|
25
|
+
if !variables_from_initializers.contains(&variable) && !assumptions.contains(&variable) {
|
|
26
|
+
assumptions.push(variable);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
let mut smells = Vec::new();
|
|
30
|
+
for assumption in assumptions {
|
|
31
|
+
let assumption = assumption.to_s().to_string();
|
|
32
|
+
smells.push(detector.smell_warning(
|
|
33
|
+
vec![detector.source_line()?],
|
|
34
|
+
format!("assumes too much for instance variable '{assumption}'"),
|
|
35
|
+
vec![("assumption", Parameter::String(assumption))],
|
|
36
|
+
)?);
|
|
37
|
+
}
|
|
38
|
+
Ok(smells)
|
|
39
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//! `IrresponsibleModule`: a class or module without a descriptive comment,
|
|
2
|
+
//! unless it only namespaces other modules, classes and constants.
|
|
3
|
+
|
|
4
|
+
use super::{Detector, SniffResult};
|
|
5
|
+
|
|
6
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
7
|
+
let contexts = detector.contexts;
|
|
8
|
+
if contexts.is_descriptively_commented(detector.context)? || contexts.is_namespace_module(detector.context) {
|
|
9
|
+
return Ok(Vec::new());
|
|
10
|
+
}
|
|
11
|
+
Ok(vec![detector.smell_warning(vec![detector.source_line()?], "has no descriptive comment".to_string(), Vec::new())?])
|
|
12
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//! `LongParameterList`: a method taking more than `max_params` arguments
|
|
2
|
+
//! (`initialize` may take 5).
|
|
3
|
+
|
|
4
|
+
use super::{exceeds, Detector, Parameter, SniffResult};
|
|
5
|
+
|
|
6
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
7
|
+
let count = detector.tree().arg_names(detector.expression())?.len() as i64;
|
|
8
|
+
if !exceeds(count, &detector.value("max_params")?)? {
|
|
9
|
+
return Ok(Vec::new());
|
|
10
|
+
}
|
|
11
|
+
Ok(vec![detector.smell_warning(
|
|
12
|
+
vec![detector.source_line()?],
|
|
13
|
+
format!("has {count} parameters"),
|
|
14
|
+
vec![("count", Parameter::Integer(count))],
|
|
15
|
+
)?])
|
|
16
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//! `LongYieldList`: a `yield` passing more than `max_params` values.
|
|
2
|
+
|
|
3
|
+
use super::{exceeds, line, Detector, Parameter, SniffResult};
|
|
4
|
+
|
|
5
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
6
|
+
let tree = detector.tree();
|
|
7
|
+
let mut smells = Vec::new();
|
|
8
|
+
for yield_node in detector.contexts.local_nodes(detector.context, &["yield"], &[]) {
|
|
9
|
+
let count = tree.children(yield_node).len() as i64;
|
|
10
|
+
if !exceeds(count, &detector.value("max_params")?)? {
|
|
11
|
+
continue;
|
|
12
|
+
}
|
|
13
|
+
smells.push(detector.smell_warning(
|
|
14
|
+
vec![line(tree, yield_node)?],
|
|
15
|
+
format!("yields {count} parameters"),
|
|
16
|
+
vec![("count", Parameter::Integer(count))],
|
|
17
|
+
)?);
|
|
18
|
+
}
|
|
19
|
+
Ok(smells)
|
|
20
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//! `ManualDispatch`: a method calling `respond_to?`.
|
|
2
|
+
|
|
3
|
+
use super::{line, Detector, SniffResult};
|
|
4
|
+
|
|
5
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
6
|
+
let tree = detector.tree();
|
|
7
|
+
let smelly_nodes: Vec<_> = detector
|
|
8
|
+
.contexts
|
|
9
|
+
.local_nodes(detector.context, &["send"], &[])
|
|
10
|
+
.into_iter()
|
|
11
|
+
.filter(|node| tree.name(*node).is_symbol("respond_to?"))
|
|
12
|
+
.collect();
|
|
13
|
+
if smelly_nodes.is_empty() {
|
|
14
|
+
return Ok(Vec::new());
|
|
15
|
+
}
|
|
16
|
+
let lines = smelly_nodes.iter().map(|node| line(tree, *node)).collect::<Result<_, _>>()?;
|
|
17
|
+
Ok(vec![detector.smell_warning(lines, "manually dispatches method call".to_string(), Vec::new())?])
|
|
18
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
//! `MissingSafeMethod`: a bang method (`save!`) without a method of the
|
|
2
|
+
//! same name without the bang.
|
|
3
|
+
|
|
4
|
+
use super::{array_items, line, Detector, Parameter, SniffResult};
|
|
5
|
+
use crate::yaml::Value;
|
|
6
|
+
|
|
7
|
+
pub fn sniff(detector: &Detector) -> SniffResult {
|
|
8
|
+
let tree = detector.tree();
|
|
9
|
+
let methods = detector.contexts.node_instance_methods(detector.context);
|
|
10
|
+
let mut ignore_method_names = None;
|
|
11
|
+
let mut smells = Vec::new();
|
|
12
|
+
for method in &methods {
|
|
13
|
+
let name = tree.name(*method).to_s().to_string();
|
|
14
|
+
// `ends_with_bang?`
|
|
15
|
+
let Some(name_without_bang) = name.strip_suffix('!') else { continue };
|
|
16
|
+
// `ignore_method?`
|
|
17
|
+
if ignore_method_names.is_none() {
|
|
18
|
+
ignore_method_names = Some(array_items(&detector.value("exclude")?, "include?")?);
|
|
19
|
+
}
|
|
20
|
+
if ignore_method_names.as_ref().expect("read above").contains(&Value::Str(name.clone())) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
// `version_without_bang_exists?`
|
|
24
|
+
if methods.iter().any(|other| tree.name(*other).to_s() == name_without_bang) {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
smells.push(detector.smell_warning(
|
|
28
|
+
vec![line(tree, *method)?],
|
|
29
|
+
format!("has missing safe method '{name}'"),
|
|
30
|
+
vec![("name", Parameter::String(name))],
|
|
31
|
+
)?);
|
|
32
|
+
}
|
|
33
|
+
Ok(smells)
|
|
34
|
+
}
|