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
data/README.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# reek-rs
|
|
2
|
+
|
|
3
|
+
> [!WARNING]
|
|
4
|
+
> This is a vibe coded, pseudo port of the [Reek](https://github.com/troessner/reek) Ruby gem. Use at your own risk. When things go right, credit goes to the authors this code is based on. When things go wrong, blame goes to you for installing it.
|
|
5
|
+
|
|
6
|
+
## Credit
|
|
7
|
+
|
|
8
|
+
The code in `reek-rs` was generated from the work of Kevin Rutherford, Timo Roessner, Matijs van Zuijlen, Piotr Szotkowski and the other contributors to [Reek](https://github.com/troessner/reek). All credit for the design of the smell detectors this is based on goes to them.
|
|
9
|
+
|
|
10
|
+
See `THIRD_PARTY_NOTICES.md`, for all notices.
|
|
11
|
+
|
|
12
|
+
## Layout
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
crates/reek-core/ engine: Prism tree -> parser-gem tree -> contexts -> 29 detectors
|
|
16
|
+
crates/reek-cli/ reek-rs binary: JSON output for the parity harness
|
|
17
|
+
ext/reek_rs/ Ruby binding (Magnus, rb_sys)
|
|
18
|
+
lib/reek_rs.rb ReekRs::Examiner, SmellWarning, Configuration::AppConfiguration, Errors
|
|
19
|
+
parity/ oracles, comparers, fixtures, configuration and table generators
|
|
20
|
+
test/ Ruby tests: ReekRs against ::Reek
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Installing
|
|
24
|
+
|
|
25
|
+
The Ruby gem builds the extension with Cargo, so you will need Rust 1.88 or later.
|
|
26
|
+
|
|
27
|
+
As a Ruby gem:
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
gem install reek-rs
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
As a Rust library or the `reek-rs` binary:
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
cargo add reek-core
|
|
37
|
+
cargo install reek-cli
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Building
|
|
41
|
+
|
|
42
|
+
Rust 1.88 or later (encoding_rs 0.8.44 requires it) and Ruby 3.3 or later.
|
|
43
|
+
|
|
44
|
+
```sh
|
|
45
|
+
bundle install
|
|
46
|
+
bundle exec rake compile # builds lib/reek_rs/reek_rs.so
|
|
47
|
+
bundle exec rake test # compares with the real reek gem
|
|
48
|
+
cargo test && cargo clippy --all-targets
|
|
49
|
+
cargo build --release -p reek-cli
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
If `bindgen` reports `'stdarg.h' file not found`, `libclang` cannot find its own headers. You'll need to point it at them, for example (use the path of your installed clang):
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
export BINDGEN_EXTRA_CLANG_ARGS="-I/usr/lib/clang/22/include"
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Ruby API
|
|
59
|
+
|
|
60
|
+
The parts of Reek's API that RubyCritic uses, under `ReekRs` instead of
|
|
61
|
+
`Reek`:
|
|
62
|
+
|
|
63
|
+
```ruby
|
|
64
|
+
require "reek_rs"
|
|
65
|
+
|
|
66
|
+
configuration = ReekRs::Configuration::AppConfiguration.from_default_path
|
|
67
|
+
configuration = ReekRs::Configuration::AppConfiguration.from_path(Pathname(".reek.yml"))
|
|
68
|
+
configuration = ReekRs::Configuration::AppConfiguration.default
|
|
69
|
+
configuration.path_excluded?(Pathname("lib/foo.rb"))
|
|
70
|
+
|
|
71
|
+
examiner = ReekRs::Examiner.new(Pathname("lib/foo.rb"), # or a File, IO or String of code
|
|
72
|
+
configuration: configuration,
|
|
73
|
+
filter_by_smells: ["FeatureEnvy"],
|
|
74
|
+
error_handler: handler) # #handle(error) -> true to continue
|
|
75
|
+
examiner.smells # [ReekRs::SmellWarning, ...]
|
|
76
|
+
examiner.smells_count
|
|
77
|
+
examiner.smelly?
|
|
78
|
+
examiner.origin
|
|
79
|
+
|
|
80
|
+
smell = examiner.smells.first
|
|
81
|
+
smell.smell_type # "FeatureEnvy"
|
|
82
|
+
smell.context # "Foo#bar"
|
|
83
|
+
smell.lines # [12, 14]
|
|
84
|
+
smell.message
|
|
85
|
+
smell.source # "lib/foo.rb"
|
|
86
|
+
smell.parameters # { name: "other" }
|
|
87
|
+
smell.to_hash / smell.yaml_hash / smell.base_message / smell.explanatory_link
|
|
88
|
+
|
|
89
|
+
ReekRs.examine_files(paths, configuration:, filter_by_smells: [])
|
|
90
|
+
# { path => [SmellWarning, ...] or the error Reek would raise }, examined in parallel
|
|
91
|
+
ReekRs::REEK_COMPAT # "6.5.0"
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Errors are `ReekRs::Errors::*`, named like `Reek::Errors::*` (`SyntaxError`,
|
|
95
|
+
`EncodingError`, `IncomprehensibleSourceError`, `ConfigFileError`, and the
|
|
96
|
+
four comment errors). An invalid regexp in a configuration file raises
|
|
97
|
+
`RegexpError`, as in Reek.
|
|
98
|
+
|
|
99
|
+
## CLI
|
|
100
|
+
|
|
101
|
+
```sh
|
|
102
|
+
reek-rs --smells [--config FILE] PATHS... # one JSON line per file
|
|
103
|
+
reek-rs --dump-ast PATHS... # the parser-gem tree Reek examines
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Parity harness
|
|
107
|
+
|
|
108
|
+
```sh
|
|
109
|
+
cd parity && bundle install
|
|
110
|
+
./run.sh # fixtures (default)
|
|
111
|
+
./run.sh path/to/code ... # any files or directories
|
|
112
|
+
REEK_CONFIG=configs/full.reek.yml ./run.sh path/to/code
|
|
113
|
+
./ast_run.sh path/to/code ... # compares trees instead of smells
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
`run.sh` examines the files with both the original Reek gem (`oracle.rb`) and `reek-rs --smells`, then `compare.rb` diffs every smell and error. `ast_run.sh` does the same for the tree Reek builds (`ast_oracle.rb`, `ast_compare.rb`).
|
|
117
|
+
|
|
118
|
+
On parity:
|
|
119
|
+
- `parity/generate_single_byte_encodings.rb` rewrites Ruby's single-byte transcoding tables in `crates/reek-core/src/single_byte_encodings.rs`
|
|
120
|
+
- `parity/QUIRKS.md` lists behavior from Reek we're intentionally matching
|
|
121
|
+
- `DIVERGENCES.md` lists known differences
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
MIT (see `LICENSE`). `reek-rs` ports code from Reek and other projects. Their notices are in `THIRD_PARTY_NOTICES.md`.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
`reek-rs` ports code from the projects below. Their copyright notices and
|
|
4
|
+
licenses apply to the parts ported from them.
|
|
5
|
+
|
|
6
|
+
## Reek 6.5.0
|
|
7
|
+
|
|
8
|
+
Smell detectors, their messages, configuration loading and `:reek:` comments (`crates/reek-core`, `ext/reek_rs`, `lib`).
|
|
9
|
+
|
|
10
|
+
Copyright (c) 2008,2009 Kevin Rutherford
|
|
11
|
+
|
|
12
|
+
License: MIT (text below).
|
|
13
|
+
|
|
14
|
+
## Prism 2.1.0
|
|
15
|
+
|
|
16
|
+
`Prism::Translation::Parser` (`crates/reek-core/src/translate.rs`).
|
|
17
|
+
|
|
18
|
+
Copyright 2022-present, Ruby developers
|
|
19
|
+
|
|
20
|
+
License: MIT (text below).
|
|
21
|
+
|
|
22
|
+
## parser 3.3
|
|
23
|
+
|
|
24
|
+
Comment association and node hashing (`crates/reek-core/src/comments.rs`).
|
|
25
|
+
|
|
26
|
+
Copyright (c) 2013-2024 parser project contributors
|
|
27
|
+
Copyright (c) 2013-2016 Catherine <whitequark@whitequark.org>
|
|
28
|
+
|
|
29
|
+
Parts of the source are derived from ruby_parser:
|
|
30
|
+
Copyright (c) Ryan Davis, seattle.rb
|
|
31
|
+
|
|
32
|
+
License: MIT (text below).
|
|
33
|
+
|
|
34
|
+
## License texts
|
|
35
|
+
|
|
36
|
+
### MIT
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
40
|
+
a copy of this software and associated documentation files (the
|
|
41
|
+
"Software"), to deal in the Software without restriction, including
|
|
42
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
43
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
44
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
45
|
+
the following conditions:
|
|
46
|
+
|
|
47
|
+
The above copyright notice and this permission notice shall be
|
|
48
|
+
included in all copies or substantial portions of the Software.
|
|
49
|
+
|
|
50
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
51
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
52
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
53
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
54
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
55
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
56
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
57
|
+
```
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "reek-cli"
|
|
3
|
+
version.workspace = true
|
|
4
|
+
edition.workspace = true
|
|
5
|
+
rust-version.workspace = true
|
|
6
|
+
license.workspace = true
|
|
7
|
+
authors.workspace = true
|
|
8
|
+
homepage.workspace = true
|
|
9
|
+
repository.workspace = true
|
|
10
|
+
description = "reek-rs: Reek 6.5.0's code smells for Ruby files, as JSON"
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
keywords = ["ruby", "reek", "code-smells", "static-analysis", "cli"]
|
|
13
|
+
categories = ["development-tools", "command-line-utilities"]
|
|
14
|
+
|
|
15
|
+
[dependencies]
|
|
16
|
+
reek-core = { version = "0.1.0", path = "../reek-core" }
|
|
17
|
+
rayon.workspace = true
|
|
18
|
+
serde_json.workspace = true
|
|
19
|
+
|
|
20
|
+
[[bin]]
|
|
21
|
+
name = "reek-rs"
|
|
22
|
+
path = "src/main.rs"
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
//! `reek-rs`: Reek 6.5.0 results as JSON.
|
|
2
|
+
//!
|
|
3
|
+
//! reek-rs --smells [--config FILE] PATHS...
|
|
4
|
+
//! reek-rs --dump-ast PATHS...
|
|
5
|
+
//!
|
|
6
|
+
//! `--smells` writes one JSON line per file with its smells (`Examiner#smells`)
|
|
7
|
+
//! or the error Reek raises for it, in the format `parity/oracle.rb` writes.
|
|
8
|
+
//! The configuration is FILE, or the `.reek.yml` Reek would find
|
|
9
|
+
//! (`AppConfiguration.from_default_path`).
|
|
10
|
+
//!
|
|
11
|
+
//! `--dump-ast` writes one JSON line per file with the tree Reek examines
|
|
12
|
+
//! (`SourceCode#syntax_tree`, comments attached), in the format
|
|
13
|
+
//! `parity/ast_oracle.rb` writes, so the two can be compared as text.
|
|
14
|
+
//!
|
|
15
|
+
//! Directories are expanded to the `.rb` files under them, sorted. Files are
|
|
16
|
+
//! processed in parallel and written in input order.
|
|
17
|
+
|
|
18
|
+
use std::path::Path;
|
|
19
|
+
use std::process::ExitCode;
|
|
20
|
+
|
|
21
|
+
use rayon::prelude::*;
|
|
22
|
+
use reek_core::ast::{Child, NodeId, Tree};
|
|
23
|
+
use reek_core::ruby_format::float_to_s;
|
|
24
|
+
use reek_core::detectors::{Parameter, SmellWarning};
|
|
25
|
+
use reek_core::config::AppConfiguration;
|
|
26
|
+
use reek_core::examiner::{examine, ExamineError};
|
|
27
|
+
use reek_core::source::{syntax_tree, SourceError};
|
|
28
|
+
use serde_json::{json, Map, Value};
|
|
29
|
+
|
|
30
|
+
enum Mode {
|
|
31
|
+
Smells,
|
|
32
|
+
DumpAst,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
struct Arguments {
|
|
36
|
+
mode: Option<Mode>,
|
|
37
|
+
config: Option<String>,
|
|
38
|
+
paths: Vec<String>,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
fn main() -> ExitCode {
|
|
42
|
+
let arguments = match parse_arguments() {
|
|
43
|
+
Ok(arguments) => arguments,
|
|
44
|
+
Err(message) => {
|
|
45
|
+
eprintln!("reek-rs: {message}");
|
|
46
|
+
return ExitCode::from(2);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
let files = expand_paths(&arguments.paths);
|
|
50
|
+
match arguments.mode {
|
|
51
|
+
Some(Mode::Smells) => {
|
|
52
|
+
let configuration = match &arguments.config {
|
|
53
|
+
Some(path) => AppConfiguration::from_path(Path::new(path)),
|
|
54
|
+
None => AppConfiguration::from_default_path(),
|
|
55
|
+
};
|
|
56
|
+
let configuration = match configuration {
|
|
57
|
+
Ok(configuration) => configuration,
|
|
58
|
+
Err(error) => {
|
|
59
|
+
eprintln!("reek-rs: {}: {}", error.class, error.message);
|
|
60
|
+
return ExitCode::from(1);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
let lines: Vec<String> =
|
|
64
|
+
files.par_iter().map(|path| smells_record(path, &configuration).to_string()).collect();
|
|
65
|
+
for line in lines {
|
|
66
|
+
println!("{line}");
|
|
67
|
+
}
|
|
68
|
+
ExitCode::SUCCESS
|
|
69
|
+
}
|
|
70
|
+
Some(Mode::DumpAst) => {
|
|
71
|
+
let lines: Vec<String> = files.par_iter().map(|path| dump_ast_record(path).to_string()).collect();
|
|
72
|
+
for line in lines {
|
|
73
|
+
println!("{line}");
|
|
74
|
+
}
|
|
75
|
+
ExitCode::SUCCESS
|
|
76
|
+
}
|
|
77
|
+
None => {
|
|
78
|
+
eprintln!("reek-rs: give --smells or --dump-ast");
|
|
79
|
+
ExitCode::from(2)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
fn parse_arguments() -> Result<Arguments, String> {
|
|
85
|
+
let mut arguments = Arguments { mode: None, config: None, paths: Vec::new() };
|
|
86
|
+
let mut args = std::env::args().skip(1);
|
|
87
|
+
while let Some(argument) = args.next() {
|
|
88
|
+
match argument.as_str() {
|
|
89
|
+
"--config" => arguments.config = Some(args.next().ok_or("--config needs a file")?),
|
|
90
|
+
"--smells" => arguments.mode = Some(Mode::Smells),
|
|
91
|
+
"--dump-ast" => arguments.mode = Some(Mode::DumpAst),
|
|
92
|
+
other if other.starts_with('-') && other != "-" => return Err(format!("unknown option: {other}")),
|
|
93
|
+
path => arguments.paths.push(path.to_string()),
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
Ok(arguments)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
fn expand_paths(paths: &[String]) -> Vec<String> {
|
|
100
|
+
let mut files = Vec::new();
|
|
101
|
+
for path in paths {
|
|
102
|
+
if Path::new(path).is_dir() {
|
|
103
|
+
let mut found = Vec::new();
|
|
104
|
+
collect_ruby_files(Path::new(path), &mut found);
|
|
105
|
+
found.sort();
|
|
106
|
+
files.extend(found);
|
|
107
|
+
} else {
|
|
108
|
+
files.push(path.clone());
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
files
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
fn collect_ruby_files(directory: &Path, found: &mut Vec<String>) {
|
|
115
|
+
let Ok(entries) = std::fs::read_dir(directory) else { return };
|
|
116
|
+
for entry in entries.flatten() {
|
|
117
|
+
let path = entry.path();
|
|
118
|
+
if path.is_dir() {
|
|
119
|
+
collect_ruby_files(&path, found);
|
|
120
|
+
} else if path.extension().is_some_and(|extension| extension == "rb") {
|
|
121
|
+
found.push(path.to_string_lossy().into_owned());
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
fn smells_record(path: &str, configuration: &AppConfiguration) -> Value {
|
|
127
|
+
let directive = match configuration.directive_for(path) {
|
|
128
|
+
Ok(directive) => directive,
|
|
129
|
+
Err(error) => return json!({"path": path, "error": error.class, "message": error.message}),
|
|
130
|
+
};
|
|
131
|
+
let source = match std::fs::read(path) {
|
|
132
|
+
Ok(source) => source,
|
|
133
|
+
Err(error) => return json!({"path": path, "error": "Errno", "message": error.to_string()}),
|
|
134
|
+
};
|
|
135
|
+
match examine(&source, path, &directive, &[]) {
|
|
136
|
+
Ok(smells) => {
|
|
137
|
+
let smells: Vec<Value> = smells.iter().map(smell_json).collect();
|
|
138
|
+
json!({"path": path, "error": null, "smells": smells})
|
|
139
|
+
}
|
|
140
|
+
Err(error) => {
|
|
141
|
+
let mut record = Map::new();
|
|
142
|
+
record.insert("path".into(), json!(path));
|
|
143
|
+
record.insert("error".into(), json!(error.reek_class()));
|
|
144
|
+
record.insert("message".into(), json!(error.message(path)));
|
|
145
|
+
if let ExamineError::Source(SourceError::Incomprehensible(cause) | SourceError::UnsupportedEncoding(cause)) = &error {
|
|
146
|
+
record.insert("cause".into(), json!(cause));
|
|
147
|
+
}
|
|
148
|
+
Value::Object(record)
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
fn smell_json(smell: &SmellWarning) -> Value {
|
|
154
|
+
let parameters: Map<String, Value> = smell
|
|
155
|
+
.parameters
|
|
156
|
+
.iter()
|
|
157
|
+
.map(|(key, value)| {
|
|
158
|
+
let value = match value {
|
|
159
|
+
Parameter::String(text) => json!(text),
|
|
160
|
+
Parameter::Integer(number) => json!(number),
|
|
161
|
+
Parameter::Strings(texts) => json!(texts),
|
|
162
|
+
};
|
|
163
|
+
(key.to_string(), value)
|
|
164
|
+
})
|
|
165
|
+
.collect();
|
|
166
|
+
json!({
|
|
167
|
+
"smell_type": smell.smell_type,
|
|
168
|
+
"context": smell.context,
|
|
169
|
+
"lines": smell.lines,
|
|
170
|
+
"message": smell.message,
|
|
171
|
+
"source": smell.source,
|
|
172
|
+
"parameters": parameters,
|
|
173
|
+
})
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
fn dump_ast_record(path: &str) -> Value {
|
|
177
|
+
let source = match std::fs::read(path) {
|
|
178
|
+
Ok(source) => source,
|
|
179
|
+
Err(error) => return json!({"path": path, "error": format!("Errno: {error}")}),
|
|
180
|
+
};
|
|
181
|
+
match syntax_tree(&source, path) {
|
|
182
|
+
Ok(tree) => {
|
|
183
|
+
let positions = CharPositions::new(&tree);
|
|
184
|
+
let ast = match tree.root {
|
|
185
|
+
Some(root) => dump_node(&tree, &positions, root),
|
|
186
|
+
None => json!(["empty", [], null, []]),
|
|
187
|
+
};
|
|
188
|
+
json!({"path": path, "error": null, "ast": ast})
|
|
189
|
+
}
|
|
190
|
+
Err(error) => {
|
|
191
|
+
let mut record = Map::new();
|
|
192
|
+
record.insert("path".into(), json!(path));
|
|
193
|
+
record.insert("error".into(), json!(error.reek_class()));
|
|
194
|
+
if let SourceError::Incomprehensible(cause) | SourceError::UnsupportedEncoding(cause) = &error {
|
|
195
|
+
record.insert("cause".into(), json!(cause));
|
|
196
|
+
}
|
|
197
|
+
Value::Object(record)
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/// Converts byte offsets to the parser's positions: characters, or bytes
|
|
203
|
+
/// when the buffer is `ASCII-8BIT`.
|
|
204
|
+
struct CharPositions {
|
|
205
|
+
/// `chars[byte offset]` = character offset.
|
|
206
|
+
chars: Option<Vec<u32>>,
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
impl CharPositions {
|
|
210
|
+
fn new(tree: &Tree) -> Self {
|
|
211
|
+
if tree.binary || tree.source.is_ascii() {
|
|
212
|
+
return CharPositions { chars: None };
|
|
213
|
+
}
|
|
214
|
+
let mut chars = Vec::with_capacity(tree.source.len() + 1);
|
|
215
|
+
let mut count = 0u32;
|
|
216
|
+
for byte in &tree.source {
|
|
217
|
+
chars.push(count);
|
|
218
|
+
if byte & 0xC0 != 0x80 {
|
|
219
|
+
count += 1;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
chars.push(count);
|
|
223
|
+
CharPositions { chars: Some(chars) }
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
fn position(&self, byte: u32) -> u32 {
|
|
227
|
+
match &self.chars {
|
|
228
|
+
Some(chars) => chars[byte as usize],
|
|
229
|
+
None => byte,
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
fn dump_node(tree: &Tree, positions: &CharPositions, id: NodeId) -> Value {
|
|
235
|
+
let node = tree.node(id);
|
|
236
|
+
let children: Vec<Value> = node.children.iter().map(|child| dump_child(tree, positions, child)).collect();
|
|
237
|
+
let location = match tree.expression(id) {
|
|
238
|
+
Some(range) => json!([
|
|
239
|
+
positions.position(range.start),
|
|
240
|
+
positions.position(range.end),
|
|
241
|
+
tree.line(id),
|
|
242
|
+
tree.last_line(id),
|
|
243
|
+
node.loc.as_ref().is_some_and(|loc| loc.heredoc),
|
|
244
|
+
]),
|
|
245
|
+
None => Value::Null,
|
|
246
|
+
};
|
|
247
|
+
let comments: Vec<Value> = node.comments.iter().map(|index| text(&tree.comments[*index].text)).collect();
|
|
248
|
+
json!([node.ty, children, location, comments])
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
fn dump_child(tree: &Tree, positions: &CharPositions, child: &Child) -> Value {
|
|
252
|
+
match child {
|
|
253
|
+
Child::Node(id) => dump_node(tree, positions, *id),
|
|
254
|
+
Child::Nil => Value::Null,
|
|
255
|
+
Child::Sym(name) => json!({"sym": text(name)}),
|
|
256
|
+
Child::Str(bytes) => match std::str::from_utf8(bytes) {
|
|
257
|
+
Ok(string) => json!({"str": string}),
|
|
258
|
+
Err(_) => json!({"strx": hex(bytes)}),
|
|
259
|
+
},
|
|
260
|
+
Child::Int(value) => json!({"int": value}),
|
|
261
|
+
Child::Float(value) => json!({"float": float_to_s(*value)}),
|
|
262
|
+
Child::Rational(value) => json!({"rational": value}),
|
|
263
|
+
Child::Complex(value) => json!({"complex": value}),
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/// The oracle's `text`: the string, or its bytes in hex when not UTF-8.
|
|
268
|
+
fn text(bytes: &[u8]) -> Value {
|
|
269
|
+
match std::str::from_utf8(bytes) {
|
|
270
|
+
Ok(string) => json!(string),
|
|
271
|
+
Err(_) => json!(hex(bytes)),
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
fn hex(bytes: &[u8]) -> String {
|
|
276
|
+
bytes.iter().map(|byte| format!("{byte:02x}")).collect()
|
|
277
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "reek-core"
|
|
3
|
+
version.workspace = true
|
|
4
|
+
edition.workspace = true
|
|
5
|
+
rust-version.workspace = true
|
|
6
|
+
license.workspace = true
|
|
7
|
+
authors.workspace = true
|
|
8
|
+
homepage.workspace = true
|
|
9
|
+
repository.workspace = true
|
|
10
|
+
description = "Reek 6.5.0's code smells for Ruby, computed from Prism's syntax tree"
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
keywords = ["ruby", "reek", "code-smells", "static-analysis", "prism"]
|
|
13
|
+
categories = ["development-tools"]
|
|
14
|
+
|
|
15
|
+
[dependencies]
|
|
16
|
+
encoding_rs = "0.8.44"
|
|
17
|
+
indexmap.workspace = true
|
|
18
|
+
onig.workspace = true
|
|
19
|
+
onig_sys = "69.9.3"
|
|
20
|
+
ruby-prism.workspace = true
|
|
21
|
+
yaml-rust2.workspace = true
|