rfmt 1.7.0 → 2.0.0.beta1
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 +4 -4
- data/CHANGELOG.md +25 -0
- data/Cargo.lock +90 -1225
- data/Cargo.toml +6 -0
- data/README.md +32 -28
- data/ext/rfmt/Cargo.toml +9 -28
- data/ext/rfmt/src/ast/mod.rs +2 -0
- data/ext/rfmt/src/config/mod.rs +267 -30
- data/ext/rfmt/src/error/mod.rs +14 -3
- data/ext/rfmt/src/format/formatter.rs +22 -11
- data/ext/rfmt/src/format/registry.rs +8 -0
- data/ext/rfmt/src/format/rule.rs +208 -136
- data/ext/rfmt/src/format/rules/call.rs +14 -22
- data/ext/rfmt/src/format/rules/fallback.rs +4 -11
- data/ext/rfmt/src/format/rules/if_unless.rs +25 -3
- data/ext/rfmt/src/format/rules/loops.rs +3 -1
- data/ext/rfmt/src/format/rules/variable_write.rs +16 -9
- data/ext/rfmt/src/lib.rs +45 -12
- data/ext/rfmt/src/parser/mod.rs +2 -0
- data/ext/rfmt/src/parser/native_adapter.rs +2735 -0
- data/ext/rfmt/src/parser/prism_adapter.rs +5 -0
- data/ext/rfmt/src/validation.rs +69 -0
- data/ext/rfmt/tests/fixtures/parity/comments_mixed.rb +9 -0
- data/ext/rfmt/tests/fixtures/parity/constructs.rb +50 -0
- data/ext/rfmt/tests/fixtures/parity/embdoc.rb +12 -0
- data/ext/rfmt/tests/fixtures/parity/heredoc_assign.rb +15 -0
- data/ext/rfmt/tests/fixtures/parity/heredoc_call_args.rb +17 -0
- data/ext/rfmt/tests/fixtures/parity/metadata_classes.rb +30 -0
- data/ext/rfmt/tests/fixtures/parity/metadata_conditionals.rb +14 -0
- data/ext/rfmt/tests/fixtures/parity/metadata_defs.rb +33 -0
- data/ext/rfmt/tests/fixtures/parity/multibyte.rb +14 -0
- data/ext/rfmt/tests/fixtures/parity/numeric.rb +6 -0
- data/ext/rfmt/tests/fixtures/parity/plain.rb +31 -0
- data/ext/rfmt/tests/native_parity.rs +245 -0
- data/ext/rfmt/tests/ruby_prism_smoke.rs +66 -0
- data/lib/rfmt/cli.rb +37 -7
- data/lib/rfmt/configuration.rb +2 -20
- data/lib/rfmt/version.rb +1 -1
- data/lib/rfmt.rb +47 -19
- metadata +17 -18
- data/lib/rfmt/prism_bridge.rb +0 -529
- data/lib/rfmt/prism_node_extractor.rb +0 -115
data/ext/rfmt/src/lib.rs
CHANGED
|
@@ -1,50 +1,78 @@
|
|
|
1
|
-
mod ast;
|
|
1
|
+
pub mod ast;
|
|
2
2
|
pub mod config;
|
|
3
3
|
pub mod doc;
|
|
4
|
-
mod error;
|
|
4
|
+
pub mod error;
|
|
5
5
|
pub mod format;
|
|
6
6
|
mod logging;
|
|
7
|
-
mod parser;
|
|
7
|
+
pub mod parser;
|
|
8
8
|
mod policy;
|
|
9
|
+
pub mod validation;
|
|
9
10
|
|
|
10
11
|
use policy::SecurityPolicy;
|
|
11
12
|
|
|
12
13
|
use config::Config;
|
|
13
14
|
use format::Formatter;
|
|
14
15
|
use magnus::{function, prelude::*, Error, Ruby};
|
|
15
|
-
use parser::{
|
|
16
|
+
use parser::{NativeAdapter, RubyParser};
|
|
16
17
|
|
|
17
|
-
fn format_ruby_code(ruby: &Ruby, source: String
|
|
18
|
+
fn format_ruby_code(ruby: &Ruby, source: String) -> Result<String, Error> {
|
|
19
|
+
format_impl(ruby, source, None)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Separate fixed-arity export instead of a variadic `format_code`: magnus
|
|
23
|
+
// handles fixed signatures natively, so no scan_args parsing to get wrong.
|
|
24
|
+
fn format_ruby_code_with_config(
|
|
25
|
+
ruby: &Ruby,
|
|
26
|
+
source: String,
|
|
27
|
+
config_path: Option<String>,
|
|
28
|
+
) -> Result<String, Error> {
|
|
29
|
+
format_impl(ruby, source, config_path)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
fn format_impl(ruby: &Ruby, source: String, config_path: Option<String>) -> Result<String, Error> {
|
|
18
33
|
let policy = SecurityPolicy::default();
|
|
19
34
|
|
|
20
35
|
policy
|
|
21
36
|
.validate_source_size(&source)
|
|
22
37
|
.map_err(|e| e.to_magnus_error(ruby))?;
|
|
23
38
|
|
|
24
|
-
let parser =
|
|
25
|
-
let ast = parser.parse(&
|
|
39
|
+
let parser = NativeAdapter::new();
|
|
40
|
+
let ast = parser.parse(&source).map_err(|e| e.to_magnus_error(ruby))?;
|
|
26
41
|
|
|
27
|
-
let config = Config::
|
|
42
|
+
let config = Config::resolve(config_path.as_deref().map(std::path::Path::new))
|
|
43
|
+
.map_err(|e| e.to_magnus_error(ruby))?;
|
|
28
44
|
let formatter = Formatter::new(config);
|
|
29
45
|
|
|
30
46
|
let formatted = formatter
|
|
31
47
|
.format(&source, &ast)
|
|
32
48
|
.map_err(|e| e.to_magnus_error(ruby))?;
|
|
33
49
|
|
|
50
|
+
validation::validate_output(&formatted).map_err(|e| e.to_magnus_error(ruby))?;
|
|
51
|
+
|
|
34
52
|
Ok(formatted)
|
|
35
53
|
}
|
|
36
54
|
|
|
37
|
-
///
|
|
55
|
+
/// Serialize the effective configuration so Ruby can display exactly what
|
|
56
|
+
/// the formatter will use (CLI `config` command, --config fail-fast check)
|
|
57
|
+
fn resolved_config_yaml(ruby: &Ruby, config_path: Option<String>) -> Result<String, Error> {
|
|
58
|
+
let config = Config::resolve(config_path.as_deref().map(std::path::Path::new))
|
|
59
|
+
.map_err(|e| e.to_magnus_error(ruby))?;
|
|
60
|
+
|
|
61
|
+
serde_yaml::to_string(&config)
|
|
62
|
+
.map_err(|e| Error::new(ruby.exception_standard_error(), e.to_string()))
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/// Parse Ruby source code and return the internal AST representation
|
|
38
66
|
/// This is useful for debugging and integration testing
|
|
39
67
|
fn parse_to_json(ruby: &Ruby, source: String) -> Result<String, Error> {
|
|
40
|
-
let parser =
|
|
68
|
+
let parser = NativeAdapter::new();
|
|
41
69
|
let ast = parser.parse(&source).map_err(|e| e.to_magnus_error(ruby))?;
|
|
42
70
|
|
|
43
71
|
Ok(format!("{:#?}", ast))
|
|
44
72
|
}
|
|
45
73
|
|
|
46
74
|
fn rust_version() -> String {
|
|
47
|
-
"
|
|
75
|
+
format!("{} (Rust)", env!("CARGO_PKG_VERSION"))
|
|
48
76
|
}
|
|
49
77
|
|
|
50
78
|
#[magnus::init]
|
|
@@ -53,8 +81,13 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
|
53
81
|
|
|
54
82
|
let module = ruby.define_module("Rfmt")?;
|
|
55
83
|
|
|
56
|
-
module.define_singleton_method("format_code", function!(format_ruby_code,
|
|
84
|
+
module.define_singleton_method("format_code", function!(format_ruby_code, 1))?;
|
|
85
|
+
module.define_singleton_method(
|
|
86
|
+
"format_code_with_config",
|
|
87
|
+
function!(format_ruby_code_with_config, 2),
|
|
88
|
+
)?;
|
|
57
89
|
module.define_singleton_method("parse_to_json", function!(parse_to_json, 1))?;
|
|
90
|
+
module.define_singleton_method("resolved_config_yaml", function!(resolved_config_yaml, 1))?;
|
|
58
91
|
module.define_singleton_method("rust_version", function!(rust_version, 0))?;
|
|
59
92
|
|
|
60
93
|
Ok(())
|