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,155 @@
|
|
|
1
|
+
//! Ruby binding for reek-core. `lib/reek_rs.rb` builds Reek's public API
|
|
2
|
+
//! (`Examiner`, `SmellWarning`, `Configuration::AppConfiguration`) on top of
|
|
3
|
+
//! `ReekRs::Native`.
|
|
4
|
+
|
|
5
|
+
use std::path::Path;
|
|
6
|
+
|
|
7
|
+
use magnus::{function, method, prelude::*, Error, RArray, RString, Ruby, Value};
|
|
8
|
+
use rayon::prelude::*;
|
|
9
|
+
use reek_core::config::{AppConfiguration, ConfigError};
|
|
10
|
+
use reek_core::detectors::{Parameter, SmellWarning};
|
|
11
|
+
use reek_core::examiner::{examine, examine_file, ExaminerError};
|
|
12
|
+
|
|
13
|
+
/// `ReekRs::Native::Configuration`: a loaded configuration.
|
|
14
|
+
#[magnus::wrap(class = "ReekRs::Native::Configuration", free_immediately, size)]
|
|
15
|
+
struct NativeConfiguration(AppConfiguration);
|
|
16
|
+
|
|
17
|
+
/// A configuration, or the error loading it: `[configuration, nil, nil]` or
|
|
18
|
+
/// `[nil, error_class, message]`.
|
|
19
|
+
fn configuration_result(ruby: &Ruby, result: Result<AppConfiguration, ConfigError>) -> Result<RArray, Error> {
|
|
20
|
+
let array = ruby.ary_new_capa(3);
|
|
21
|
+
match result {
|
|
22
|
+
Ok(configuration) => {
|
|
23
|
+
array.push(NativeConfiguration(configuration))?;
|
|
24
|
+
array.push(ruby.qnil())?;
|
|
25
|
+
array.push(ruby.qnil())?;
|
|
26
|
+
}
|
|
27
|
+
Err(error) => {
|
|
28
|
+
array.push(ruby.qnil())?;
|
|
29
|
+
array.push(error.class)?;
|
|
30
|
+
array.push(error.message)?;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
Ok(array)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/// `Native::Configuration.from_default_path`.
|
|
37
|
+
fn configuration_from_default_path(ruby: &Ruby) -> Result<RArray, Error> {
|
|
38
|
+
configuration_result(ruby, AppConfiguration::from_default_path())
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/// `Native::Configuration.from_path(path)`.
|
|
42
|
+
fn configuration_from_path(ruby: &Ruby, path: String) -> Result<RArray, Error> {
|
|
43
|
+
configuration_result(ruby, AppConfiguration::from_path(Path::new(&path)))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/// `Native::Configuration.default`.
|
|
47
|
+
fn configuration_default() -> NativeConfiguration {
|
|
48
|
+
NativeConfiguration(AppConfiguration::default())
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
impl NativeConfiguration {
|
|
52
|
+
/// `#exclude_paths`: the patterns, as written.
|
|
53
|
+
fn exclude_paths(&self) -> Vec<String> {
|
|
54
|
+
self.0.exclude_paths().to_vec()
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/// `#directive_error(origin)`: `nil`, or `[error_class, message]` when
|
|
58
|
+
/// choosing the directive for the file fails.
|
|
59
|
+
fn directive_error(&self, origin: String) -> Option<(String, String)> {
|
|
60
|
+
self.0.directive_for(&origin).err().map(|error| (error.class.to_string(), error.message))
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/// One file's result: `[:smells, [smell, ...]]` or `[:error, error_class,
|
|
65
|
+
/// message, cause]`, where a smell is `[smell_type, context, lines, message,
|
|
66
|
+
/// source, [[parameter, value], ...]]`.
|
|
67
|
+
fn result_to_ruby(ruby: &Ruby, result: Result<Vec<SmellWarning>, ExaminerError>) -> Result<RArray, Error> {
|
|
68
|
+
let array = ruby.ary_new_capa(4);
|
|
69
|
+
match result {
|
|
70
|
+
Ok(smells) => {
|
|
71
|
+
array.push(ruby.to_symbol("smells"))?;
|
|
72
|
+
let list = ruby.ary_new_capa(smells.len());
|
|
73
|
+
for smell in &smells {
|
|
74
|
+
list.push(smell_to_ruby(ruby, smell)?)?;
|
|
75
|
+
}
|
|
76
|
+
array.push(list)?;
|
|
77
|
+
}
|
|
78
|
+
Err(error) => {
|
|
79
|
+
array.push(ruby.to_symbol("error"))?;
|
|
80
|
+
array.push(error.class)?;
|
|
81
|
+
array.push(error.message)?;
|
|
82
|
+
array.push(error.cause)?;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
Ok(array)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
fn smell_to_ruby(ruby: &Ruby, smell: &SmellWarning) -> Result<RArray, Error> {
|
|
89
|
+
let parameters = ruby.ary_new_capa(smell.parameters.len());
|
|
90
|
+
for (key, value) in &smell.parameters {
|
|
91
|
+
let value: Value = match value {
|
|
92
|
+
Parameter::String(text) => ruby.str_new(text).as_value(),
|
|
93
|
+
Parameter::Integer(number) => ruby.integer_from_i64(*number).as_value(),
|
|
94
|
+
Parameter::Strings(texts) => ruby.ary_from_vec(texts.clone()).as_value(),
|
|
95
|
+
};
|
|
96
|
+
parameters.push((ruby.to_symbol(key), value))?;
|
|
97
|
+
}
|
|
98
|
+
let array = ruby.ary_new_capa(6);
|
|
99
|
+
array.push(smell.smell_type)?;
|
|
100
|
+
array.push(smell.context.as_str())?;
|
|
101
|
+
array.push(ruby.ary_from_vec(smell.lines.clone()))?;
|
|
102
|
+
array.push(smell.message.as_str())?;
|
|
103
|
+
array.push(smell.source.as_str())?;
|
|
104
|
+
array.push(parameters)?;
|
|
105
|
+
Ok(array)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/// `Native.examine(source, origin, configuration, filter_by_smells)`.
|
|
109
|
+
fn native_examine(
|
|
110
|
+
ruby: &Ruby,
|
|
111
|
+
source: RString,
|
|
112
|
+
origin: String,
|
|
113
|
+
configuration: &NativeConfiguration,
|
|
114
|
+
filter_by_smells: Vec<String>,
|
|
115
|
+
) -> Result<RArray, Error> {
|
|
116
|
+
// SAFETY: the bytes are copied before any Ruby code can run.
|
|
117
|
+
let bytes = unsafe { source.as_slice() }.to_vec();
|
|
118
|
+
let result = configuration.0.directive_for(&origin).map_err(ExaminerError::from_config).and_then(|directive| {
|
|
119
|
+
examine(&bytes, &origin, &directive, &filter_by_smells).map_err(|error| ExaminerError::from_examine(error, &origin))
|
|
120
|
+
});
|
|
121
|
+
result_to_ruby(ruby, result)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/// `Native.examine_files(paths, configuration, filter_by_smells)`: reads and
|
|
125
|
+
/// examines the files in parallel; results come back in input order.
|
|
126
|
+
fn native_examine_files(
|
|
127
|
+
ruby: &Ruby,
|
|
128
|
+
paths: Vec<String>,
|
|
129
|
+
configuration: &NativeConfiguration,
|
|
130
|
+
filter_by_smells: Vec<String>,
|
|
131
|
+
) -> Result<RArray, Error> {
|
|
132
|
+
let results: Vec<_> =
|
|
133
|
+
paths.par_iter().map(|path| examine_file(path, &configuration.0, &filter_by_smells)).collect();
|
|
134
|
+
let array = ruby.ary_new_capa(results.len());
|
|
135
|
+
for result in results {
|
|
136
|
+
array.push(result_to_ruby(ruby, result)?)?;
|
|
137
|
+
}
|
|
138
|
+
Ok(array)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
#[magnus::init]
|
|
142
|
+
fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
143
|
+
let reek_rs = ruby.define_module("ReekRs")?;
|
|
144
|
+
let native = reek_rs.define_module("Native")?;
|
|
145
|
+
native.define_singleton_method("examine", function!(native_examine, 4))?;
|
|
146
|
+
native.define_singleton_method("examine_files", function!(native_examine_files, 3))?;
|
|
147
|
+
native.const_set("REEK_COMPAT", reek_core::REEK_COMPAT)?;
|
|
148
|
+
let configuration = native.define_class("Configuration", ruby.class_object())?;
|
|
149
|
+
configuration.define_singleton_method("from_default_path", function!(configuration_from_default_path, 0))?;
|
|
150
|
+
configuration.define_singleton_method("from_path", function!(configuration_from_path, 1))?;
|
|
151
|
+
configuration.define_singleton_method("default", function!(configuration_default, 0))?;
|
|
152
|
+
configuration.define_method("exclude_paths", method!(NativeConfiguration::exclude_paths, 0))?;
|
|
153
|
+
configuration.define_method("directive_error", method!(NativeConfiguration::directive_error, 1))?;
|
|
154
|
+
Ok(())
|
|
155
|
+
}
|
data/lib/reek_rs.rb
ADDED
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "pathname"
|
|
4
|
+
require_relative "reek_rs/version"
|
|
5
|
+
|
|
6
|
+
# Precompiled gems ship one library per Ruby minor version.
|
|
7
|
+
begin
|
|
8
|
+
require_relative "reek_rs/#{RUBY_VERSION[/\d+\.\d+/]}/reek_rs"
|
|
9
|
+
rescue LoadError
|
|
10
|
+
require_relative "reek_rs/reek_rs"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Reek 6.5.0's code smells, computed in Rust from Prism's syntax tree.
|
|
14
|
+
#
|
|
15
|
+
# ReekRs has the parts of Reek's public API that tools such as RubyCritic
|
|
16
|
+
# use: Examiner, SmellWarning, Configuration::AppConfiguration and the
|
|
17
|
+
# Errors classes, under ReekRs instead of Reek.
|
|
18
|
+
module ReekRs
|
|
19
|
+
# Reek::Errors.
|
|
20
|
+
module Errors
|
|
21
|
+
# Reek::Errors::BaseError.
|
|
22
|
+
class BaseError < ::RuntimeError
|
|
23
|
+
def long_message
|
|
24
|
+
message
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# The `long_message` of the errors that wrap another exception: the
|
|
29
|
+
# message, an explanation, and the wrapped exception (the `cause`).
|
|
30
|
+
module WrappingError
|
|
31
|
+
def long_message
|
|
32
|
+
format(self.class::LONG_TEMPLATE,
|
|
33
|
+
message: message,
|
|
34
|
+
exception: cause.inspect,
|
|
35
|
+
original: Array(cause&.backtrace).join("\n\t"))
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Reek::Errors::SyntaxError.
|
|
40
|
+
class SyntaxError < BaseError
|
|
41
|
+
include WrappingError
|
|
42
|
+
|
|
43
|
+
TEMPLATE = "Source '%<source>s' cannot be processed by Reek due to a syntax error in the source file."
|
|
44
|
+
|
|
45
|
+
LONG_TEMPLATE = <<-MESSAGE
|
|
46
|
+
!!!
|
|
47
|
+
%<message>s
|
|
48
|
+
|
|
49
|
+
This is a problem that is outside of Reek's scope and should be fixed by you, the
|
|
50
|
+
user, in order for Reek being able to continue.
|
|
51
|
+
|
|
52
|
+
Things you can try:
|
|
53
|
+
- Check the syntax of the problematic file
|
|
54
|
+
- If the file is not in fact a Ruby file, exclude it in your .reek.yml file
|
|
55
|
+
|
|
56
|
+
Exception message:
|
|
57
|
+
|
|
58
|
+
%<exception>s
|
|
59
|
+
|
|
60
|
+
Original backtrace:
|
|
61
|
+
|
|
62
|
+
%<original>s
|
|
63
|
+
|
|
64
|
+
!!!
|
|
65
|
+
MESSAGE
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Reek::Errors::EncodingError.
|
|
69
|
+
class EncodingError < BaseError
|
|
70
|
+
include WrappingError
|
|
71
|
+
|
|
72
|
+
TEMPLATE = "Source '%<source>s' cannot be processed by Reek due to an encoding error in the source file."
|
|
73
|
+
|
|
74
|
+
LONG_TEMPLATE = <<-MESSAGE
|
|
75
|
+
!!!
|
|
76
|
+
%<message>s
|
|
77
|
+
|
|
78
|
+
This is a problem that is outside of Reek's scope and should be fixed by you, the
|
|
79
|
+
user, in order for Reek being able to continue.
|
|
80
|
+
Check out this article for an idea on how to get started:
|
|
81
|
+
https://www.justinweiss.com/articles/3-steps-to-fix-encoding-problems-in-ruby/
|
|
82
|
+
|
|
83
|
+
Exception message:
|
|
84
|
+
|
|
85
|
+
%<exception>s
|
|
86
|
+
|
|
87
|
+
Original backtrace:
|
|
88
|
+
|
|
89
|
+
%<original>s
|
|
90
|
+
|
|
91
|
+
!!!
|
|
92
|
+
MESSAGE
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Reek::Errors::IncomprehensibleSourceError.
|
|
96
|
+
class IncomprehensibleSourceError < BaseError
|
|
97
|
+
include WrappingError
|
|
98
|
+
|
|
99
|
+
TEMPLATE = "Source %<source>s cannot be processed by Reek."
|
|
100
|
+
|
|
101
|
+
LONG_TEMPLATE = <<-MESSAGE
|
|
102
|
+
!!!
|
|
103
|
+
%<message>s
|
|
104
|
+
|
|
105
|
+
This is most likely a Reek bug.
|
|
106
|
+
|
|
107
|
+
It would be great if you could report this back to the Reek team by opening a
|
|
108
|
+
corresponding issue at https://github.com/troessner/reek/issues.
|
|
109
|
+
|
|
110
|
+
Please make sure to include the source in question, the Reek version,
|
|
111
|
+
and this entire error message, including the original exception below.
|
|
112
|
+
|
|
113
|
+
Exception message:
|
|
114
|
+
|
|
115
|
+
%<exception>s
|
|
116
|
+
|
|
117
|
+
Original exception:
|
|
118
|
+
|
|
119
|
+
%<original>s
|
|
120
|
+
|
|
121
|
+
!!!
|
|
122
|
+
MESSAGE
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
class ConfigFileError < BaseError; end
|
|
126
|
+
class BadDetectorInCommentError < BaseError; end
|
|
127
|
+
class BadDetectorConfigurationKeyInCommentError < BaseError; end
|
|
128
|
+
class GarbageDetectorConfigurationInCommentError < BaseError; end
|
|
129
|
+
class LegacyCommentSeparatorError < BaseError; end
|
|
130
|
+
|
|
131
|
+
# The error class for a class name the native extension reports.
|
|
132
|
+
def self.class_for(name)
|
|
133
|
+
case name
|
|
134
|
+
when /\AReek::Errors::(\w+)\z/ then const_get(Regexp.last_match(1))
|
|
135
|
+
when "RegexpError" then ::RegexpError
|
|
136
|
+
else IncomprehensibleSourceError
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Raises the error the native extension reports. `cause` is the message
|
|
141
|
+
# of the exception Reek would have wrapped, when there is one.
|
|
142
|
+
def self.raise_native(name, message, cause = nil)
|
|
143
|
+
raise class_for(name), message unless cause
|
|
144
|
+
|
|
145
|
+
begin
|
|
146
|
+
raise RuntimeError, cause
|
|
147
|
+
rescue RuntimeError
|
|
148
|
+
raise class_for(name), message
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Reek::DocumentationLink.
|
|
154
|
+
module DocumentationLink
|
|
155
|
+
HELP_LINK_TEMPLATE = "https://github.com/troessner/reek/blob/v%<version>s/docs/%<item>s.md"
|
|
156
|
+
|
|
157
|
+
module_function
|
|
158
|
+
|
|
159
|
+
def build(subject)
|
|
160
|
+
Kernel.format(HELP_LINK_TEMPLATE, version: REEK_COMPAT, item: name_to_param(subject))
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def name_to_param(name)
|
|
164
|
+
name.split(/([A-Z][a-z][a-z]*)/).map(&:strip).reject(&:empty?).join("-")
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# Reek::SmellWarning: one smell.
|
|
169
|
+
class SmellWarning
|
|
170
|
+
include Comparable
|
|
171
|
+
|
|
172
|
+
attr_reader :context, :lines, :message, :parameters, :smell_type, :source
|
|
173
|
+
|
|
174
|
+
def initialize(smell_type, lines:, message:, source:, context: "", parameters: {})
|
|
175
|
+
@smell_type = smell_type
|
|
176
|
+
@source = source
|
|
177
|
+
@context = context.to_s
|
|
178
|
+
@lines = lines
|
|
179
|
+
@message = message
|
|
180
|
+
@parameters = parameters
|
|
181
|
+
freeze
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def hash
|
|
185
|
+
identifying_values.hash
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def <=>(other)
|
|
189
|
+
identifying_values <=> other.identifying_values
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def eql?(other)
|
|
193
|
+
(self <=> other).zero?
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def to_hash
|
|
197
|
+
stringified_params = parameters.transform_keys(&:to_s)
|
|
198
|
+
base_hash.merge(stringified_params)
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def yaml_hash
|
|
202
|
+
to_hash.merge("documentation_link" => explanatory_link)
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def base_message
|
|
206
|
+
"#{smell_type}: #{context} #{message}"
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def explanatory_link
|
|
210
|
+
DocumentationLink.build(smell_type)
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
protected
|
|
214
|
+
|
|
215
|
+
def identifying_values
|
|
216
|
+
[smell_type, context, message, lines]
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
private
|
|
220
|
+
|
|
221
|
+
def base_hash
|
|
222
|
+
{
|
|
223
|
+
"context" => context,
|
|
224
|
+
"lines" => lines,
|
|
225
|
+
"message" => message,
|
|
226
|
+
"smell_type" => smell_type,
|
|
227
|
+
"source" => source
|
|
228
|
+
}
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
# Reek::Configuration.
|
|
233
|
+
module Configuration
|
|
234
|
+
# Reek::Configuration::AppConfiguration: the `.reek.yml` settings.
|
|
235
|
+
class AppConfiguration
|
|
236
|
+
# The `.reek.yml` in the current directory or the nearest directory
|
|
237
|
+
# above it, else in the home directory; the defaults when there is none.
|
|
238
|
+
def self.from_default_path
|
|
239
|
+
new(loaded(Native::Configuration.from_default_path))
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# The configuration file at `path` (the default search when nil).
|
|
243
|
+
def self.from_path(path)
|
|
244
|
+
return from_default_path unless path
|
|
245
|
+
|
|
246
|
+
new(loaded(Native::Configuration.from_path(path.to_s)))
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
def self.default
|
|
250
|
+
new(Native::Configuration.default)
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
def self.loaded((native, error_class, message))
|
|
254
|
+
Errors.raise_native(error_class, message) unless native
|
|
255
|
+
native
|
|
256
|
+
end
|
|
257
|
+
private_class_method :loaded
|
|
258
|
+
|
|
259
|
+
# The native configuration, for Examiner.
|
|
260
|
+
attr_reader :native
|
|
261
|
+
|
|
262
|
+
def initialize(native)
|
|
263
|
+
@native = native
|
|
264
|
+
# ExcludedPaths#add: the patterns, expanded now.
|
|
265
|
+
@excluded_paths = native.exclude_paths.flat_map { |path| Dir[path] }.map { |path| Pathname(path) }
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
def path_excluded?(path)
|
|
269
|
+
@excluded_paths.map(&:expand_path).include?(path.expand_path)
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
# Raises what Reek's `directive_for(origin)` raises, if anything.
|
|
273
|
+
def check_directive_for(origin)
|
|
274
|
+
error_class, message = native.directive_error(origin.to_s)
|
|
275
|
+
Errors.raise_native(error_class, message) if error_class
|
|
276
|
+
end
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
# Reek::Examiner: the smells in one source.
|
|
281
|
+
class Examiner
|
|
282
|
+
# Reek::Examiner::NullHandler: errors are raised.
|
|
283
|
+
class NullHandler
|
|
284
|
+
def handle(_exception)
|
|
285
|
+
false
|
|
286
|
+
end
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
IO_IDENTIFIER = "STDIN"
|
|
290
|
+
STRING_IDENTIFIER = "string"
|
|
291
|
+
|
|
292
|
+
attr_reader :origin
|
|
293
|
+
|
|
294
|
+
# `source` is a Pathname, File, IO or String of code, as for Reek.
|
|
295
|
+
def initialize(source,
|
|
296
|
+
filter_by_smells: [],
|
|
297
|
+
configuration: Configuration::AppConfiguration.default,
|
|
298
|
+
error_handler: NullHandler.new)
|
|
299
|
+
@source = source
|
|
300
|
+
@origin = origin_for(source)
|
|
301
|
+
@filter_by_smells = filter_by_smells.map(&:to_s)
|
|
302
|
+
@configuration = configuration
|
|
303
|
+
@error_handler = error_handler
|
|
304
|
+
configuration.check_directive_for(@origin)
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
def smells
|
|
308
|
+
@smells ||= run
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
def smells_count
|
|
312
|
+
smells.length
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
def smelly?
|
|
316
|
+
!smells.empty?
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
private
|
|
320
|
+
|
|
321
|
+
def run
|
|
322
|
+
wrap_exceptions do
|
|
323
|
+
examine_tree
|
|
324
|
+
end
|
|
325
|
+
rescue StandardError => error
|
|
326
|
+
raise unless @error_handler.handle error
|
|
327
|
+
|
|
328
|
+
[]
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
# Reek's own errors pass through; any other error is reported as one of
|
|
332
|
+
# Reek's, with the original as its cause.
|
|
333
|
+
def wrap_exceptions
|
|
334
|
+
yield
|
|
335
|
+
rescue Errors::BaseError
|
|
336
|
+
raise
|
|
337
|
+
rescue ::EncodingError
|
|
338
|
+
raise Errors::EncodingError, format(Errors::EncodingError::TEMPLATE, source: origin)
|
|
339
|
+
rescue StandardError
|
|
340
|
+
raise Errors::IncomprehensibleSourceError, format(Errors::IncomprehensibleSourceError::TEMPLATE, source: origin)
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
def examine_tree
|
|
344
|
+
status, *details = Native.examine(code, origin, @configuration.native, @filter_by_smells)
|
|
345
|
+
return details.first.map { |values| ReekRs.smell_warning(values) } if status == :smells
|
|
346
|
+
|
|
347
|
+
Errors.raise_native(*details)
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
def origin_for(source)
|
|
351
|
+
case source
|
|
352
|
+
when File then source.path
|
|
353
|
+
when IO then IO_IDENTIFIER
|
|
354
|
+
when Pathname then source.to_s
|
|
355
|
+
when String then STRING_IDENTIFIER
|
|
356
|
+
end
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
def code
|
|
360
|
+
case @source
|
|
361
|
+
when File, Pathname then @source.read
|
|
362
|
+
when IO then @source.readlines.join
|
|
363
|
+
when String then @source
|
|
364
|
+
end
|
|
365
|
+
end
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
# A SmellWarning from the native extension's values.
|
|
369
|
+
def self.smell_warning((smell_type, context, lines, message, source, parameters))
|
|
370
|
+
SmellWarning.new(smell_type, context: context, lines: lines, message: message, source: source,
|
|
371
|
+
parameters: parameters.to_h)
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
# Examines the files in parallel. Returns `{ path => [SmellWarning, ...] }`,
|
|
375
|
+
# with the error Reek would raise in place of the smells for a file it
|
|
376
|
+
# cannot examine.
|
|
377
|
+
def self.examine_files(paths, configuration: Configuration::AppConfiguration.default, filter_by_smells: [])
|
|
378
|
+
paths = paths.map(&:to_s)
|
|
379
|
+
results = Native.examine_files(paths, configuration.native, filter_by_smells.map(&:to_s))
|
|
380
|
+
paths.zip(results).to_h do |path, (status, *details)|
|
|
381
|
+
next [path, details.first.map { |values| smell_warning(values) }] if status == :smells
|
|
382
|
+
|
|
383
|
+
begin
|
|
384
|
+
Errors.raise_native(*details)
|
|
385
|
+
rescue StandardError => error
|
|
386
|
+
[path, error]
|
|
387
|
+
end
|
|
388
|
+
end
|
|
389
|
+
end
|
|
390
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: reek-rs
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Eric Burns
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rb_sys
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
-
|
|
17
|
+
- ~>
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 0.9.130
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
-
|
|
25
|
+
- ~>
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: 0.9.130
|
|
28
|
+
description: |
|
|
29
|
+
ReekRs reproduces Reek 6.5.0's smells (type, context, lines, message,
|
|
30
|
+
source and parameters), configuration files and comment directives, without the
|
|
31
|
+
parser gem.
|
|
32
|
+
executables: []
|
|
33
|
+
extensions:
|
|
34
|
+
- ext/reek_rs/extconf.rb
|
|
35
|
+
extra_rdoc_files: []
|
|
36
|
+
files:
|
|
37
|
+
- Cargo.lock
|
|
38
|
+
- Cargo.toml
|
|
39
|
+
- DIVERGENCES.md
|
|
40
|
+
- LICENSE
|
|
41
|
+
- README.md
|
|
42
|
+
- THIRD_PARTY_NOTICES.md
|
|
43
|
+
- crates/reek-cli/Cargo.toml
|
|
44
|
+
- crates/reek-cli/src/main.rs
|
|
45
|
+
- crates/reek-core/Cargo.toml
|
|
46
|
+
- crates/reek-core/src/ast.rs
|
|
47
|
+
- crates/reek-core/src/code_comment.rs
|
|
48
|
+
- crates/reek-core/src/comments.rs
|
|
49
|
+
- crates/reek-core/src/config.rs
|
|
50
|
+
- crates/reek-core/src/context.rs
|
|
51
|
+
- crates/reek-core/src/detectors/attribute.rs
|
|
52
|
+
- crates/reek-core/src/detectors/boolean_parameter.rs
|
|
53
|
+
- crates/reek-core/src/detectors/class_variable.rs
|
|
54
|
+
- crates/reek-core/src/detectors/control_parameter.rs
|
|
55
|
+
- crates/reek-core/src/detectors/data_clump.rs
|
|
56
|
+
- crates/reek-core/src/detectors/duplicate_method_call.rs
|
|
57
|
+
- crates/reek-core/src/detectors/feature_envy.rs
|
|
58
|
+
- crates/reek-core/src/detectors/instance_variable_assumption.rs
|
|
59
|
+
- crates/reek-core/src/detectors/irresponsible_module.rs
|
|
60
|
+
- crates/reek-core/src/detectors/long_parameter_list.rs
|
|
61
|
+
- crates/reek-core/src/detectors/long_yield_list.rs
|
|
62
|
+
- crates/reek-core/src/detectors/manual_dispatch.rs
|
|
63
|
+
- crates/reek-core/src/detectors/missing_safe_method.rs
|
|
64
|
+
- crates/reek-core/src/detectors/mod.rs
|
|
65
|
+
- crates/reek-core/src/detectors/module_initialize.rs
|
|
66
|
+
- crates/reek-core/src/detectors/nested_iterators.rs
|
|
67
|
+
- crates/reek-core/src/detectors/nil_check.rs
|
|
68
|
+
- crates/reek-core/src/detectors/repeated_conditional.rs
|
|
69
|
+
- crates/reek-core/src/detectors/subclassed_from_core_class.rs
|
|
70
|
+
- crates/reek-core/src/detectors/too_many_constants.rs
|
|
71
|
+
- crates/reek-core/src/detectors/too_many_instance_variables.rs
|
|
72
|
+
- crates/reek-core/src/detectors/too_many_methods.rs
|
|
73
|
+
- crates/reek-core/src/detectors/too_many_statements.rs
|
|
74
|
+
- crates/reek-core/src/detectors/uncommunicative_method_name.rs
|
|
75
|
+
- crates/reek-core/src/detectors/uncommunicative_module_name.rs
|
|
76
|
+
- crates/reek-core/src/detectors/uncommunicative_parameter_name.rs
|
|
77
|
+
- crates/reek-core/src/detectors/uncommunicative_variable_name.rs
|
|
78
|
+
- crates/reek-core/src/detectors/unused_parameters.rs
|
|
79
|
+
- crates/reek-core/src/detectors/unused_private_method.rs
|
|
80
|
+
- crates/reek-core/src/detectors/utility_function.rs
|
|
81
|
+
- crates/reek-core/src/encodings.rs
|
|
82
|
+
- crates/reek-core/src/examiner.rs
|
|
83
|
+
- crates/reek-core/src/lib.rs
|
|
84
|
+
- crates/reek-core/src/node.rs
|
|
85
|
+
- crates/reek-core/src/ruby_format.rs
|
|
86
|
+
- crates/reek-core/src/ruby_inspect.rs
|
|
87
|
+
- crates/reek-core/src/ruby_regexp.rs
|
|
88
|
+
- crates/reek-core/src/single_byte_encodings.rs
|
|
89
|
+
- crates/reek-core/src/source.rs
|
|
90
|
+
- crates/reek-core/src/translate.rs
|
|
91
|
+
- crates/reek-core/src/yaml.rs
|
|
92
|
+
- ext/reek_rs/Cargo.toml
|
|
93
|
+
- ext/reek_rs/extconf.rb
|
|
94
|
+
- ext/reek_rs/src/lib.rs
|
|
95
|
+
- lib/reek_rs.rb
|
|
96
|
+
- lib/reek_rs/version.rb
|
|
97
|
+
homepage: "https://gloom.fm"
|
|
98
|
+
licenses:
|
|
99
|
+
- MIT
|
|
100
|
+
metadata:
|
|
101
|
+
source_code_uri: "https://github.com/gloom-fm/reek-rs"
|
|
102
|
+
bug_tracker_uri: "https://github.com/gloom-fm/reek-rs/issues"
|
|
103
|
+
rubygems_mfa_required: "true"
|
|
104
|
+
rdoc_options: []
|
|
105
|
+
require_paths:
|
|
106
|
+
- lib
|
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
108
|
+
requirements:
|
|
109
|
+
-
|
|
110
|
+
- ">="
|
|
111
|
+
- !ruby/object:Gem::Version
|
|
112
|
+
version: "3.3"
|
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
114
|
+
requirements:
|
|
115
|
+
-
|
|
116
|
+
- ">="
|
|
117
|
+
- !ruby/object:Gem::Version
|
|
118
|
+
version: "0"
|
|
119
|
+
requirements: []
|
|
120
|
+
rubygems_version: 4.1.1
|
|
121
|
+
specification_version: 4
|
|
122
|
+
summary: "Reek 6.5.0 code smells, computed in Rust from Prism's syntax tree"
|
|
123
|
+
test_files: []
|