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.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/Cargo.lock +614 -0
  3. data/Cargo.toml +24 -0
  4. data/DIVERGENCES.md +15 -0
  5. data/LICENSE +22 -0
  6. data/README.md +125 -0
  7. data/THIRD_PARTY_NOTICES.md +57 -0
  8. data/crates/reek-cli/Cargo.toml +22 -0
  9. data/crates/reek-cli/src/main.rs +277 -0
  10. data/crates/reek-core/Cargo.toml +21 -0
  11. data/crates/reek-core/src/ast.rs +449 -0
  12. data/crates/reek-core/src/code_comment.rs +245 -0
  13. data/crates/reek-core/src/comments.rs +179 -0
  14. data/crates/reek-core/src/config.rs +803 -0
  15. data/crates/reek-core/src/context.rs +891 -0
  16. data/crates/reek-core/src/detectors/attribute.rs +13 -0
  17. data/crates/reek-core/src/detectors/boolean_parameter.rs +20 -0
  18. data/crates/reek-core/src/detectors/class_variable.rs +25 -0
  19. data/crates/reek-core/src/detectors/control_parameter.rs +110 -0
  20. data/crates/reek-core/src/detectors/data_clump.rs +114 -0
  21. data/crates/reek-core/src/detectors/duplicate_method_call.rs +63 -0
  22. data/crates/reek-core/src/detectors/feature_envy.rs +32 -0
  23. data/crates/reek-core/src/detectors/instance_variable_assumption.rs +39 -0
  24. data/crates/reek-core/src/detectors/irresponsible_module.rs +12 -0
  25. data/crates/reek-core/src/detectors/long_parameter_list.rs +16 -0
  26. data/crates/reek-core/src/detectors/long_yield_list.rs +20 -0
  27. data/crates/reek-core/src/detectors/manual_dispatch.rs +18 -0
  28. data/crates/reek-core/src/detectors/missing_safe_method.rs +34 -0
  29. data/crates/reek-core/src/detectors/mod.rs +479 -0
  30. data/crates/reek-core/src/detectors/module_initialize.rs +15 -0
  31. data/crates/reek-core/src/detectors/nested_iterators.rs +82 -0
  32. data/crates/reek-core/src/detectors/nil_check.rs +40 -0
  33. data/crates/reek-core/src/detectors/repeated_conditional.rs +51 -0
  34. data/crates/reek-core/src/detectors/subclassed_from_core_class.rs +22 -0
  35. data/crates/reek-core/src/detectors/too_many_constants.rs +18 -0
  36. data/crates/reek-core/src/detectors/too_many_instance_variables.rs +25 -0
  37. data/crates/reek-core/src/detectors/too_many_methods.rs +15 -0
  38. data/crates/reek-core/src/detectors/too_many_statements.rs +16 -0
  39. data/crates/reek-core/src/detectors/uncommunicative_method_name.rs +32 -0
  40. data/crates/reek-core/src/detectors/uncommunicative_module_name.rs +33 -0
  41. data/crates/reek-core/src/detectors/uncommunicative_parameter_name.rs +49 -0
  42. data/crates/reek-core/src/detectors/uncommunicative_variable_name.rs +138 -0
  43. data/crates/reek-core/src/detectors/unused_parameters.rs +21 -0
  44. data/crates/reek-core/src/detectors/unused_private_method.rs +59 -0
  45. data/crates/reek-core/src/detectors/utility_function.rs +27 -0
  46. data/crates/reek-core/src/encodings.rs +180 -0
  47. data/crates/reek-core/src/examiner.rs +165 -0
  48. data/crates/reek-core/src/lib.rs +21 -0
  49. data/crates/reek-core/src/node.rs +423 -0
  50. data/crates/reek-core/src/ruby_format.rs +60 -0
  51. data/crates/reek-core/src/ruby_inspect.rs +172 -0
  52. data/crates/reek-core/src/ruby_regexp.rs +243 -0
  53. data/crates/reek-core/src/single_byte_encodings.rs +6007 -0
  54. data/crates/reek-core/src/source.rs +280 -0
  55. data/crates/reek-core/src/translate.rs +3112 -0
  56. data/crates/reek-core/src/yaml.rs +520 -0
  57. data/ext/reek_rs/Cargo.toml +18 -0
  58. data/ext/reek_rs/extconf.rb +6 -0
  59. data/ext/reek_rs/src/lib.rs +155 -0
  60. data/lib/reek_rs/version.rb +8 -0
  61. data/lib/reek_rs.rb +390 -0
  62. metadata +123 -0
@@ -0,0 +1,179 @@
1
+ //! `Parser::Source::Comment.associate(ast, comments)` and Reek's dressing
2
+ //! of the tree with the result.
3
+ //!
4
+ //! `associate` keys its result by node with `eql?`, so structurally equal
5
+ //! nodes share one list: a comment above the first of two identical method
6
+ //! definitions is attached to both. `TreeDresser#dress` then looks up every
7
+ //! node of the tree, giving each node the comments of its equality class.
8
+
9
+ use std::collections::HashMap;
10
+ use std::hash::{DefaultHasher, Hash, Hasher};
11
+
12
+ use crate::ast::{Child, NodeId, Range, Tree};
13
+ use crate::ruby_regexp::RubyRegexp;
14
+
15
+ /// Node types whose children `children_in_source_order` sorts by position.
16
+ const POSTFIX_TYPES: [&str; 6] = ["if", "while", "while_post", "until", "until_post", "masgn"];
17
+
18
+ /// `Associator::MAGIC_COMMENT_RE`.
19
+ const MAGIC_COMMENT_RE: &str = r"^#\s*(-\*-|)\s*(frozen_string_literal|warn_indent|warn_past_scope):.*\1$";
20
+
21
+ /// `Source::Buffer::ENCODING_RE`.
22
+ pub const ENCODING_RE: &str =
23
+ r"[\s#](en)?coding\s*[:=]\s*((utf8-mac)|([A-Za-z0-9_-]+?)(-unix|-dos|-mac)|([A-Za-z0-9_-]+))";
24
+
25
+ /// Attaches the tree's comments to its nodes as Reek does.
26
+ pub fn associate(tree: &mut Tree) {
27
+ let Some(root) = tree.root else { return };
28
+ let hashes = parser_hashes(tree);
29
+ let mut associator = Associator { tree, mapping: Mapping { hashes, buckets: HashMap::new() }, current: 0 };
30
+ associator.advance_through_directives();
31
+ associator.visit(root);
32
+ let mapping = associator.mapping;
33
+ let ids: Vec<NodeId> = (0..tree.nodes.len() as u32).map(NodeId).collect();
34
+ for id in ids {
35
+ if let Some(comments) = mapping.get(tree, id) {
36
+ tree.nodes[id.0 as usize].comments = comments.to_vec();
37
+ }
38
+ }
39
+ }
40
+
41
+ /// Each node's `hash` as the parser gem's nodes have it: structural, but
42
+ /// hashing a `<<~` heredoc string's value from before dedenting
43
+ /// ([`crate::ast::NodeData::hashed_value`]). A node's children always come
44
+ /// before it in the tree.
45
+ fn parser_hashes(tree: &Tree) -> Vec<u64> {
46
+ let mut hashes: Vec<u64> = Vec::with_capacity(tree.nodes.len());
47
+ for node in &tree.nodes {
48
+ let mut hasher = DefaultHasher::new();
49
+ node.ty.hash(&mut hasher);
50
+ for child in &node.children {
51
+ match child {
52
+ Child::Node(id) => hashes[id.0 as usize].hash(&mut hasher),
53
+ Child::Str(value) => node.hashed_value.as_ref().unwrap_or(value).hash(&mut hasher),
54
+ other => tree.child_hash(other).hash(&mut hasher),
55
+ }
56
+ }
57
+ hashes.push(hasher.finish());
58
+ }
59
+ hashes
60
+ }
61
+
62
+ /// A `Hash` keyed by node: buckets by the parser gem's node hash, then
63
+ /// `eql?`.
64
+ struct Mapping {
65
+ hashes: Vec<u64>,
66
+ buckets: HashMap<u64, Vec<(NodeId, Vec<usize>)>>,
67
+ }
68
+
69
+ impl Mapping {
70
+ fn push(&mut self, tree: &Tree, node: NodeId, comment: usize) {
71
+ let bucket = self.buckets.entry(self.hashes[node.0 as usize]).or_default();
72
+ match bucket.iter_mut().find(|(key, _)| tree.eql(*key, node)) {
73
+ Some((_, comments)) => comments.push(comment),
74
+ None => bucket.push((node, vec![comment])),
75
+ }
76
+ }
77
+
78
+ fn get(&self, tree: &Tree, node: NodeId) -> Option<&[usize]> {
79
+ let bucket = self.buckets.get(&self.hashes[node.0 as usize])?;
80
+ bucket.iter().find(|(key, _)| tree.eql(*key, node)).map(|(_, comments)| comments.as_slice())
81
+ }
82
+ }
83
+
84
+ struct Associator<'t> {
85
+ tree: &'t mut Tree,
86
+ mapping: Mapping,
87
+ current: usize,
88
+ }
89
+
90
+ impl Associator<'_> {
91
+ fn current_comment(&self) -> Option<(Range, usize)> {
92
+ self.tree.comments.get(self.current).map(|comment| (comment.range, self.current))
93
+ }
94
+
95
+ fn advance_through_directives(&mut self) {
96
+ if self.tree.comments.get(self.current).is_some_and(|c| c.text.starts_with(b"#!")) {
97
+ self.current += 1;
98
+ }
99
+ let magic = RubyRegexp::new(MAGIC_COMMENT_RE, "").expect("magic comment regexp");
100
+ if self.tree.comments.get(self.current).is_some_and(|c| magic.is_match(&c.text)) {
101
+ self.current += 1;
102
+ }
103
+ let encoding = RubyRegexp::new(ENCODING_RE, "x").expect("encoding regexp");
104
+ if self.tree.comments.get(self.current).is_some_and(|c| encoding.is_match(&c.text)) {
105
+ self.current += 1;
106
+ }
107
+ }
108
+
109
+ fn visit(&mut self, node: NodeId) {
110
+ self.process_leading_comments(node);
111
+ let Some((comment, _)) = self.current_comment() else { return };
112
+ let comment_line = self.tree.line_for(comment.start);
113
+ let heredoc = self.tree.loc(node).is_some_and(|loc| loc.heredoc);
114
+ let last_line = self.tree.last_line(node).unwrap_or(0);
115
+ if comment_line <= last_line || heredoc {
116
+ for child in self.children_in_source_order(node) {
117
+ self.visit(child);
118
+ }
119
+ self.process_trailing_comments(node);
120
+ }
121
+ }
122
+
123
+ fn children_in_source_order(&self, node: NodeId) -> Vec<NodeId> {
124
+ let mut children: Vec<NodeId> = if POSTFIX_TYPES.contains(&self.tree.ty(node)) {
125
+ self.tree.child_nodes(node).collect()
126
+ } else {
127
+ self.tree.child_nodes(node).filter(|child| self.tree.expression(*child).is_some()).collect()
128
+ };
129
+ if POSTFIX_TYPES.contains(&self.tree.ty(node)) {
130
+ children.sort_by_key(|child| self.tree.expression(*child).map_or(0, |range| range.start));
131
+ }
132
+ children
133
+ }
134
+
135
+ fn process_leading_comments(&mut self, node: NodeId) {
136
+ if self.tree.ty(node) == "begin" {
137
+ return;
138
+ }
139
+ while self.current_comment_before(node) {
140
+ self.associate_and_advance(node);
141
+ }
142
+ }
143
+
144
+ fn process_trailing_comments(&mut self, node: NodeId) {
145
+ while self.current_comment_before_end(node) {
146
+ self.associate_and_advance(node);
147
+ }
148
+ while self.current_comment_decorates(node) {
149
+ self.associate_and_advance(node);
150
+ }
151
+ }
152
+
153
+ fn current_comment_before(&self, node: NodeId) -> bool {
154
+ match (self.current_comment(), self.tree.expression(node)) {
155
+ (Some((comment, _)), Some(range)) => comment.end <= range.start,
156
+ _ => false,
157
+ }
158
+ }
159
+
160
+ fn current_comment_before_end(&self, node: NodeId) -> bool {
161
+ match (self.current_comment(), self.tree.expression(node)) {
162
+ (Some((comment, _)), Some(range)) => comment.end <= range.end,
163
+ _ => false,
164
+ }
165
+ }
166
+
167
+ fn current_comment_decorates(&self, node: NodeId) -> bool {
168
+ match self.current_comment() {
169
+ Some((comment, _)) => Some(self.tree.line_for(comment.start)) == self.tree.last_line(node),
170
+ None => false,
171
+ }
172
+ }
173
+
174
+ fn associate_and_advance(&mut self, node: NodeId) {
175
+ let index = self.current;
176
+ self.mapping.push(self.tree, node, index);
177
+ self.current += 1;
178
+ }
179
+ }